0% found this document useful (0 votes)
46 views

Chapter 5

The document discusses operations on linked lists, including: 1. Traversing a linked list by visiting each node. 2. Inserting nodes at the beginning, end, or middle of a linked list. 3. Deleting nodes from the beginning, end, or arbitrary positions in a linked list. 4. Searching for a specific node in a linked list. It provides examples of implementing these operations using C code on singly linked lists. The operations allow linked lists to be manipulated and used to store and organize data.

Uploaded by

shjdje
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Chapter 5

The document discusses operations on linked lists, including: 1. Traversing a linked list by visiting each node. 2. Inserting nodes at the beginning, end, or middle of a linked list. 3. Deleting nodes from the beginning, end, or arbitrary positions in a linked list. 4. Searching for a specific node in a linked list. It provides examples of implementing these operations using C code on singly linked lists. The operations allow linked lists to be manipulated and used to store and organize data.

Uploaded by

shjdje
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter 5: Linked List -II

Learning Outcomes:
 Students will be able to understand linked list application.
 Students will have a clarity operation on linked list.
 Students will be able to learn generalized linked list.
Structure
5.1. Operations on Linked List
 Knowledge Check 1
 Outcome Based Activity
5.2. Applications of Linked List
5.3. Generalized linked list
 Knowledge Check 2
 Outcome Based Activity
5.4. Summary
5.5. Self-Assessment Questions
5.6. References
5.1. Operations on Linked List
The various operation that can be performed on linked list are given below.
1. Create: create a list of a node.
2. Traverse: Visit each node of the list.
3. Length: Count the total number of node in the list.
4. Insert: A node can be inserted at the beginning, end or in between two node of the
list.
5. Delete: Deletion from the list may be done either position-wise or element-wise.
6. Search: This process searches for a specific elements in the list.
7. Reverse: This process reverse the order of node in the list.
8. Concatenation: This operation appends the node of the second list at the end of the
first list i.e. it join two list.
9. Merge: This operation merge two sorted linked list into the third list such that the
third list also sorted order.
The above operation on the list an extra node at the beginning allows operation to be
performed more efficiently. This is called as header node.
Header Node
A header node is an extra node that is added at the beginning of the list. This node
does not store any data element but can be used to store some control information like the
number of element etc.
The use of header node simplifies list operation.

Creating node:
All linked list operation are performed using a pointer. The syntax to declared a pointer to
a node is.
Syntax:
struct node *pointer;
Example:
struct node *head, *temp, *newnode;
To create a node dynamically during run-time we use functions like malloc(), calloc(). To
free the node, use function free().
Singly Linked list

Doubly linked list

5.1.1. Singly Linked List:


1) Creating a Singly Linked List:
A node of a linked list is a structure because it contains data of different types. In addition to
the information part, it contains a pointer that can point to a node i.e. to itself or to some other
node.
a) Structure of the node for singly linked list:
struct node
{
int value; // contains data item
struct node *next; // pointer to node
};
struct node node1; //node1 of type node gets created
int value, *front; // value 2 integer variable and front points to integer
front =&value ; /* initializes the value of the front pointer variable with the
address of the variable 'value’ in the memory */
b) Steps for Creating a Singly Linked List:
i) To initialize or assign value:
value=100; or front=100;
ii) Reference:
The reference to the list being created is done by
node1 *front; // pointer to the node of the linked list
iii) Memory Allocation for the Node:
To define the node that stores the first data item, use dynamic memory allocation for the node
storing front as below:
front=(node1*)malloc(size of(node1));

iv) Assigning Values to Fields:


Having created model, assign data in value field as;
front->data=100; // value field contains 100
front->next=‘\0’; /* next of node1 is NULL since it is not pointing or assigned
anywhere */

2) Insertion of a Node into in a Singly Linked List:


a) Inserting a Node at the beginning of the List:
Let the linked list be pointed by the front pointer. Let information to be inserted be val1.
void insert_begin(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=start;
start=tmp;
}
b) Inserting Node at the End of Linked List:
Let the linked list be pointed by front. Let information to be inserted in value field be 30. Let
another pointer ‘rear’ point to where front points.

void insert_end(int num,int end)


{
int i;
q=start;
for(i=0;i<end-1;i++)
{
q=q->addr_next;
if(q==NULL)
{
printf("Invalid Position");
return;
}
}

tmp=malloc(sizeof(struct node) );
tmp->addr_next=q->addr_next;
tmp->data=num;
q->addr_next=tmp;
}

c) Inserting a Node at Specific Position in a Singly Linked List:


Consider a linked list containing 3 nodes as shown in Figure.

Let the node to be inserted is temp as shown in below figure 4.8 (d) after the 2nd node in the
list. This means there should be some form of counter to count the number of nodes so as to
reach at the desired position

Node to be Appended

Original Linked List with a New Pointer


The node to be inserted must appear after the second node. Now; the next pointer of the node
30 should be made point to node 40. The next pointer of the node, where temp is currently
positioned must be made to point, where pointer temp points. This is done by the following
two lines of code.
temp->next=locptr->next;
locptr->next = temp;

Temp points to the node 30 that is to be appended. This is shown in below Figure
Linked list after appending a node
void insert_middle(int num,int mid)
{
int i;
q=start;
for(i=0;i<mid-2;i++)
{
q=q->addr_next;
if(q==NULL)
{
printf("Invalid Position");
return;
}
}

tmp=malloc(sizeof(struct node) );
tmp->addr_next=q->addr_next;
tmp->data=num;
q->addr_next=tmp;
}

3) Traversing Singly Linked List:


Traversing linked list means visiting each and every node of the singly linked list. For
traversing the singly linked list firstly move to the first node, fetch the data from the node and
perform the operations such as arithmetic operation or any operation depending on data type.
void display()
{
if(start == NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=start; //point q at start
while(q!=NULL) //go to end of linked list
{
printf("| %d | %d | --> ", q->data,q->addr_next);
q=q->addr_next;
}
printf("\n\n");
}
}

4) Deleting Nodes from a Singly Linked List:


a) Deleting First Node from the Singly Linked List:
Deleting the first node from the linked list requires that a pointer should point to the start of
the linked list i.e. starting address of list is permanently changed. Assign a new pointer
‘delptr' that points, where front points. The purpose of doing this is to preserve the reference
to the node being deleted as the memory occupied by the node need be deleted. This is shown
in Figure

Now, advance front pointer to point to the next node so that after deleting the first node, the
node that the front points now will be the first node of the list. This is illustrated in below
Figure

Original Linked List with Front Pointing to Second Node


There are two lists; the first is the original list with one node deleted. The second is the list
that came into existence because of the deletion operation. This is pointed by delptr. Now this
node should free up the memory occupied. This scenario is illustrated in Figure.

List with One Node Deleted


b) Deleting the Last Node of the Singly Linked List:
Deleting the last node from the linked list requires having another pointer with the help of
which one can traverse the list and also check for the node whose next field is NULL. This
node is the one at the end of the list that is to be deleted. Once the last node is deleted, a
reference to the last node of the new list which is 20 now is used to set the next field of this
node to NULL. Hence an extra pointer called last is needed. This is illustrated in Figure.

Original List from which Last Node to be deleted

Modified list and deleted node.


c) Deleting any arbitrary node from any specified position of the given Singly Linked
List:
Deleting a node from any specified position in the list requires traversing the list till the
specified
position and then setting the respective pointers. Let the third node of the list has to be
deleted
from the list shown in Figure:

Use a variable count to store the count of the number of node traversed and a pointer ‘delptr’.
When count less than or equal to the position (third), the delptr-> next should point where the
next pointer of the node to be deleted is pointing. Let ‘nodeptr’ be the pointer pointing to this
node as shown in Figure

5. Search Nodes from a Singly Linked List


To search an element in the list, we have to traverse the entire list and compare the
data in each node. If found the function returns the position of the node and -1 otherwise. To
find the position of the node, we need an additional integer which is incremented as we move
along the node.

6. Sort Nodes from a Singly Linked List


This operation sort the element of a singly linked list. A sorted list helps in faster search
operation.

7. Reverse Nodes from a Singly Linked List


This operation reverse all the nodes of the linked list such that the first node become the
last and vice versa. The reverse operation reverse the links of each node.
8. Merge Nodes from a Singly Linked List
Merging is the process of combining two lists containing elements in the sorted order into
a third such that the third list is also sorted.
9. Concatenation Nodes from a Singly Linked List
This operation join two list. The node of the second list are append at the end of the first
list.

Program for Singly linked list:


#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define NEWNODE (struct node *)malloc(sizeof(struct node))
struct node
{
char data;
struct node *next;
};
void display(struct node *f)
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
printf(" %c ",s->data);
}
}
struct node * insert(struct node *f,char x,int pos)
{
struct node* s,*t;
int i;
t=NEWNODE;
t->data=x;
t->next=NULL;
if(pos==1)
{
t->next=f;
f=t;
return f;
}
else
{
s=f;
for(i=1;i<=pos-2 && s!=NULL;i++)
{
s=s->next;
}
if(s==NULL)
{
printf("\nERROR : INVALID POSITION");
return f;
}
t->next=s->next;
s->next=t;
return f;
}
}
struct node * delete(struct node *f,char x)
{
struct node *s,*t;
int i;
if(f==NULL)
{
printf("Linked List is Empty....!!!\n");
return f;
}

while(f!=NULL)
{
if(f->data==x)
{
t=f;
f=f->next;
free(t);
}
else
{
break;
}
}
if(f==NULL)
return f;
for(s=f;s!=NULL&&s->next!=NULL;s=s->next)
{
if(s->next->data==x)
{
t=s->next;
s->next=t->next;
free(t);
}
}
return f;
}
struct node * search(struct node *f,char x)
{
struct node *s;

for(s=f;s!=NULL;s=s->next)
{
if(s->data==x)
return s;

}
return NULL;
}
int main()
{
struct node *head=NULL;
int choice,pos,n;
char x;
while(1)
{
printf("\nMENU\n\n1:DISPLAY SINGLY LINKED LIST\n2:INSERTING
NODE AT GIVEN POSITION OF LINKED LIST\n3:DELETING NODE
FROM GIVEN POSITION OF THE LINKED LIST\n4:SEARCH\n5:EXIT");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nLINKED LIST = ");
display(head);
break;
case 2: printf("\nENTER POSITION FOR INSERTION : ");
scanf("%d",&pos);
printf("\nENTER THE CHARACTER : ");
scanf("%c",&x);
head=insert(head,x,pos);
break;
case 3: printf("\nENTER CHARACTER FOR DELETION : ");
scanf("%c",&x);
head=delete(head,x);
break;
case 4: printf("\nENTER DATA FOR SEARCH : ");
scanf("%c",&x);
if(search(head,x)==NULL)
{
printf("Given character %c is not Found....\n",x);
}
else
{
printf("Given Character %c is Found....\n",x);
}
break;
case 5: exit(0);
default:printf("\nINVALID CHOICE,Please RE-ENTER AGAIN");
}//switch
}//while
}//main
Output:
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 2
ENTER POSITION FOR INSERTION : 1
ENTER THE CHARACTER : 3
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 1
LINKED LIST = 3
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 3
ENTER CHARACTER FOR DELETION : 1
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 4
ENTER DATA FOR SEARCH : 5
Given character 5 is not Found....

5.1.2. Doubly Linked List


In a singly linked list each node contains only one pointer to the next node, thus traversal is
possible only in one direction. A doubly linked list is a linked list in which each node
contains two links- one pointing to the previous node and one pointing to the next node.

Advantages:
1. Traversal in both direction is possible.
2. Searching an element is faster.
3. Operation like insertion and deletion can be performed easily.
Disadvantages:
1. Extra storage is needed for the pointer.
a) Creating a Doubly Linked List:
The node of a doubly linked list is a structure with fields; data which stores the value of
the node, *prev which stores the address of the previous node and *next stores the address of
the next node. Two nodes *front which always points to the first node of the linked list and
*rear which is used to point to the last node of the linked list are initialized. Initially temp-
>prev = NULL and temp->next = NULL.
void create(int num)
{
tmp=malloc(sizeof(struct node));
tmp->data=num;
tmp->addr_next=NULL;
if(start==NULL) //check if 1st element
{
tmp->addr_prev=NULL;//assign null for previous address as it the first
element
start->addr_prev=tmp;
start=tmp; //point start at tmp
}
else //not 1st element
{
q=start; //point q at start
while(q->addr_next!=NULL) //go to end of linked list
{
q=q->addr_next;
}
q->addr_next=tmp; //store address
tmp->addr_prev=q;
}
}

b) Inserting a Node at the beginning of Doubly Linked List:


Front and rear pointers are assigned for easier navigation of list. Now assign a pointer
‘temp’ to it and store some data in value field. Let the next pointer of this node point, where
front initially points.

newnode->next=dlist;
dlist->prev=newnode;
dlist=newnode;

c) Inserting a Node at the end of Doubly Linked List:


temp->=newnode;
newnode->prev=temp;
d) Displaying a Doubly Linked List:
In order to display a doubly linked list create a linked list .Then traverse through the linked
list. In order to traverse from start to end assign Address of starting node in pointer variable
i.e. temp.
void display()
{
if(start == NULL) //linked list is empty
{
printf("List is empty\n");
}
else
{
q=start; //point q at start
while(q!=NULL) //go to end of linked list
{
printf("| %d | %d | %d | --> ",q->addr_prev,q->data,q->addr_next);
q=q->addr_next;
}
printf("\n\n");
}
}

Program for Doubly Linked List

doubly.c
#include<stdio.h>
#include "doublylist.h"
int main()
{
struct node *head=NULL;
int choice,pos,n,x;
while(1)
{
// system("clear");// this is standered liabrary function which clears the screen.
printf("\nMENU\n\n1:DISPLAY SINGLY LINKED LIST\n2:INSERTING NODE AT
GIVEN POSITION OF LINKED LIST\n3:DELETING NODE FROM GIVEN POSITION
OF THE LINKED LIST\n4:SEARCH\n5:EXIT");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nLINKED LIST = ");
display(head);
break;
case 2: printf("\nENTER POSITION FOR INSERTION : ");
scanf("%d",&pos);
printf("\nENTER THE DATA : ");
scanf("%d",&x);
head=insert(head,x,pos);
break;
case 3: printf("\nENTER DATA FOR DELETION : ");
scanf("%d",&x);
head=delete(head,x);
break;
case 4: printf("\nENTER DATA FOR SEARCH : ");
scanf("%d",&x);
if(search(head,x)==NULL)
{
printf("Given Number %d is not Found....\n",x);
}
else
{
printf("Given Number %d is Found....\n",x);
}
break;
case 5: exit(0);
default:printf("\nINVALID CHOICE,Please RE-ENTER AGAIN");
}//switch
}//while
}//main
doublylist.h
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define NEWNODE (struct node *)malloc(sizeof(struct node))
struct node
{
struct node *prev;
int data;
struct node *next;
};
void display(struct node *f)
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
printf(" %d ",s->data);
}

}
struct node * insert(struct node *f,int x,int pos)
{
struct node* s,*t;
int i;
t=NEWNODE;
t->data=x;
t->next=NULL;
t->prev=NULL;
if(pos==1)
{
t->next=f;
if(f!=NULL)
f->prev=t;
f=t;
return f;
}
else
{
s=f;
for(i=1;i<=pos-2 && s!=NULL;i++)
{
s=s->next;
}
if(s==NULL)
{
printf("\nERROR : INVALID POSITION");
return f;
}
t->next=s->next;
if(s->next!=NULL)
s->next->prev=t;
t->prev=s;
s->next=t;
return f;
}
}
struct node * delete(struct node *f,int x)
{
struct node *s,*t;
int i;
if(f==NULL)
{
printf("Linked List is Empty....!!!\n");
return f;
}
while(f!=NULL)
{
if(f->data==x)
{
t=f;
f=f->next;
free(t);
}
else
{
break;
}
}
if(f==NULL)
return f;
f->prev=NULL;
for(s=f;s!=NULL && s->next!=NULL;s=s->next)
{
if(s->next->data==x)
{
t=s->next;
s->next=t->next;
free(t);
}

}
return f;
}
struct node * search(struct node *f,int x)
{
struct node *s;
for(s=f;s!=NULL;s=s->next)
{
if(s->data==x)
return s;
}
return NULL;
}
Output:
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 2
ENTER POSITION FOR INSERTION : 1
ENTER THE DATA : 23
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 3
ENTER DATA FOR DELETION : 2
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 4
ENTER DATA FOR SEARCH : 23
Given Number 23 is Found....
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 1
LINKED LIST = 23
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 3
ENTER DATA FOR DELETION : 1
MENU
1:DISPLAY SINGLY LINKED LIST
2:INSERTING NODE AT GIVEN POSITION OF LINKED LIST
3:DELETING NODE FROM GIVEN POSITION OF THE LINKED LIST
4:SEARCH
5:EXIT
ENTER YOUR CHOICE : 5

5.1.3. Circularly Linked List:


A circular list is one in which the next field of the last node points to the first node of the list
or the next field of the last node contains the address of the first node of the list.

The two pointers front and rear can be used to associate with the first and last node
respectively.

Adding Reference Pointers to a Circularly Linked List

A) Types of Circularly Linked List:


The Circularly linked list is divided into two types:
a) Circularly Singly Linked List:
In circular linked list the last node points to the header node. The singly linked list can be
converted to circular linked list by linking the last node to the first node.

3.3.3.1 Operations on Circularly Singly Linked List:


a) Creating a Circularly Singly Linked List:
To create a new node in a circularly linked list associates the front and the rear pointers to the
newly created node.
Structure of node of a circularly singly linked list:
struct node
{
int value;
struct node *next;
};
typedef struct node node1;
node1 *front, *rear; // declaring pointers

B) Steps for creating circularly singly linked list :


a) Memory allocation for the first node:
node1 *firstnode;
firstnode=(node1*)malloc(sizeof(node1));
b) To associate the front and the rear pointers to firstnode : .
front=firstnode ;
rear=firstnode;
c) To insert data in value field of the node :
firstnode->value=val1;
d) Second node creation:
node1 *secondnode;
secondnode= (node1*) malloc ( size of(node1) );
secondnode->value=val2;
The rear pointer which is now pointing to the first node should point to second node.
rear=rear->next;
b) Insert Node at the beginning of the Circular Singly Linked List:
After the creating the first node in a circular Singly Linked List, nodes can be inserted at
the beginning can be done, if the list is empty, it is first created.

c) Displaying a Circularly Singly Linked List :


To display a circularly singly linked list, create a pointer ‘locptr’ to traverse the list.
Traversing takes place from the front to the end of the list.

3.3.3.2 Circularly Doubly Linked List:


A circularly double linked list is one in which the next field of last node points the first
node and the previous field of the first node points the last node.

For convenient traversal of the list and to simplify the insertion and deletion of nodes, a
header node can be placed at the front of the list.

Representation of circularly doubly list with header node

1) Operations on Circularly Doubly List:


The following operations can be performed on circularly doubly linked list:
a) Creating a Circularly Doubly Linked List:
For creating a circularly doubly linked list, assign the previous and next fields of newly
created node to NULL.
Structure for Node of Circularly Doubly List:
struct node
{
int value;
struct node *front;
struct node *back;
};
struct node *temp;
struct node *head;
b) Displaying a Circularly Doubly Linked List:
In circularly double linked list the right side pointer points to the next node address or the
address of first node and left side pointer points to the previous node address or the address of
last node of a list, so circularly doubly linked list can be displayed using both the pointers.
Singly Linked Doubly Linked Singly circular Doubly circular
List List Linked List Linked List
Create O(n) O(n) O(n) O(n)
Traverse O(n) O(n) O(n) O(n)
Search O(n) O(n) O(n) O(n)
Insert at O(1) O(1) O(1) O(1)
beginning
Insert at end O(n) O(n) O(n) O(1)
Delete at O(1) O(1) O(1) O(1)
beginning
Delete at end O(n) O(n) O(n) O(1)

 Knowledge Check 1
1. In linked list each node contains a minimum of two fields. One field is data field to
store the data second field is…………
2. Differentiates a circular linked list from a normal linked list…………
3. ………….. list does insert operation at end take O(1) time.
4. ………. Linked list the last node points to the first.
 Outcome Based Activity
1. Write a program to count the number of nodes in a circular doubly linked list.

5.2. Applications of Linked List


There are several application which use a linked list. There are
Step1: In operating system for dynamic memory management.
Step 2: Representation of polynomials.
Step 3: In dynamic implementation of data structure like stack, queue, tree, graph etc.
Step 4: Performing operation on long integer.
Step 5: Representation of sparse matrices.
Step 6: In linked hash tables and dictionaries.
Polynomial Representation:
Polynomial is a one variable can be represented as a linked list where each node of the
list represent one term of the polynomial.
Example:
Each node contains:
i. Coefficient
ii. Exponent
iii. Pointer to the next term
typedef struct node
{
int coef, exp;
struct node *next;
}POLY;

Addition of two polynomials


The term of a polynomial can be added if their power are the same.
Example:

Algorithm for addition of two polynomial,


Step 1: start
Step 2: accept polynomial 1.
Step 3: accept polynomial 2.
Step 4: t1, t2 and t3 are temporary pointer used for traversal.
Step 5: set t1=p1next, t2=p2next and t3=p3. (p3 is the resultant polynomial)
Step 6: while t1≠NULL and t2≠ NULL
Create newnode
If(t1exp = = t2exp)
Create newnode with exponent and coefficient of t1
Move t1 to the next node
else
create newnode with exponent and coefficient of t2
Move t2 to the next node
Attach newnode t3.
Move t3 to the next node.
Step 7: While t1≠NULL
Copy remaining node from t1 to t2.
Step 8: While t2≠NULL
Copy remaining node from t2 to t3.
Step 9: Stop.

Program for Addition of two Polynomial.


#include<stdio.h>
#include<stdlib.h>
struct poly
{
int pow,coeff;
struct poly *next;
};
int i,j;
struct poly *head,*head1,*h;
struct poly *create(int n)
{
struct poly *newnode,*first,*q;
newnode=(struct poly *)malloc(sizeof(struct poly));
printf("Enter the coeff & power:");
scanf("%d%d",&newnode->coeff,&newnode->pow);
first=newnode;
newnode->next=NULL;
q=first;
for(i=1;i<n;i++)
{
newnode=(struct poly *)malloc(sizeof(struct poly));
q->next=newnode;
q=q->next;
printf("Enter the coeff & power:");
scanf("%d%d",&q->coeff,&q->pow);
}
q->next=NULL;
return(first);
}
void display(struct poly *head)
{
do
{
printf("%dx^%d+",head->coeff,head->pow);
head=head->next;
}while(head!=NULL);
printf("\n");
}

struct poly *addpoly(struct poly *head,struct poly *head1)


{
struct poly *p,*q,*newnode,*s,*f;
newnode=(struct poly *)malloc(sizeof(struct poly));
newnode->next=NULL;
f=newnode;
p=head;
q=head1;
while(p!=NULL&& q!=NULL)
{
if(p->pow>q->pow)
{
s=(struct poly *)malloc(sizeof(struct poly));
f->next=s;
s->pow=p->pow;
s->coeff=p->coeff;
p=p->next;
f=f->next;
}
else
{
if(p->pow<q->pow)
{
s=(struct poly *)malloc(sizeof(struct poly));
f->next=s;
s->pow=q->pow;
s->coeff=q->coeff;
q=q->next;
f=f->next;
}
else
{
s=(struct poly *)malloc(sizeof(struct poly));
f->next=s;
s->pow=q->pow;
s->coeff=q->coeff+p->coeff;
q=q->next;
p=p->next;
f=f->next;
}
}
}

if(p!=NULL)
{
while(p!=NULL)
{
s=(struct poly *)malloc(sizeof(struct poly));
f->next=s;
s->pow=p->pow;
s->coeff=p->coeff;
p=p->next;
f=f->next;
}
}

if(q!=NULL)
{
while(q!=NULL)
{
s=(struct poly *)malloc(sizeof(struct poly));
f->next=s;
s->pow=q->pow;
s->coeff=q->coeff;
q=q->next;
f=f->next;
}
}

newnode=newnode->next;
return(newnode);

int main()
{
int n;
printf("\nEnter no in poly one:");
scanf("%d",&n);
head=create(n);
display(head);
printf("\nEnter no in poly two:");
scanf("%d",&n);
head1=create(n);
display(head1);
printf("Addition of two polylomial is");
h=addpoly(head,head1);
display(h);
return 0;
}
Output:
Enter no in poly one:2
Enter the coeff & power:2 2
Enter the coeff & power:3 1
2x^2+3x^1
Enter no in poly two:3
Enter the coeff & power:1 2
Enter the coeff & power:3 1
Enter the coeff & power:5 0
1x^2+3x^1+5x^0
Addition of two polynomial is:
3x^2+6x^1+5x^0

5.3. Generalized linked list


A Generalized Linked List L, is defined as a finite sequence of n>=0 elements, l1, l2,
l3, l4,…, ln, such that li are either atom or the list of atoms. Thus L = (l1, l2, l3, l4, …, ln)
where n is total number of nodes in the list.

To represent a list of items there are certain assumptions about the node structure.
 Flag = 1 implies that down pointer exists
 Flag = 0 implies that next pointer exists
 Data means the atom
 Down pointer is the address of node which is down of the current node
 Next pointer is the address of node which is attached as the next node
Why Generalized Linked List?
Generalized linked lists are used because although the efficiency of polynomial
operations using linked list is good but still, the disadvantage is that the linked list is unable
to use multiple variable polynomial equation efficiently. It helps us to represent multi-
variable polynomial along with the list of elements.
Typical ‘C’ structure of Generalized Linked List

Example of GLL {List representation}


( a, (b, c), d)
When first field is 0, it indicates that the second field is variable. If first field is 1
means the second field is a down pointer, means some list is starting.

Polynomial Representation using Generalized Linked List


The typical node structure will be:

 Here Flag = 0 means variable is present


 Flag = 1 means down pointer is present
 Flag = 2 means coefficient and exponent is present
Example:
9x5 + 7xy4 + 10xz

In the above example the head node is of variable x. The temp node shows the first
field as 2 means coefficient and exponent are present.
 Knowledge Check 2
1. The ……….helps us to represent multi-variable polynomial along with the list of
elements.
2. Generalized linked lists are used ……………
3. Polynomial is a one variable can be represented as…………….
4. Disadvantage is that the linked list is…………..
 Outcome Based Activity
1.
5.4. Summary
 A header node is an extra node that is added at the beginning of the list. This node
does not store any data element but can be used to store some control information
like the number of element etc.
 All linked list operation are performed using a pointer.
 To create a node dynamically during run-time we use functions like malloc(),
calloc(). To free the node, use function free().
 A doubly linked list is a linked list in which each node contains two links- one
pointing to the previous node and one pointing to the next node.
 A circular list is one in which the next field of the last node points to the first node
of the list or the next field of the last node contains the address of the first node of
the list.
 Polynomial is a one variable can be represented as a linked list where each node
of the list represent one term of the polynomial.
 A Generalized Linked List L, is defined as a finite sequence of n>=0 elements, l1,
l2, l3, l4,…, ln, such that li are either atom or the list of atoms. Thus L = (l1, l2, l3,
l4, …, ln) where n is total number of nodes in the list.

5.5. Self-Assessment Questions


Short Answer Type
1. Write a node structure of doubly linked list.
2. Define Generalized linked list.
3. Deleting a node from singly linked list is easier than from a doubly linked list. State
True/False.
4. Write a node structure of singly linked list.
5. What is the circular linked list?
Long Answer Type
1. What are the different operations that can be performed on linked list?
2. Explain how a multi-variable polynomial is represented using generalized linked list?
3. Explain with example how a polynomial in one variable is represented using a singly
listed list.
4. Represent the following generalized linked lists diagrammatically:
a. (A,(B,(C,D),E) b. ((A,B),(C,D),E)
5. Delete the middle element node from a singly linked list.
6. Count number of non-zero element in a singly linked list.
5.6. References
1. Weiss, Data Structures and Algorithm Analysis in C, II Edition, Pearson Education,
2001.
2. Lipschutz: Schaum’s outline series Data structures Tata McGraw-Hill.
3. Data Structures by E. Balagurusamy, McGraw Hill Education.
4. Tenenbaum, Data Structures. Pearson Education, 2000.
5. Kamthane: Introduction to Data Structures in C. Pearson Education 2005.

You might also like