Professional Documents
Culture Documents
*/
#include<stdio.h>
#include<stdlib.h>
int data;
}node;
node *create(){
node *temp,*head=NULL,*prev=NULL;
int ch;
do{
//memory allocation
temp=(node*)malloc(sizeof(node));
if(temp==NULL){
exit(0);
printf("\nEnter data:");
scanf("%d",&temp->data);
temp->next=NULL;
//node insertion
if(head==NULL){
head=temp;
prev=head;
else{
prev->next=temp;
prev=temp;
//wanna repeat
//fflush(stdin);
scanf("%d",&ch);
}while(ch==1);
return head;
node *c=head;
if(head==NULL){
else{
while(c){
printf("%d\t",c->data);
c=c->next;
node *temp;
//memory allocation
temp=(node*)malloc(sizeof(node));
if(temp==NULL){
printf("\nMemory Allocation Failed...");
exit(0);
printf("\nEnter data:");
scanf("%d",&temp->data);
temp->next=head;
head=temp;
return head;
node *temp,*c;
//memory allocation
temp=(node*)malloc(sizeof(node));
if(temp==NULL){
exit(0);
printf("\nEnter data:");
scanf("%d",&temp->data);
temp->next=NULL;
c=head;
while(c->next){
c=c->next;
c->next=temp;
return head;
node *c=head,*temp;
while(--k&&c->next!=NULL)
c=c->next;
if(c->next!=NULL)
temp=(node*)malloc(sizeof(node));
if(temp==NULL)
else
scanf("%d",&temp->data);
temp->next=c->next;
c->next=temp;
else
printf("\nWrong Position...");
node *temp;
if(head==NULL)
return NULL;
temp=head;
head=head->next;
free(temp);
return head;
}
node *temp,*prev=NULL;
if(head==NULL)
return NULL;
node *c=head;
while(c->next){
prev=c;
c=c->next;
prev->next=NULL;
free(c);
return head;
node *c=head,*temp;
while(c){
if(c->data==k){
break;
c=c->next;
if(c==NULL){
else{
temp=(node*)malloc(sizeof(node));
if(temp==NULL)
printf("\nEnter data:");
scanf("%d",&temp->data);
temp->next=c->next;
c->next=temp;
node *c=head,*temp;
--k;
while(--k&&c->next!=NULL)
c=c->next;
if(c->next!=NULL)
temp=(node*)malloc(sizeof(node));
if(temp==NULL)
else
scanf("%d",&temp->data);
temp->next=c->next;
c->next=temp;
else
printf("\nWrong Position...");
}
node *c=head,*temp,*prev=NULL;
while(c){
if(c->data==k){
break;
prev=c;
c=c->next;
if(c==NULL){
else{
temp=(node*)malloc(sizeof(node));
if(temp==NULL)
else{
printf("\nEnter data:");
scanf("%d",&temp->data);
temp->next=prev->next;
prev->next=temp;
node *c=head,*temp;
while(--k&&c->next!=NULL)
c=c->next;
if(c->next!=NULL)
temp=c->next;
c->next=temp->next;
free(temp);
else
printf("\nWrong Position...");
node *c=head,*temp;
if(k==1){
printf("\nWrong Position...");
return;
if(k==2)
return;
k-=2;
while(--k&&c->next!=NULL)
c=c->next;
if(c->next!=NULL)
temp=c->next;
c->next=temp->next;
free(temp);
else
printf("\nWrong Position...");
node *c=head,*temp;
if(k==1)
return;
--k;
while(--k&&c->next!=NULL)
c=c->next;
if(c->next!=NULL)
temp=c->next;
c->next=temp->next;
free(temp);
else
printf("\nWrong Position...");
node *temp,*prev=NULL,*c=head;
//loop to reverse a linked list
while(c){
temp=c;
c=c->next;
temp->next=prev;
prev=temp;
return prev;
node *c,*temp,*prev=NULL;
if(head->data==val){
return;
c=head;
while(c->next){
prev=c;
c=c->next;
if(c->data==val){
break;
if(c->next!=NULL){
temp=c;
prev->next=c->next;
free(temp);
else{
node *c=head;
int index=0;
while(c){
if(c->data==val){
return;
index++;
c=c->next;
node *dummy,*curr;
//memory allocation
dummy=(node*)malloc(sizeof(node));
if(dummy==NULL){
exit(0);
dummy->data=0;
dummy->next=NULL;
curr=dummy;
while(l1&&l2){
if(l1->data<l2->data){
curr->next=l1;
l1=l1->next;
else{
curr->next=l2;
l2=l2->next;
curr=curr->next;
if(l1){
curr->next=l1;
if(l2){
curr->next=l2;
return dummy->next;
node *slow,*fast,*mid,*l1=NULL,*l2=NULL;
if(head==NULL||head->next==NULL)
return head;
slow=head;
fast=head->next;
while(slow->next!=NULL&&fast->next!=NULL){
slow=slow->next;
fast=fast->next;
mid=slow->next;
slow->next=NULL;
l1=sortList(head);
l2=sortList(mid);
return merge(l1,l2);
node *c=h1;
while(c->next){
c=c->next;
c->next=h2;
return h1;
node *c1=h1,*c2=h2;
int flag=1;
while(c1&&c2){
if(c1->data!=c2->data){
flag=0;
break;
c1=c1->next;
c2=c2->next;
}
if((!c1&&c2)||(c1&&!c2)){
flag=0;
if(flag==1){
else{
int main(){
int ch,k;
node *temp,*head=NULL,*head1=NULL,*head2=NULL;
do{
scanf("%d",&ch);
switch(ch){
case 1:
head=create();
break;
case 2:
display(head);
break;
case 3:
head=add_beg(head);
break;
case 4:
head=add_end(head);
break;
case 5:
printf("\nEnter k:");
scanf("%d",&k);
add_after_k(head,k);
break;
case 6:
printf("\nEnter value:");
scanf("%d",&k);
add_after_val(head,k);
break;
case 7:
printf("\nEnter k:");
scanf("%d",&k);
add_before_k(head,k);
break;
case 8:
printf("\nEnter value:");
scanf("%d",&k);
add_before_val(head,k);
break;
case 9:
head=del_beg(head);
break;
case 10:
head=del_end(head);
break;
case 11:
printf("\nEnter k:");
scanf("%d",&k);
del_after_k(head,k);
break;
case 12:
printf("\nEnter k:");
scanf("%d",&k);
del_before_k(head,k);
break;
case 13:
printf("\nEnter k:");
scanf("%d",&k);
del_k(head,k);
break;
case 14:
printf("\nEnter value:");
scanf("%d",&k);
del_val(head,k);
break;
case 15:
head=reverse(head);
break;
case 16:
printf("\nEnter value:");
scanf("%d",&k);
search(head,k);
break;
case 17:
head=sortList(head);
break;
case 18:
head1=create();
head2=create();
display(head1);
display(head2);
head1=merge(head1,head2);
display(head1);
break;
case 19:
head1=create();
head=concat(head,head1);
break;
case 20:
head1=create();
chk_equal(head,head1);
break;
}while(ch>=1&&ch<=20);
return 0;
}
/*
*/
#include<stdio.h>
#include<stdlib.h>
int coeff,exp;
}node;
node *temp=(node*)malloc(sizeof(node));
if(temp==NULL){
exit(0);
temp->coeff=co;
temp->exp=exp;
temp->next=NULL;
return temp;
node *createPoly(){
node *temp,*head=NULL,*prev=NULL;
int ch,co,exp;
do{
temp=getNode(co,exp);
//node insertion
if(head==NULL){
head=temp;
prev=head;
else{
prev->next=temp;
prev=temp;
//wanna repeat
scanf("%d",&ch);
}while(ch==1);
return head;
node *c=head;
if(head==NULL){
else{
while(c){
printf("(%d)x^(%d)",c->coeff,c->exp);
if(c->next)
printf("+");
c=c->next;
node *c1=p1,*c2=p2,*p3=NULL,*temp,*prev=NULL;
if(p1==NULL&&p2==NULL)
return NULL;
if(p1==NULL)
return p2;
if(p2==NULL)
return p1;
while(c1!=NULL||c2!=NULL)
if(c1!=NULL&&c2!=NULL)
if(c1->exp==c2->exp)
temp=getNode(c1->coeff+c2->coeff,c1->exp);
c1=c1->next;
c2=c2->next;
else if(c1->exp>c2->exp)
{
temp=getNode(c1->coeff,c1->exp);
c1=c1->next;
else
temp=getNode(c2->coeff,c2->exp);
c2=c2->next;
else if(c1==NULL)
temp=getNode(c2->coeff,c2->exp);
c2=c2->next;
else
temp=getNode(c1->coeff,c1->exp);
c1=c1->next;
if(p3==NULL){
p3=temp;
prev=p3;
else
prev->next=temp;
prev=temp;
}
}
return p3;
node *c1=p1,*c2=p2,*p3=NULL,*temp,*prev=NULL;
if(p1==NULL&&p2==NULL)
return NULL;
if(p1==NULL)
return p2;
if(p2==NULL)
return p1;
while(c1!=NULL||c2!=NULL)
if(c1!=NULL&&c2!=NULL)
if(c1->exp==c2->exp)
temp=getNode(c1->coeff-c2->coeff,c1->exp);
c1=c1->next;
c2=c2->next;
else if(c1->exp>c2->exp)
{
temp=getNode(c1->coeff,c1->exp);
c1=c1->next;
else
temp=getNode(-c2->coeff,c2->exp);
c2=c2->next;
else if(c1==NULL)
temp=getNode(-c2->coeff,c2->exp);
c2=c2->next;
else
temp=getNode(c1->coeff,c1->exp);
c1=c1->next;
if(p3==NULL){
p3=temp;
prev=p3;
else
prev->next=temp;
prev=temp;
}
return p3;
node *c1,*c2,*temp,*p3=NULL,*prev=NULL,*c3;
if(p1==NULL&&p2==NULL)
return NULL;
if(p1==NULL)
return p2;
if(p2==NULL)
return p1;
c1=p1;
while(c1!=NULL)
c2=p2;
while(c2!=NULL)
temp=getNode(c1->coeff*c2->coeff,c1->exp+c2->exp);
if(p3==NULL)
p3=temp;
//otherwise
else
c3=p3;
//loop to append new node to proper position in resultant list
while(c3!=NULL)
if(c3->exp==temp->exp)
c3->coeff+=temp->coeff;
break;
else if(temp->exp>c3->exp){
temp->next=prev->next;
prev->next=temp;
break;
else
prev=c3;
c3=c3->next;
if(c3==NULL)
prev->next=temp;
c2=c2->next;
c1=c1->next;
return p3;
int main(){
int ch;
node *poly1=NULL,*poly2=NULL,*poly3=NULL;
do{
scanf("%d",&ch);
switch(ch){
case 1:
poly1=createPoly();
display(poly1);
break;
case 2:
poly2=createPoly();
display(poly2);
break;
case 3:
poly3=addPoly(poly1,poly2);
display(poly3);
break;
case 4:
poly3=subPoly(poly1,poly2);
display(poly3);
break;
case 5:
poly3=mulPoly(poly1,poly2);
display(poly3);
break;
}while(ch>0&&ch<6);
return 0;