|
******************************************
Welcome To CrazyEngineers (CE) – an online community of engineers from all over the world! With the younger CEan at 84 and the youngest at 16, CE boasts of professional engineers, students, professors, entrepreneurs, CEOs, geeks & nerds. We exchange innovative ideas, share knowledge, help each other and expand our worldwide network of engineers! You need not have a formal degree in engineering to be a part of CrazyEngineers! Need we say more? Join CE! | Be a CE Ambassador! | Forgot password? | Sponsor CE | Contact Us |
![]() |
| Sponsored links | |
|
|
|
|
CE - Apprentice
![]() ![]()
I'm a Crazy Computer Science & Engineering Engineer
Join Date: 16th November 2008 Location: India
Posts: 10
|
I just joined the site and would like to contribute in here
Code in PASCAL using linked list {Program to perform polynomial addition using linked list} Code:
PROGRAM polyaddll(input,output);
TYPE nodeptr=^node;
node=RECORD
coef:integer;
exp:integer;
link:nodeptr;
END;
VAR one,two,res:nodeptr;
k:integer;
PROCEDURE print(n:nodeptr);
VAR p:nodeptr;
BEGIN
p:=n;
WHILE (p<>NIL) DO
BEGIN
write(p^.coef,' X^ ',p^.exp);
IF (p^.link<>NIL) THEN
write(' + ');
p:=p^.link;
END;
writeln;
END;
PROCEDURE create(VAR n:nodeptr);
VAR p,q:nodeptr;
c:char;
BEGIN
c:='y';
WHILE (c<>'n') DO
BEGIN
new(p);
write('Enter the coefficient of the term : ');
readln(k);
p^.coef:=k;
write('Enter the exponent of the term : ');
readln(k);
p^.exp:=k;
p^.link:=NIL;
write('Do you want to add any more terms (Y/N) : ');
readln(c);
IF (n=NIL) THEN
BEGIN
n:=p;
q:=p;
END
ELSE
BEGIN
q^.link:=p;
q:=p;
END;
END;
END;
PROCEDURE sum(VAR a,b,c:nodeptr);
VAR p,q,r,s:nodeptr;
BEGIN
p:=a;
q:=b;
WHILE ((p<>NIL) AND (q<>NIL)) DO
BEGIN
new(r);
r^.link:=NIL;
IF (p^.exp>q^.exp) THEN
BEGIN
r^.coef:=p^.coef;
r^.exp:=p^.exp;
p:=p^.link;
END
ELSE
IF (q^.exp>p^.exp) THEN
BEGIN
r^.coef:=q^.coef;
r^.exp:=q^.exp;
q:=q^.link;
END
ELSE
BEGIN
r^.coef:=p^.coef+q^.coef;
r^.exp:=p^.exp;
p:=p^.link;
q:=q^.link;
END;
IF (c=NIL) THEN
BEGIN
c:=r;
s:=r;
END
ELSE
BEGIN
s^.link:=r;
s:=r;
END;
END;
WHILE (p<>NIL) DO
BEGIN
new(r);
r^.coef:=p^.coef;
r^.exp:=p^.exp;
r^.link:=NIL;
p:=p^.link;
IF (c=NIL) THEN
BEGIN
c:=r;
s:=r;
END
ELSE
BEGIN
s^.link:=r;
s:=r;
END;
END;
WHILE (q<>NIL) DO
BEGIN
new(r);
r^.coef:=q^.coef;
r^.exp:=q^.exp;
r^.link:=NIL;
q:=q^.link;
IF (c=NIL) THEN
BEGIN
c:=r;
s:=r;
END
ELSE
BEGIN
s^.link:=r;
s:=r;
END;
END;
END;
BEGIN
one:=NIL;
two:=NIL;
res:=NIL;
writeln('Enter the first polynomial : ');
create(one);
writeln('Enter the second polynomial : ');
create(two);
writeln('The first polynomial is : ');
print(one);
writeln('The second polynomial is : ');
print(two);
sum(one,two,res);
writeln('The reult of addition is : ');
print(res);
readln;
END.
We Rock!!!!!!!Last edited by The_Big_K : 17th November 2008 at 10:15 PM. |
|
|
|
| Sponsored links | |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
LinkBacks (?)
LinkBack to this Thread: http://www.crazyengineers.com/forum/computer-science-engineering/2122-c-function-addition-two-single-variable-polynomials.html
|
||||
| Posted By | For | Type | Date | |
| Uniting Engineers Across The World ! | This thread | Refback | 28th February 2008 10:38 AM | |
| Contact Us - Home - Impressum | Impressum - |