Single Linked List
Single Linked List
h> int pos,pos1,i; struct list { int no; struct list *next; } *q,*temp,*head; void insert(); void delete(); void display(); void exit(); void main() { int choice; clrscr(); head=(struct list*)malloc(sizeof(struct list)); head->next=NULL; while(1) { printf("\n 1.insert\n2.delete\n3.list all\n 4.exit"); printf("\nEnter the choice:"); scanf("%d",&choice); switch(choice) { case 1: insert(); break; case 2: delete(); break; case 3: display(); break; default: exit(0); break; } } }
void insert() { int ch; temp = (struct list*) malloc(sizeof(struct list)); printf("\n enter the element:"); scanf("%d",&temp->no); if(head->next==NULL) { head->next=temp; temp->next=NULL; } else { printf("\n Enter the position u want to insert:"); printf("\n1.First\n2.Middle\n3.End\n--->"); scanf("%d",&ch); switch(ch) { case 1: temp->next=head->next; head->next=temp; break; case 2: printf("\n Enter the position:"); scanf("%d",&pos); q=head->next; for(i=1;i<pos-1;i++) { q=q->next; } temp->next=q->next; q->next=temp; break; case 3: q=head->next; while(q->next!=NULL) q=q->next; q->next=temp; temp->next=NULL; break; } } }
void delete() { if(head->next==NULL) printf("\nlist is empty"); else { printf("\n Enter the position u want to delete:"); scanf("%d",&pos1); q=head->next; if(pos1==1) { head->next=q->next; } else { for(i=1;i<pos1-1;i++) q=q->next; q->next=q->next->next; } } } void display() { if(head->next==NULL) printf("\nlist is empty"); else { q=head->next; printf("\n the elements present in the list are:\n"); while(q!=NULL) { printf("%d->",q->no); q=q->next; } getch(); } }
Polynomial Addition #include <stdio.h> #include <conio.h> #include <stdlib.h> int ch; struct poly { int coeff; int exp; struct poly *next; }*poly1,*poly2,*poly3,*head1,*head2,*head3,*p,*q,*temp; void input1(); void input2(); void add(); void display(); void main() { clrscr(); head1=(struct poly*)malloc(sizeof(struct poly)); head1->next=NULL; head2=(struct poly*)malloc(sizeof(struct poly)); head2->next=NULL; head3=(struct poly*)malloc(sizeof(struct poly)); head3->next=NULL; while(1) { printf("\n1.Input for polynomial 1\n2.Input for polynomial 2\n3.Polynomial addition\n4.Result\n5.Exit\n"); printf("\n Enter the choice:"); scanf("%d",&ch); switch(ch) { case 1: input1(); break; case 2: input2(); break; case 3: add(); break; case 4: display(); break;
default: exit(0); } } } void input1() { poly1=(struct poly*)malloc(sizeof(struct poly)); printf("\n Enter the co-efficient:"); scanf("%d",&poly1->coeff); printf("\n Enter the power:"); scanf("%d",&poly1->exp); if(head1->next==NULL) { head1->next=poly1; poly1->next=NULL; } else { poly1->next=head1->next; head1->next=poly1; } } void input2() { poly2=(struct poly*)malloc(sizeof(struct poly)); printf("\n Enter the co-efficient:"); scanf("%d",&poly2->coeff); printf("\n Enter the power:"); scanf("%d",&poly2->exp); if(head2->next==NULL) { head2->next=poly2; poly2->next=NULL; } else { poly2->next=head2->next; head2->next=poly2; } } void add() { p=head1->next; q=head2->next;
while(p!=NULL && q!=NULL) { poly3=(struct poly*)malloc(sizeof(struct poly)); if(p->exp==q->exp) { poly3->exp=p->exp; poly3->coeff=p->coeff+q->coeff; p=p->next; q=q->next; } else if(p->exp>q->exp) { poly3->coeff=p->coeff; poly3->exp=p->exp; p=p->next; } else { poly3->coeff=q->coeff; poly3->exp=q->exp; q=q->next; } if(head3->next==NULL) { head3->next=poly3; poly3->next=NULL; } else { poly3->next=head3->next; head3->next=poly3; } } while(p!=NULL) { poly3=(struct poly*)malloc(sizeof(struct poly)); poly3->coeff=p->coeff; poly3->exp=p->exp; p=p->next; if(head3->next==NULL) { head3->next=poly3->next; poly3->next=NULL; } else {
poly3->next=head3->next; head3->next=poly3; } } while(q!=NULL) { poly3=(struct poly*)malloc(sizeof(struct poly)); poly3->coeff=q->coeff; poly3->exp=q->exp; q=q->next; if(head3->next==NULL) { head3->next=poly3->next; poly3->next=NULL; } else { poly3->next=head3->next; head3->next=poly3; } } } void display() { printf("\n The resultant polynomial\n"); if(head3->next==NULL) printf("\n No Terms\n"); else { temp=head3->next; while(temp!=NULL) { printf(" -> %d^%d",temp->coeff,temp->exp); temp=temp->next; } getch(); } }
Output Program5 1.Input for polynomial 1 2.Input for polynomial 2 3.Polynomial addition 4.Result 5.Exit Enter the choice:1 Enter the co-efficient:1 Enter the power:1 1.Input for polynomial 1 2.Input for polynomial 2 3.Polynomial addition 4.Result 5.Exit Enter the choice:2 Enter the co-efficient:1 Enter the power:1 1.Input for polynomial 1 2.Input for polynomial 2 3.Polynomial addition 4.Result 5.Exit Enter the choice:2 Enter the co-efficient:2 Enter the power:2 1.Input for polynomial 1 2.Input for polynomial 2 3.Polynomial addition 4.Result 5.Exit Enter the choice:3 1.Input for polynomial 1 2.Input for polynomial 2 3.Polynomial addition 4.Result 5.Exit Enter the choice:4 The resultant polynomial -> 2^1 -> 2^2