You are on page 1of 26

/*

1. program to create singly linked list and perform various operations in it

*/

#include<stdio.h>

#include<stdlib.h>

typedef struct Node{

int data;

struct record *next;

}node;

//method to create linked list

node *create(){

node *temp,*head=NULL,*prev=NULL;

int ch;

//do-while loop to create a singly linked list

do{

//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=NULL;

//node insertion

if(head==NULL){

head=temp;

prev=head;

else{

prev->next=temp;
prev=temp;

//wanna repeat

printf("\nChoose 1 to continue,else 0:");

//fflush(stdin);

scanf("%d",&ch);

}while(ch==1);

return head;

//method to display the list

void display(node *head){

node *c=head;

//if list is empty

if(head==NULL){

printf("\nThe List is Empty...");

else{

printf("\nThe list is:");

//loop to print list elements

while(c){

printf("%d\t",c->data);

c=c->next;

//method to insert node at front

node *add_beg(node *head){

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);

//node insertion at beginning

temp->next=head;

head=temp;

return head;

//method to insert at last

node *add_end(node *head){

node *temp,*c;

//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=NULL;

//node insertion at end

c=head;

while(c->next){

c=c->next;

c->next=temp;

return head;

//method to add after kth node


void *add_after_k(node *head,int k){

node *c=head,*temp;

//loop to go to the position

while(--k&&c->next!=NULL)

c=c->next;

//if the position do exist

if(c->next!=NULL)

temp=(node*)malloc(sizeof(node));

if(temp==NULL)

printf("\nMemory allocation is not possible!!!");

else

printf("\nEnter the data:");

scanf("%d",&temp->data);

temp->next=c->next;

c->next=temp;

else

printf("\nWrong Position...");

//method to delete front node

node *del_beg(node *head){

node *temp;

if(head==NULL)

return NULL;

temp=head;

head=head->next;

free(temp);

return head;
}

//method to delete last node

node *del_end(node *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;

//method to add after a value

void *add_after_val(node *head,int k){

node *c=head,*temp;

while(c){

if(c->data==k){

break;

c=c->next;

if(c==NULL){

printf("\nValue not found...");

else{

temp=(node*)malloc(sizeof(node));

if(temp==NULL)

printf("\nMemmory allocation failed...");


else{

printf("\nEnter data:");

scanf("%d",&temp->data);

temp->next=c->next;

c->next=temp;

//method to add before kth node

void add_before_k(node *head,int k){

node *c=head,*temp;

--k;

//loop to go to the position

while(--k&&c->next!=NULL)

c=c->next;

//if the position do exist

if(c->next!=NULL)

temp=(node*)malloc(sizeof(node));

if(temp==NULL)

printf("\nMemory allocation is not possible!!!");

else

printf("\nEnter the data:");

scanf("%d",&temp->data);

temp->next=c->next;

c->next=temp;

else

printf("\nWrong Position...");
}

//method to add before a value

void add_before_val(node *head,int k){

node *c=head,*temp,*prev=NULL;

while(c){

if(c->data==k){

break;

prev=c;

c=c->next;

if(c==NULL){

printf("\nValue not found...");

else{

temp=(node*)malloc(sizeof(node));

if(temp==NULL)

printf("\nMemmory allocation failed...");

else{

printf("\nEnter data:");

scanf("%d",&temp->data);

temp->next=prev->next;

prev->next=temp;

//method to delete after kth node

void del_after_k(node *head,int k){

node *c=head,*temp;

//loop to go to the position

while(--k&&c->next!=NULL)
c=c->next;

//if the position do exist

if(c->next!=NULL)

temp=c->next;

c->next=temp->next;

free(temp);

else

printf("\nWrong Position...");

//method to delete before kth node

void del_before_k(node *head,int k){

node *c=head,*temp;

if(k==1){

printf("\nWrong Position...");

return;

if(k==2)

printf("\nCan be handled by another case...");

return;

k-=2;

//loop to go to the position

while(--k&&c->next!=NULL)

c=c->next;

//if the position do exist

if(c->next!=NULL)

temp=c->next;
c->next=temp->next;

free(temp);

else

printf("\nWrong Position...");

//method to delete kth node

void del_k(node *head,int k){

node *c=head,*temp;

if(k==1)

printf("\nCan be handled by another case...");

return;

--k;

//loop to go to the position

while(--k&&c->next!=NULL)

c=c->next;

//if the position do exist

if(c->next!=NULL)

temp=c->next;

c->next=temp->next;

free(temp);

else

printf("\nWrong Position...");

//reverse the linked list

node *reverse(node *head){

node *temp,*prev=NULL,*c=head;
//loop to reverse a linked list

while(c){

temp=c;

c=c->next;

temp->next=prev;

prev=temp;

printf("\nLinked list reversed...");

return prev;

//method to delete a specified value

void del_val(node *head,int val){

node *c,*temp,*prev=NULL;

//if the value is present at head node

if(head->data==val){

printf("\nUse head node delete case...");

return;

c=head;

//loop to search the value

while(c->next){

prev=c;

c=c->next;

if(c->data==val){

break;

//if value found,delete the node

if(c->next!=NULL){

temp=c;

prev->next=c->next;
free(temp);

//otherwise value not found

else{

printf("\nValue not found...");

//method to search a value

void search(node *head,int val){

node *c=head;

int index=0;

while(c){

if(c->data==val){

printf("Item found at index %d",index);

return;

index++;

c=c->next;

printf("\nItem not found...");

//merge two list by comaprison

node *merge(node *l1,node *l2){

node *dummy,*curr;

//memory allocation

dummy=(node*)malloc(sizeof(node));

if(dummy==NULL){

printf("\nMemory Allocation Failed...");

exit(0);

dummy->data=0;
dummy->next=NULL;

curr=dummy;

//traverse the lists to merge

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 list2 ended

if(l1){

curr->next=l1;

//if list1 ended

if(l2){

curr->next=l2;

return dummy->next;

//method to sort a linked list in O(nlogn) time

//split lists by mid node

node *sortList(node *head){

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);

//method to concatenate two singly linked lists

node *concat(node *h1,node *h2){

node *c=h1;

while(c->next){

c=c->next;

c->next=h2;

return h1;

//method to check whether two BSTs are equal or not

void chk_equal(node *h1,node *h2){

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){

printf("\nThe two lists are equal...");

else{

printf("\nThe two lists are different...");

int main(){

int ch,k;

node *temp,*head=NULL,*head1=NULL,*head2=NULL;

printf("\n1.Create singly linked list\n2.Display\n3.Insert at front\n4.Insert at end");

printf("\n5.Insert a node after kth node\n6.Insert a node after a value\n7.Insert a node


before kth node");

printf("\n8.Insert a node before a value\n9.Delete First Node\n10.Delete Last


Node\n11.Delete a node after kth node");

printf("\n12.Delete a node before kth node\n13.Delete kth node\n14.Delete a node


containing a specified value");

printf("\n15.Find Reverse\n16.Search a value\n17.Sort\n18.Merge two lists which are in


ascending order\n19.Concatenate two lists\n20.Find if two lists are equal");

do{

printf("\nEnter your choice:");

scanf("%d",&ch);

switch(ch){

//create singly linked list

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:

printf("\nCreate 1st linked list in ascending order\n");

head1=create();

printf("\nCreate 2nd linked list in ascending order\n");

head2=create();

printf("\nThe lists are:\n");

display(head1);

display(head2);

head1=merge(head1,head2);

printf("\nThe merged list is:\n");

display(head1);

break;

case 19:

printf("\nCreate 2nd list\n");

head1=create();

head=concat(head,head1);

break;

case 20:

printf("\nCreate 2nd list\n");

head1=create();

chk_equal(head,head1);

break;

}while(ch>=1&&ch<=20);

return 0;

}
/*

2. Write a menu-driven program representing a polynomial as a data structure using a single


linked list

and write functions to add, subtract and multiply two polynomials.

*/

#include<stdio.h>

#include<stdlib.h>

//node of storing each term of a polynomial

typedef struct record{

int coeff,exp;

struct record *next;

}node;

//method to create a node

node *getNode(int co,int exp){

node *temp=(node*)malloc(sizeof(node));

if(temp==NULL){

printf("\nMemory Allocation Failed...");

exit(0);

temp->coeff=co;

temp->exp=exp;

temp->next=NULL;

return temp;

//method to create a polynomial

node *createPoly(){

node *temp,*head=NULL,*prev=NULL;

int ch,co,exp;

//do-while loop to create a singly linked list

do{

printf("\nEnter coefficient and exponent:");


scanf("%d%d",&co,&exp);

temp=getNode(co,exp);

//node insertion

if(head==NULL){

head=temp;

prev=head;

else{

prev->next=temp;

prev=temp;

//wanna repeat

printf("\nChoose 1 to continue,else 0:");

scanf("%d",&ch);

}while(ch==1);

return head;

//method to display a polynomial

void display(node *head){

node *c=head;

//if list is empty

if(head==NULL){

printf("\nPolynomial has not built...");

else{

printf("\nThe Polynomial is:");

//loop to print list elements

while(c){

printf("(%d)x^(%d)",c->coeff,c->exp);

if(c->next)

printf("+");
c=c->next;

//method to add two polynomials

node *addPoly(node *p1,node *p2){

node *c1=p1,*c2=p2,*p3=NULL,*temp,*prev=NULL;

//if no polynomial is there

if(p1==NULL&&p2==NULL)

return NULL;

//if only one polynomial is there then return it

if(p1==NULL)

return p2;

if(p2==NULL)

return p1;

//loop to add two polynomials

while(c1!=NULL||c2!=NULL)

//both polynomials there

if(c1!=NULL&&c2!=NULL)

//same exponent,add coefficients

if(c1->exp==c2->exp)

temp=getNode(c1->coeff+c2->coeff,c1->exp);

c1=c1->next;

c2=c2->next;

//different exponents,append the node with higher exponent first

//and move respective pointer

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;

//only one polynomila is there

else if(c1==NULL)

temp=getNode(c2->coeff,c2->exp);

c2=c2->next;

else

temp=getNode(c1->coeff,c1->exp);

c1=c1->next;

//insert a new node in resultant list

if(p3==NULL){

p3=temp;

prev=p3;

else

prev->next=temp;

prev=temp;

}
}

return p3;

//method to subtract two polynomials

node *subPoly(node *p1,node *p2){

node *c1=p1,*c2=p2,*p3=NULL,*temp,*prev=NULL;

//if no polynomial is there

if(p1==NULL&&p2==NULL)

return NULL;

//if only one polynomial is there then return it

if(p1==NULL)

return p2;

if(p2==NULL)

return p1;

//loop to subtract two polynomials

while(c1!=NULL||c2!=NULL)

//both polynomials there

if(c1!=NULL&&c2!=NULL)

//same exponent,add coefficients

if(c1->exp==c2->exp)

temp=getNode(c1->coeff-c2->coeff,c1->exp);

c1=c1->next;

c2=c2->next;

//different exponents,append the node with higher exponent first

//and move respective pointer

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;

//only one polynomila is there

else if(c1==NULL)

temp=getNode(-c2->coeff,c2->exp);

c2=c2->next;

else

temp=getNode(c1->coeff,c1->exp);

c1=c1->next;

//insert a new node in resultant list

if(p3==NULL){

p3=temp;

prev=p3;

else

prev->next=temp;

prev=temp;

}
return p3;

//method to multiply two polynomials

node *mulPoly(node *p1,node *p2){

node *c1,*c2,*temp,*p3=NULL,*prev=NULL,*c3;

//if no polynomial is there

if(p1==NULL&&p2==NULL)

return NULL;

//if only one polynomial is there then return it

if(p1==NULL)

return p2;

if(p2==NULL)

return p1;

c1=p1;

//loop to traverse first polynomial

while(c1!=NULL)

c2=p2;

//loop to traverse second polynomial

while(c2!=NULL)

temp=getNode(c1->coeff*c2->coeff,c1->exp+c2->exp);

//if 3rd polynomial is not at all created

if(p3==NULL)

p3=temp;

//otherwise

else

c3=p3;
//loop to append new node to proper position in resultant list

while(c3!=NULL)

//same exponent,add coefficient

if(c3->exp==temp->exp)

c3->coeff+=temp->coeff;

break;

//lesser exponent,add node to prev of current node

else if(temp->exp>c3->exp){

temp->next=prev->next;

prev->next=temp;

break;

//otherwise keeping a track of prev node,move on

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;

printf("\n1.Create 1st Polynomial\n2.Create 2nd


Polynomial\n3.Add\n4.Subtract\n5.Multiply");

do{

printf("\nEnter your choice:");

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;

You might also like