CSE23302 Module2 Chapter 3.Pptx
CSE23302 Module2 Chapter 3.Pptx
Data Structures
Module 2- Chapter 3
Linked Lists
Dr. R Kanagavalli
Professor
Department of CSE
Global Academy of Technology
Bangalore
Objectives
To Understand the concept of linked list
To implement the primitive operations on linked list
To understand the linked list implementation of stack
To understand the linked list implementation of
queues
To write programs to demonstrate the applications of
linked list.
2
LINKED LISTS
What are the drawbacks of using sequential storage to represent stacks
and queues?
2.No more than that fixed amount of storage may be allocated, thus
introducing the possibility of overflow.
3
Linked list -Introduction
• A linked list is a collection of zero or more nodes where each node has
some information.
• Between each node in the list , there exits a logical relationship. Each
node can hold the data along with a pointer field using which address
of the next node can be obtained.
4
Types of Linked Lists
5
info next info next info next
list null
7
Singly Linked List
• A linked list is a linked list where each node has some data and
pointer to the next node in the list .
• can traverse it from the beginning (first) node only. Cannot traverse in
backward or from the last node to first node.
•
8
Structure Definition of a node
struct node
{
int info;
struct node *link ;
};
typedef struct node *NODE;
9
Operations on Linked Lists
1. Insertion
2. Deletion
3. Display
10
Inserting Nodes into a List
• A list is a dynamic data structure.
11
• For example, suppose that we are given a list of
integers, as shown in fig below:
13
C function to get a node from availability list
if(x ==NULL)
NODE getnode()
{
{
printf(“Out of memory\n”);
NODE x;
exit(0);
x=( NODE ) }
malloc(sizeof(struct return x;
node));
}
14
C function to insert a node at the front of the list
NODE insert_front(int item, NODE first)
{
NODE temp;
temp= (NODE ) malloc(sizeof (struct node);
temp->info = item;
temp->link = first;
return temp;
}
15
info next
temp
= 5000
5000
16
info next
temp 6 1000
= 5000
5000
Initial configuration:
Final configuration:
info next info next
first 5 9 null
node node 18
C function to delete a node from the front of the list
• Sequence of operations:
1. temp = first;
temp
info next info next info next
first 7 5 9 null
node node node
20
2. temp=temp->link
first
info next info next info next
temp 7 5 9 null
node node node
temp
21
3. x = first->info
first
info next info next info next
7 5 9 null
node node node
temp
X=7
22
C function to display the contents of the list
void display(NODE first) printf(“The contents of singly linked
{ list\n”);
cur = first;
NODE cur;
while ( cur !=NULL )
if (first == NULL) {
printf(“%d “, cur->info);
{ cur = cur->link;
printf(“List is empty\n”); }
}
return;
}
23
C function to insert element from rear end
NODE insert_rear(int item, NODE first) cur = first;
{ while ( cur->link != NULL )
NODE temp;
{
NODE cur;
temp = getnode(); cur = cur->link;
temp->info = item; }
temp->link = NULL; cur->link = temp;
if ( first == NULL ) return first;
return temp; }
24
C function to delete element from rear end
NODE delete_rear(NODE first) if( first->link == NULL )
{ {
NODE cur, prev;
printf(“The item to deleted is
if (first == NULL)
{ %d\n, first->info);
printf(“List is empty cannot be free(first);
deleted\n”); return NULL;
return first; }
}
25
prev = NULL; printf(“The item deleted is
cur = first; %d\n”,cur->info);
while( cur->link !=NULL ) free(cur);
{ prev->link = NULL
prev = cur; return first;
cur = cur->link; }
}
26
Linked List implementation of Stacks
27
• Ex: stack as a linked list
info next info next info next
s 60 90 40 null
1. Insert(s,45)
s s
2. delete(s)
s s
X = 45
28
• After freeing temp, we get
s
X = 45
29
• The advantage of the list implementation of the stack
is that:
30
4. Each stack is able to grow and shrink to any size.
31
Linked List implementation of Queues
rear =
Insert() 3000
rear =
4000
33
• Use the following functions
• insert_rear()
• delete_front()
• display()
34
Insertion of new node in the given position
• If p points to an element of the list, inserting a new element after
node(p) involves
• Allocating a new node
• Inserting the information
• Adjusting the pointers
35
•q=getnode( )
•q->info=x
•q->link=p->link
•p->link=q
36
p
s x0 x1 x2 x3 x4 x5 x6
s x0 x1 x2 x3 x4 x5 x6
x q
37
C function to insert element at some
position(AFTER)
NODE insert_after(int item, NODE first, NODE cur = first;
p) while ( cur != p )
{
{
NODE temp;
NODE cur; cur = cur->link;
temp = getnode(); }
temp->info = item; temp->link = temp->link = cur->link;
NULL; cur->link=temp ;
return first;
}
38
• If p points to an element of the list, inserting a new element before
node(p) involves
• Allocating a new node
• Inserting the information
• Inserting the element immediately after node(p) and then
interchanging info(p) with info field of the newly created successor.
39
C function to insert element at some
position(BEFORE)
NODE insert_before(int item, NODE first, cur = first;
NODE p) while ( cur != p )
{
{
NODE temp;
NODE cur; cur = cur->link;
temp = getnode(); }
temp->info = item; temp->link = temp->link = cur->link;
NULL; cur->link=temp ;
cur->info=temp->info;
temp->info=p->info;
return first;
}
40
Other useful operations in the linked list
• To find the length of the linked list
• To search for an item in the list
• To delete an item from the list
• To concatenate two lists
• To reverse a list
41
Node declaration of Polynomial
struct node
{
int coeff ;
int exp;
struct node *next;
};
42
Function to add two polynomials using Linked list
43
// If power of 2nd polynomial is elseif ( poly1->exp < poly2->exp)
greater then 1st, then store 2nd {
as it is and move its pointer
res->exp= poly2->exp;
res->coeff = poly2->coeff;
poly2 = poly2->next;
}
44
// If power of both polynomial else
numbers is same then add {
their coefficients
res->exp= poly1->exp;
res->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
45
// Dynamically create new node
res->next = (structNODE )malloc(sizeof(structnode));
res = res->next;
res->next = NULL;
}
46
while(poly1->next || poly2->next) if (poly2->next)
{ {
if (poly1->next) res->exp= poly2->exp;
{ res->coeff = poly2->coeff;
res->exp= poly1->exp; poly2 = poly2->next;
res->coeff = poly1->coeff; }
poly1 = poly1->next;
}
47
res->next= (struct NODE ) malloc (sizeof (struct node));
res = res->next;
res->next = NULL;
}
}
48
Function to Multiply two polynomials using
Linked list
struct node
{
int co,po;
struct node *addr;
};
typedef struct node *NODE;
49
NODE insertend(NODE start,int co,int po)
{
if(start==NULL)
NODE temp,cur;
return temp;
temp=(NODE)malloc(sizeof(struct
cur=start;
node));
while(cur->addr!=NULL)
temp->co=co;
cur=cur->addr;
temp->po=po;
cur->addr=temp;
temp->addr=NULL;
return start;
}
50
NODE addterm(NODE res,int co,int po) while(cur!=NULL)
{ {
NODE temp,cur; if(cur->po==po)
temp=(NODE)malloc(sizeof(struct {
node)); cur->co=cur->co+co;
temp->co=co; return res;
temp->po=po; }
temp->addr=NULL; cur=cur->addr;
if(res==NULL) }
return temp; if(cur==NULL)
cur=res; res=insertend(res,co,po);
return res;
}
51
NODE multiply(NODE poly1,NODE poly2)
{
NODE p1,p2,res=NULL;
for(p1=poly1;p1!=NULL;p1=p1->addr)
for(p2=poly2;p2!=NULL;p2=p2->addr)
res=addterm(res,p1->co*p2->co,p1->po+p2->po);
return res;
}
52