0% found this document useful (0 votes)
19 views24 pages

Linked List - Module 3

The document outlines the syllabus for a Data Structures course, focusing on Linked Lists, including their definitions, types, operations, and applications. It details various operations such as insertion, deletion, and searching, along with code examples for singly and doubly linked lists. Additionally, it covers advanced topics like merging and reversing linked lists.

Uploaded by

ADITHYA KENI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views24 pages

Linked List - Module 3

The document outlines the syllabus for a Data Structures course, focusing on Linked Lists, including their definitions, types, operations, and applications. It details various operations such as insertion, deletion, and searching, along with code examples for singly and doubly linked lists. Additionally, it covers advanced topics like merging and reversing linked lists.

Uploaded by

ADITHYA KENI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DAYANANDA SAGAR COLLEGE OF ENGINEERING

(An Autonomous Institute Affiliated to VTU, Belagavi)

Course: Data Structures with Applications Course code: 21CS34


Semester: III

UNIT -3
Syllabus:
Linked Lists: Definition, Types of Linked List: Singly Linked List, Doubly Linked Lists &
Circular doubly linked list.

Operations on Linked List: Insert, Delete, Display, Concatenate, Search, Merge, Sort, and Reverse,
Stacks and Queues implementation.

Applications of Linked Lists: Adding Polynomials, addition of long positive integers using circular list.

A linked list is a linear collection of data elements, called nodes, each pointing to the next node
by means of a pointer. It is a data structure consisting of a group of nodes which together represent
a sequence.

Representation of structure

struct node
{
int info;
struct node *ptr;
};

While declaring the structure for a linked list

Declare a structure with two members is there i.e data member and
next pointer member. The data member can be character or integer or
depends upon the type of the information that the linked list is having. The link
member contains the address of next node.

Types of linked list


1. Singly linked list (SLL)
2. Circular singly linked list (CSLL)
3. Doubly linked list (DLL)
4. Circular doubly linked list (CDLL)

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 1
Operations on linked list

1. Insert to node at the end of linked list


2. Insert to node at the front of linked list
3. Insert to node at the specified position in the linked list
4. Delete the node from the end of linked list
5. Delete the node from the beginning of linked list
6. Delete the node at the specified position from the linked
list 7. Search the node in linked list
8. Implement a linked list based on stack principle
9. Implement a linked list based on queue principle
10. Count the number of nodes in the linked list.

Singly linked list (SLL)

It is called singly linked list only one link field to point to the next node. This is also
called linear list because the last elements points to nothing it is linear in nature. The
last node contains NULL which means that there is no node further or the last node in
the list.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>

struct node
{
int info;
struct node* link;
};
struct node *root=NULL;
int c;

void insfront()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the item\n");
scanf("%d",&temp->info);
temp->link=NULL;
if(root==NULL)
root=temp;
else
{
temp->link=root;

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 2
root=temp;
}
}
void insrear()
{
struct node* temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the item\n");
scanf("%d",&temp->info);
temp->link=NULL;
if(root==NULL)
root=temp;
else
{
p=root;
while(p->link!=NULL)
p=p->link;
p->link=temp;
}
}
void display()
{
struct node *temp;
if(root==NULL)
printf("list is empty\n");
else
{
temp=root;
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}

}
void del_front()
{
struct node *temp;
if(root==NULL)
printf("list is empty\n");
else
{
temp=root;
printf("item deleted is %d\n",temp->info);
root=temp->link;
temp->link=NULL;
free(temp);
}
}
void del_rear()
{
struct node *t,*p;
if(root==NULL)
printf("list is empty\n");
else if(root->link==NULL)
{ //first node
t=root;
printf("item deleted is %d\n",t->info);

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 3
root=NULL;
free(t);
}
else
{
t=root;
while(t->link!=NULL)
{
p=t; // p is positoning to previous node
t=t->link;
}
printf("item deleted is %d\n",t->info);
p->link=NULL;
free(t);
}
}
int length()
{
struct node *t;
int c;
if(root==NULL)
printf("No nodes\n");
else
{
c=1;
t=root;
while(t->link!=NULL)
{
c=c+1;
t=t->link;
}
//

}
return c;
}

void inspos()
{
struct node *temp,*p;
int i,loc;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the item\n");
scanf("%d",&temp->info);
temp->link=NULL;
printf("enter location to be inserted\n");
scanf("%d",&loc);
if(loc>length()||loc==0)
printf("invalid position\n");
else if(loc==1)
{
temp->link=root;
root=temp;
}
else
{
p=root;
for(i=1;i<loc-1;i++)

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 4
p=p->link;
temp->link=p->link;
p->link=temp;
}
}

void search()
{
int key,c=0;
struct node *t;
printf("Enter the Key to Search");
scanf("%d",&key);
if(root==NULL)
{
printf("List Empty");
}
else
{

t=root;
while(t!=NULL)
{
c++;
if(t->info==key)
{
printf("Key found at location %d",c);
getch();
exit(0);
}
t=t->link;
}
printf("Key not found");
}
}

void delkey()
{
struct node *p,*t;
int key;
printf("enter the key \n");
scanf("%d",&key);
if(root==NULL)
printf("list is empty");
else if(key==root->info)
{
t=root;
root=root->link;
free(t);
}
else
{
p=root;
while(p!=NULL)
{
if(key==p->info)
{
t->link = p->link;
p->link=NULL;

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 5
free(p);
}
else
{
t=p;
p=p->link;
}
}
}
}

Sort a List

#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *link;
};
typedef struct node NODE;
NODE *root=NULL;
void main()
{

int i;
int num ;
NODE *nw, *t1,*t2;
printf("\n Enter the number of node you want to create: ");
scanf("%d", &num );
// insert @ front
for (i = 0; i < num ; i++)
{
nw= (NODE*) malloc(sizeof(NODE));
printf("\n Enter the node: %d: ", i+1);
scanf("%d", &nw->data);
nw->link = NULL;
if(root==NULL)
root=nw;
else
{
nw->link=root;
root=nw;
}
}
t1=root;

while(t1!=NULL)
{
t2=t1->link;
while(t2!=NULL)
{
if(t1->data > t2->data)
{
int temp = t1->data;
t1->data = t2->data;
t2->data = temp;
}

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 6
t2=t2->link;
}
t1=t1->link;
}
nw =root;
printf("\n After sorting the Linked list is as follows:\n");
while (nw)
{
printf("%d\t", nw->data);
nw = nw->link;
}
}

Deleting based on position


#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *link;
};
typedef struct node NODE;
NODE *root=NULL;
void main()
{

int i;
int num ;
NODE *nw, *t1,*t2;
printf("\n Enter the number of node you want to create: ");
scanf("%d", &num );
// insert @ front
for (i = 0; i < num ; i++)
{
nw= (NODE*) malloc(sizeof(NODE));
printf("\n Enter the node: %d: ", i+1);
scanf("%d", &nw->data);
nw->link = NULL;
if(root==NULL)
root=nw;
else
{
nw->link=root;
root=nw;
}
}

NODE *temp1,*temp2;
int pos;
printf("enter the position to delete\n");
scanf("%d",&pos);
temp1=root;
if(pos==1)
{ printf("\nElement deleted is : %d\n", temp1->data);
root = root->link; // Advancing the head pointer
free(temp1); // Node is deleted
}

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 7
else
{
for (i = 1; i < pos - 1; i++)
temp1 = temp1->link;

temp2=temp1->link;

temp1->link = temp2->link;
printf("\nElement deleted is : %d\n", temp2->data);
free(temp2); // Node is deleted
}

nw=root;
while (nw)
{
printf("%d\t", nw->data);
nw = nw->link;
}
}
Merge two lists
#include<stdio.h>
#include<stdlib.h>

typedef struct node


{
int data;
struct node *next;
}node;

node *merge(node *p , node *q , node *sorting)


{
node *head3;

if(p == NULL)
return q;
if(q == NULL)
return p;

if(p && q)
{
if(p->data <= q->data)
{
sorting = p;
p = sorting->next;
}
else
{
sorting = q;
q = sorting->next;
}

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 8
head3 = sorting;

while(p && q)
{
if(p->data <= q->data)
{
sorting->next = p;
sorting = p;
p = sorting->next;
}
else
{
sorting->next = q;
sorting = q;
q = sorting->next;
}
}

if(p==NULL)
{
sorting->next = q;
}

if(q==NULL)
{
sorting->next = p;
}

return head3;
}

int main()
{
node *p=NULL , *q=NULL , *head1=NULL , *head2=NULL , *sorting = NULL, *t;
int n1 , n2 , a , i;

printf("Enter the number of nodes in the First Linked List");


scanf("%d",&n1);

printf("Enter the number of nodes in the second Linked List");


scanf("%d",&n2);

printf("Enter the first Linked List");


for(i=1;i<=n1;i++)
{
scanf("%d",&a);
p= (node*)malloc(sizeof(node));
p->data = a;
p->next = NULL;
if(head1==NULL)
head1=p;
else
{

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 9
t=head1;
while(t->next!=NULL)
t=t->next;

t->next = p;
}

printf("Enter the second Linked List");


for(i=1;i<=n2;i++)
{
scanf("%d",&a);
q= (node*)malloc(sizeof(node));
q->data = a;
q->next = NULL;
if(head2==NULL)
head2=q;
else
{
t=head2;
while(t->next!=NULL)
t=t->next;

t->next = q;
}

p = head1;
q = head2;
printf("\n First Linked List => ");
while(p!=NULL)
{
printf("%d ",p->data);
p = p->next;
}

printf("\n Second Linked List =>");


while(q!=NULL)
{
printf("%d ",q->data);
q = q->next;
}

p = head1;
q = head2;
sorting = merge(p , q , sorting);

printf("\n Sorted Merged Linked List =>");


while(sorting!=NULL)
{
printf("%d ",sorting->data);
sorting = sorting->next;

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 10
}
return 0;
}

Reverse a Linked List

void reverse()
{
struct node *curr, *prev=NULL,*next=NULL;
curr=root;
while(curr!=NULL)
{
next=curr->link;
curr->link=prev;
prev=curr;
curr=next;
}
root=prev;
}

Doubly Linked List


A Doubly Linked List (DLL) contains an extra pointer, typically called the previous pointer,
together with the next pointer and data which are there in the singly linked list.

Advantages of DLL over the singly linked list:


 A DLL can be traversed in both forward and backward directions.
 The delete operation in DLL is more efficient if a pointer to the node to be deleted is
given.
 We can quickly insert a new node before a given node.
 In a singly linked list, to delete a node, a pointer to the previous node is needed. To get this
previous node, sometimes the list is traversed. In DLL, we can get the previous node using
the previous pointer.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>

struct node
{
int info;
struct node* llink,*rlink;
};
struct node *root=NULL;

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 11
//int c;

void insfront()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the item\n");
scanf("%d",&temp->info);
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
root=temp;

else
{
temp->rlink=root;
root->llink=temp;
root=temp;
}
}
void insrear()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the item\n");
scanf("%d",&temp->info);
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
root=temp;
else
{
struct node *p=root;
while(p->rlink!=NULL)
p=p->rlink;

p->rlink=temp;
temp->llink=p;
}
}

void display()
{
struct node* temp;
if(root==NULL)
printf("list empty\n");
else
{
temp=root;
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->rlink;
}
}
}
int length()
{
struct node *temp=root;

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 12
int count=0;
while(temp!=NULL)
{
count++;
temp=temp->rlink;
}
return count;
}
void delfront()
{
struct node *temp;
if(root==NULL)
printf("List Empty\n");
else
{
temp=root;
if(temp->rlink==NULL)
{
root=NULL;
free(temp);
}
else
{
root=temp->rlink;
temp->rlink=NULL;
root->llink=NULL;
free(temp);
}
}
}
void delrear()
{
struct node *temp1,*temp2;
if(root==NULL)
printf("List Empty\n");
else
{
temp2=root;
if(temp2->rlink==NULL)
{
root=NULL;
free(temp2);
}
else
{
while(temp2->rlink!=NULL)
{
temp1=temp2;
temp2=temp2->rlink;
}

temp2->llink=NULL;
temp1->rlink=NULL;
free(temp2);
}
}
}

void main()

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 13
{
int ch;
clrscr();

for(;;)
{
printf("\n1.insert front\n2.insert rear\n3.display\n4.delete
front\n5.delete rear\n6.length of DLL\n");
printf("enter ur choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insfront();
break;

case 2: insrear();
break;
case 3: display();
break;
case 4: delfront();
break;
case 5:delrear();
break;
case 6: printf("\nlength=%d\n",length());
break;
default: exit(0);

}
}
}

Doubly Circular Linked List

Insertion in Circular Doubly Linked List:

1. Insertion at the end of the list or in an empty list:

A node(Say N) is inserted with data = 5. So, the previous pointer of N points to N and the next
pointer of N also points to N. But now start pointer points to the first node of the list.

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 14
Insertion in an empty list

2. List initially contains some nodes, start points to the first node of the List:
A node(Say M) is inserted with data = 7, so the previous pointer of M points to the last node,
the next pointer of M points to the first node and the last node’s next pointer points to this M
node, and first node’s previous pointer points to this M node.

Insertion at the end of list

4. Insertion in between the nodes of the list:


To insert a node in between the list, two data values are required one after which new node will
be inserted and another is the data of the new node.

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 15
Advantages of circular doubly linked list:
 The list can be traversed from both directions i.e. from head to tail or from tail to head.
 Jumping from head to tail or from tail to head is done in constant time O(1).
 Circular Doubly Linked Lists are used for the implementation of advanced data structures
like the Fibonacci Heap.

#include<stdio.h>
#include<stdlib.h>

struct node
{
int info;
struct node* llink,*rlink;
};
struct node *root=NULL;
void insfront()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the item\n");
scanf("%d",&temp->info);
if(root==NULL)
{
temp->llink=temp->rlink=temp;
root=temp;
}
else
{
temp->rlink=root;
temp->llink=root->llink;
root->llink->rlink=temp;
root->llink=temp;
root=temp;
}
}

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 16
void insrear()
{
struct node *temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the item\n");
scanf("%d",&temp->info);
if(root==NULL)
{
temp->llink=temp->rlink=temp;
root=temp;
}
else
{
p=root->llink;
p->rlink=temp;
temp->llink=p;
root->llink=temp;
temp->rlink=root;
}
}
void display()
{
struct node * temp;
if(root==NULL)
printf("list empty\n");
else
{
temp=root;
do
{
printf("%d\t",temp->info);
temp=temp->rlink;
}while(temp!=root);
}

}
void delfront()
{
struct node *cur;
if(root==NULL)
printf("List empty\n");
else if(root->rlink==root)
{
printf("item deleted is %d\n",root->info);
free(root);
root=NULL;
}
else
{
cur=root;
root=root->rlink;
cur->llink->rlink=root;
root->llink=cur->llink;
printf("item deleted is %d\n",cur->info);
free(cur);
}
}
void delrear()
{

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 17
struct node *cur;
if(root==NULL)
printf("List empty\n");
else if(root->rlink==root)
{
printf("item deleted is %d\n",root->info);
free(root);
root=NULL;
}
else
{
cur=root->llink;
cur->llink->rlink=cur->rlink;
root->llink=cur->llink;
// root->llink=cur->llink;
printf("item deleted is %d\n",cur->info);
free(cur);
}
}
void delkey()
{
struct node *temp;;
int key,flag=0;
if(root==NULL)
printf("List empty\n");
else
{
printf("enter the key to delete\n");
scanf("%d",&key);
temp=root;
do
{
if(temp->info==key)
{
// if first node root to be changed to next
node
if(temp==root)
{
printf("%d is deleted\n",temp->info);
root=temp->rlink;
temp->llink->rlink=temp->rlink;
temp->rlink->llink=temp->llink;
free(temp);
flag=1;
break;
}
//for remaining case inbetween and
else
{
printf("%d is deleted\n",temp->info);
temp->llink->rlink=temp->rlink;
temp->rlink->llink=temp->llink;
free(temp);
flag=1;
break;
}
}
temp=temp->rlink;
}while(temp!=root);

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 18
if(flag==0)
printf("key not found\n");
}

}
int main()
{
int ch;
for(;;)
{
printf("\n1.insert front\n2.insrear\n3.delfront\n 4. delrear\n
5.delkey\n 6. display");
printf("enter ur choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insfront();
break;
case 2: insrear();
break;
case 6: display();
break;
case 4:delrear();break;
case 5: delkey();break;
case 3: delfront();
break;
default: exit(0);

}
}
}

Applications
1. Addition of two long positive integers

#include <stdio.h>
#include <stdlib.h>

/* Linked list node */


struct Node
{
int data;
struct Node* link;
};
typedef struct Node NODE;

NODE *first=NULL;
NODE *second=NULL;
NODE *res=NULL;

NODE* newNode(int data)


{
NODE* new_node = (NODE *)malloc(sizeof(NODE));
new_node->data = data;
new_node->link = NULL;
return new_node;
}

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 19
/* Function to insert a node at the
beginning of the Singly Linked List */

void insfront(NODE**head_ref, int new_data)


{
/* allocate node */
NODE* new_node = newNode(new_data);
/* link the old list off the new node */
new_node->link = *head_ref;
/* move the head to point to the new node */
*head_ref = new_node;
}

/* Adds contents of two linked lists and


return the head node of resultant list */
NODE* addTwoLists(NODE* first, NODE* second)
{
// res is head node of the resultant list
NODE* res = NULL;
NODE *temp;
int carry = 0, sum;

// while both lists exist


while (first != NULL || second != NULL)
{
sum = carry + (first ? first->data : 0)+ (second ? second->data : 0);
// update carry for next calculation
carry = (sum >= 10) ? 1 : 0;
// update sum if it is greater than 10
sum = sum % 10;
// Create a new node with sum as data
temp = newNode(sum);
// if this is the first node then set it as head of the resultant list
if (res == NULL)
res = temp;
// If this is not the first node then connect it to the rest.
else
{
temp->link = res;
// Set prev for next insertion
//prev = temp;
res=temp;
}

// Move first and second pointers to next nodes


if (first)
first = first->link;
if (second)
second = second->link;
}
if (carry > 0)
{
temp= newNode(carry);
temp->link = res;
res=temp;
}
// return head of the resultant list
return res;
}

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 20
// A utility function to print a linked list
void printList(NODE* node)
{
while (node != NULL) {
printf("%d ",node->data);
node = node->link;
}
printf("\n");
}

/* Driver code */
int main(void)
{

// create first list 7->5->9->4->6


insfront(&first, 6);
insfront(&first, 4);
insfront(&first, 9);
insfront(&first, 5);
insfront(&first, 7);
printf("First list is ");
printList(first);

// create second list


insfront(&second, 7);
insfront(&second, 2);
insfront(&second, 1);
insfront(&second, 4);
insfront(&second, 8);
// insfront(&second, 8);
printf("Second list is ");
printList(second);

res = addTwoLists(first, second);

printf("Resultant list is ");


printList(res);
return 0;
}

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 21
2. Addition of Polynomials
#include<stdio.h>
#include<stdlib.h>
struct node
{
int co,exp;
struct node *next;
};
struct node *create(struct node*head, int co, int exp)
{
struct node *temp,*flag;
if(head==NULL)
{
temp=(struct node*) malloc(sizeof(struct node));
temp->co=co;
temp->exp=exp;
temp->next=NULL;
head=temp;
}
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
flag=(struct node*) malloc(sizeof(struct node));
flag->co=co;
flag->exp=exp;
flag->next=NULL;
temp->next=flag;
}
return head;
}

struct node* polyAdd(struct node* p1, struct node *p2, struct node *sum)
{
struct node *poly1=p1, *poly2=p2, *res;
if(poly1!=NULL && poly2==NULL)
{
sum=poly1;
return sum;
}
else if(poly1==NULL && poly2!=NULL)
{
sum=poly2;
return sum;
}
while(poly1!=NULL && poly2!=NULL)
{
if(sum==NULL)
{

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 22
sum=(struct node*)malloc(sizeof(struct node));
res=sum;
}
else
{
res->next=(struct node*)malloc(sizeof(struct node));
res=res->next;
}
if(poly1->exp>poly2->exp)
{
res->co=poly1->co;
res->exp=poly1->exp;
poly1=poly1->next;

}
else if(poly1->exp<poly2->exp)
{
res->co=poly2->co;
res->exp=poly2->exp;
poly2=poly2->next;
}
else if(poly1->exp==poly2->exp)
{
res->co=poly1->co+poly2->co;
res->exp=poly1->exp;
poly1=poly1->next;
poly2=poly2->next;
}
}
while(poly1!=NULL)
{
res->next=(struct node*)malloc(sizeof(struct node));
res=res->next;
res->co=poly1->co;
res->exp=poly1->exp;
poly1=poly1->next;
}
while(poly2!=NULL)
{
res->next=(struct node*)malloc(sizeof(struct node));
res=res->next;
res->co=poly2->co;
res->exp=poly2->exp;
poly2=poly2->next;
}

res->next=NULL;
return sum;

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 23
void display(struct node*head)
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%dx^%d+", temp->co, temp->exp);
temp=temp->next;
}
printf("\n");
}

int main()
{
struct node *p1=NULL,*p2=NULL, *sum=NULL;
int ch, co, exp;
while(1)
{
printf("1. Add to polynomial 1 2. Add to polynomial 2 3. Perform addition 4. Exit
\n");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("Enter the coefficient\n");
scanf("%d", &co);
printf("Enter the exponent\n");
scanf("%d", &exp);
p1=create(p1, co, exp);

break;
case 2: printf("Enter the coefficient\n");
scanf("%d", &co);
printf("Enter the exponent\n");
scanf("%d", &exp);
p2=create(p2, co, exp);
break;
case 3: sum=polyAdd(p1,p2,sum);
printf("Polynomial 1\n");
display(p1);
printf("Polynomial 2\n");
display(p2);
printf("Sum\n");
display(sum);
break;
case 4: exit(0);
break;
default: printf("Enter another valid choice\n");
break;
}
}
}

Dr. Harish Kumar N, Assistant Professor, Dept. of CSE, DSCE, Bangalore -560111 24

You might also like