Linked List
1
By:
Dabal Singh Mahara
2017
Unit – 5
Contents Hours Marks
a. Concept and Definition
b. Inserting and deleting nodes
c. Linked List implementation of Stack (Push/Pop)
d. Linked list implementation of Queues
(Insert/Remove)
e. Circular List
• Stack as a Circular List (Push/Pop)
• Queue as a Circular List (Insert/Remove)
f. Doubly Linked List (Insert/Remove)
6 8
2
Concept and Definition
of Linked List
4
What is a linked list?
• If the memory is allocated at compile time of a program, then it is fixed and
cannot be changed.
• There is an alternative strategy to allocate memory only when it is required.
This special data structure is called linked list that provides a more flexible
storage system.
• Linked list is a linear collection of data elements called nodes, linked to one
another by means of the pointers.
• Each node is divided into two parts:
• The first part is data or info of the element
• The second part is link or pointer to next node in the list.
10 NULL
data next
Example Linked List
• Start is a special pointer that points to the first node of the list and next field
of last node contains a special pointer known as NULL pointer.
• NULL indicates the end of linked list.
• If start == NULL then the list is empty.
10
data next
15 20 NULLstart
Representation of Node
• The node is represented by a self-referential structure.
• That is, a structure that contains a reference to itself is known as self-
referential structure.
• node structure is given below:
struct node
{
int data;
struct node * next;
};
Advantages and Disadvantages
• Advantages:
o Linked list is dynamic data-structure that can grow and shrink during the
execution of a program. This feature helps in efficient memory utilization.
o Insertion and deletion are more easier and efficient.
• Disadvantages:
o More memory is needed to store a data item as an extra field for address is
allocated.
o Access to arbitrary element is a bit more cumbersome and time consuming.
Operations on Linked List
The basic operations performed on the linked list are as follows:
• Create: To create a linked list
• Insert: To insert a node at any specified position
• Delete: To delete a node from linked list
• Traversing: Going through all the nodes in the linked list
• Searching: This operation is used to find an element in the linked list.
• Concatenation: Process of appending second list to the end of the
first list.
Types of Linked List
• Basically, there are three types of linked lists:
• Singly Linked List
• Doubly Linked List
• Circular Linked List
• Singly Linked List: A singly linked list is a dynamic data structure in which each node has
two fields:
• One for data and another is link field.
• The data field stores the data value and link field stores address of next node in the
list.
• The first node is pointed by external pointer 'start' and node at last has a NULL
pointer.
10
data next
15 20 NULL
start
Fig. the singly linked list with three elements 10, 15 and 20
Representation of Singly Linked List
• Create a structure for a node with two fields: info and
next field as given below:
struct node
{
int info;
struct node * next;
};
struct node *start;
void createEmptyList()
{
start = NULL;
}
Creating a node
• Creation of a node consists:
struct node *newnode;
newnode = (struct node *) malloc(sizeof(struct node));
newnode -> info = x;
newnode ->next = NULL;
• We can define a function to create a node that allocates memory for a
node dynamically and returns a pointer to newly created node.
• struct node *getnode()
{
struct node *newnode;
newnode = (struct node *) malloc(sizeof(struct node));
return (newnode);
}
Inserting a node in Linked List
• To insert a node into linked list, following three steps are performed:
 Allocating a node
 Assigning data to info field of node
 Adjusting a pointer
• The new node can be inserted :
• At the beginning of linked list
• At the end of the linked list
• At any specified position of the lined list
• Insertion requires obtaining a new node and changing tow links
Inserting at the beginning of List
• Algorithm: Let *start be the pointer to first node in the current list.
1. Create a new node using malloc function
i.e. newnode = (struct node *) malloc(sizeof(struct node));
2. Assign data to the info field of new node
i.e. newnode->info = x
3. Set next field of newnode to start.
i.e. newnode->next = start;
4. Set the start pointer to the newnode.
i.e. start = newnode;
5. end
C-function to insert a node at beginning
void insertBeg( int item)
{
struct node *newnode;
newnode = (struct node *) malloc(sizeof(struct node));
newnode->info = item;
newnode->next = start;
start = newnode;
}
Insert a node at End
• Let start be the pointer to the first node in the current list
• Algorithm:
1. Create a newnode using malloc function.
2. Assign data to the info field of new node.
3. Set: newnode->next = NULL;
4. If start = NULL then
set: start = newnode;
else
set: temp = start
while(temp->next != NULL)
temp = temp->next;
temp->next = newnode
5. end
C-function to insert a node at the End
void insertEnd( int item)
{
struct node *newnode, *temp;
newnode = (struct node *) malloc(sizeof(struct node));
newnode->info = item;
if(start==NULL)
start = newnode;
else
{
temp= start;
while(temp->next !=NULL)
temp = temp->next;
temp->next = newnode;
}
}
Insert a node at specified position
• Let *start be the pointer to first node.
1. Create a newnode using malloc function
2. Assign data to info of new node
3. Enter position of node at which the node to be inserted. Let position be pos.
4. if start == NULL
i. print "empty list"
ii. exit
5. else
i. temp = start
ii. for(i=1; i<pos-1; i++)
{ temp = temp->next;
if temp = null
print "can't inserted at given position" and exit }
6. Set NewNode ->next=temp->next
7. Set temp->next = NewNode
8. End
Exercise
• Write a C –function to Insert a node at specified
position in the linked list.
Deleting Nodes
A node may be deleted:
• From the beginning of the linked list
• From the end of the linked list
• From the specified position in a linked list
Deleting a node at the beginning
• Let *start be the pointer to first node in the current list.
1. If start == NULL then
print " The list is empty"
exit
2. temp = start
3. start = start - > next
4. free(temp)
5. end
C-function to delete a node at beginning
void deletebeg()
{
struct node * temp;
if(start == NULL)
{
printf(" Empty List");
exit(1);
}
else
{
temp = start;
printf("n The deleted node is %d", start->info);
start = start ->next;
free(temp);
}
}
Deleting a node at the end
• Let *start be the pointer to first node in the current list.
1. If start == NULL then
print " The list is empty"
exit
2. else if start -> next == NULL
{
temp = start
start = NULL
free(temp)
}
3. else
{
temp = start
while(temp->next->next != NULL)
temp= temp -> next
free( temp -> next )
temp - > next = NULL
}
4. End
C-function to delete a node at the end
void deleteend()
{
struct node * temp;
if(start == NULL)
{
printf(" Empty List");
exit(1);
}
else if (start -> next == NULL)
{
temp = start;
printf("n The deleted node is %d", start->info);
start = NULL;
free(temp);
}
else
{
temp = start;
while( temp->next -> next != NULL)
temp = temp -> next;
free(temp->next);
temp->next = NULL;
}
Deleting a node at the specified position
• Let *start be the pointer to first node in the current list.
1. Read position of the node to be deleted. Let it be pos.
2. If start == NULL then
print " The list is empty"
exit
3. else
{
temp = start
for( i = 1; i < pos -1; i++)
{
temp= temp -> next
if temp = null
print "can't delete at given position" and exit
}
hold = temp->next
temp - > next = hold->next
free( hold )
}
4. End
Exercise
•Write C – function to delete a node at the
specified position.
Searching an item in the linked list
• To search an item in the linked list we need to find the node that
contains data value.
Algorithm: Let *start be the pointer to first node in the current list.
1. Read key to search.
2. If start == NULL then
print " The list is empty"
exit
3. else
{
temp = start
while( temp != NULL)
{
if( temp->info == key)
print " Key found" and break
temp = temp -> next
}
if ( temp == NULL)
print " Unsuccessful search"
}
4. End
Exercise
•Write C – function to search an item in the
linked list.
C-function to display all elements in the list
void display()
{
struct node * temp;
if(start == NULL)
{
printf(" Empty List");
exit(1);
}
else
{
temp = start;
printf("n The list is: ");
while(temp != NULL)
{
printf(" %dt", temp->info);
temp = temp ->next;
}
}
Linked List Implementation of Stack
• Let *top be a top of a stack or pointer to the first node of the list.
• struct node
{
int item;
struct node * next;
};
struct node * top = NULL;
Push Function
void push(int item)
{
struct node *nnode;
nnode=( struct node *)malloc(sizeof(struct node));
if(top==NULL)
{
nnode->info=item;
nnode->next=NULL;
top=nnode;
}
else
{
nnode->info=item;
nnode->next=top;
top=nnode;
}
}
Pop function
void pop()
{
struct node *temp;
if(top==NULL)
{
printf("Stack contain no elements:n");
return;
}
else
{
temp=top;
top=top->next;
printf("n deleted item is %dt", temp->info);
free(temp);
}
}
Display Function
void display()
{
struct node *temp;
if(top==NULL)
{
printf("Stack is emptyn");
return;
}
else
{
temp=top;
printf("Stack items are:n");
while(temp!=NULL)
{
printf("%dt", temp->info);
temp=temp->next;
}
}
}
Linked list Implementation of Queue
struct node
{
int info; //Declaring an info field
struct node *next; // next pointer of node
};
struct node *front = NULL;
struct node *rear = NULL;
Enqueue Function
void insert()
{
int item;
struct node *nnode,*temp;
printf("Enter the itemt ");
scanf("%d",&item);
nnode = (struct node *) malloc(sizeof(struct node));
nnode->info = item;
nnode->next = NULL;
if(front ==NULL)
front=rear = nnode;
else
{
rear->next = nnode;
rear = nnode;
}
}
Dequeue Function
int del()
{
int item;
struct node *temp;
if(front ==NULL)
{
printf("n Empty List !!!");
return 0;
}
else if(front->next==NULL)
{
temp= front;
rear=front =NULL;
printf("n The deleted item is %d",
temp->info);
free(temp);
}
else
{
temp=front;
front = front->next;
printf("n Deleted item is %dn",
temp->info);
free(temp);
}
}
Display Function
int display()
{
struct node *temp;
if(front==NULL)
{
printf("n The list is empty");
return 0;
}
else
{
temp=front;
printf("n The list is:t");
while(temp!=NULL)
{
printf(" %d -> ",temp->info);
temp=temp->next;
}
}
}
Circular Linked List
• A circular linked list is a list where next field of last node points to the very
first node of list.
• Circular linked list can be used to help the traverse he same list again and
again if needed. A circular list is very similar to the linear list where in the
circular list the pointer of the last node points not NULL but the first node.
• There is not last node in the circular list. But for convention, last pointer is
chosen to indicate the last node.
C-Representation of Circular List
• We declare structure for the circular list node in the same way as
done in the linear linked list
struct cnode
{
int info;
struct node * next;
};
struct cnode *start = NULL;
struct cnode *last = NULL;
Adding a node at the Beginning of of CLL
1. Create a new node as : newnode=(NodeType*)malloc(sizeof(NodeType));
2. if start==NULL then
newnode->info=item
newnode->next=newnode
start=newnode
last = newnode
end if
3. else
newnode->info=item
newnode->next=start
start=newnode
last->next=newnode
else end
4. End
Adding a node at the End of CLL
1. Create a new node as : newnode=(NodeType*) malloc(sizeof(NodeType));
2. if start==NULL then
newnode->info=item
newnode->next=newnode
start=newnode
last = newnode
end if
3. else
newnode->info=item
newnode->next=start
last->next=newnode
last = newnode
else end
4. End
Exercise
• Write C-functions to insert a node at:
• Beginning of CLL
• End of CLL
Adding a node at the Beginning of of CLL
void insert_beg() // inserting a node at the beginning
{
int item;
struct node *nnode;
printf("Enter the itemt ");
scanf("%d",&item);
nnode = (struct node *) malloc(sizeof(struct node));
nnode->info = item;
if(start ==NULL)
{
nnode->next = nnode;
start = nnode;
last = nnode;
}
else
{
nnode->next = start;
start = nnode;
last->next = nnode;
}
}
Deleting a node at the beginning of CLL
1. if start==NULL then
print “empty list” and exit
2. else if start == last then
temp = start
start = last = NULL
print " Deleted element: ", temp->info
free(temp)
end else if
3. else
temp=start
start=start->next
print the deleted element=temp->info
last->next=start;
free(temp)
end else
4. End
Deleting a node at the beginning of CLL
int del_beg()
{
int item;
struct node *temp;
if(start ==NULL)
{
printf("n Empty List !!!");
return 0;
}
else if(start==last)
{
temp = start;
start=last = NULL;
printf("n The deleted node is %d",temp->info);
free(temp);
}
else
{
temp= start;
printf("n The deleted item is %d",start->info);
start = start->next;
last->next= start;
free(temp);
}
}
Deleting a node at the End of CLL
1. if start==NULL then
print “empty list” and exit
2. else if start==last
set temp=start
print "Deleted element", temp->info
free(temp)
start=last=NULL
3. else
temp=start
while( temp->next!=last)
temp=temp->next
end while
hold=temp->next
last=temp
last->next=start
print the deleted element=hold->info
free(hold)
end else
4. End
Deleting a node at the End of CLL
int del_end()
{
int item;
struct node *temp,*hold;
if(start ==NULL)
{
printf("n Empty List !!!");
return 0;
}
else if( start==last)
{
temp = start;
printf("n Deleted item is %d",start->info);
start = last = NULL;
free(temp);
}
else
{ temp= start;
while(temp->next!=last)
temp=temp->next;
hold = temp->next;
last = temp;
last->next = start;
printf("n The deleted item is %d",hold->info);
free(hold);
}
}
Display Function in CLL
int display()
{
struct node *temp;
if(start==NULL)
{
printf("n The list is empty");
return 0;
}
else
{
temp=start;
printf("n The list is:");
while(temp!=last )
{
printf("%d ",temp->info);
temp=temp->next;
}
printf("%d",temp->info);
}
}
Stack as a Circular List
• A stack is a LIFO data structure.
• In a stack, the most recently inserted element is removed first out the
stack.
• A stack can be easily implemented using a circular list.
oLet pstack be a pointer to the last node of the circular list.
oFirst node is the top of the stack.
oAn empty stack is represented by a null list.
Structure of a node
struct node
{
int info;
struct node * next;
};
struct node *pstack = NULL;
Push Operation
void push() // inserting a node at the beginning
{
int item;
struct node *nnode;
printf("Enter the itemt ");
scanf("%d",&item);
nnode = (struct node *) malloc(sizeof(struct node));
nnode->info = item;
if(pstack ==NULL)
{
nnode->next = nnode;
pstack = nnode;
}
else
{
nnode->next = pstack->next;
pstack->next = nnode;
}
}
Pop Operation
int pop()
{
int item;
struct node *temp;
if(pstack ==NULL)
{
printf("n Empty List !!!");
return 0;
}
else if(pstack->next==pstack)
{
temp = pstack;
pstack = NULL;
printf("n The deleted node is %d",temp->info);
free(temp);
}
else
{
temp= pstack->next;
printf("n The deleted item is %d",temp->info);
pstack->next = temp->next;
free(temp);
}
}
Display Function
int display()
{
struct node *temp;
if(pstack==NULL)
{
printf("n The list is empty");
return 0;
}
else
{
temp=pstack->next;
printf("n The list is:");
while(temp!=pstack )
{
printf("%d ",temp->info);
temp=temp->next;
}
printf("%d",temp->info);
}
}
Queue as a Circular List
• A queue is a FIFO data structure.
• By using a circular list, a queue may be specified using a single pointer
q to that list.
• A node pointed by q is rear of the queue and the node following q is
its front.
q
Structure of a node
struct node
{
int info;
struct node * next;
};
struct node *q = NULL;
Insert Operation
void insert_node()
{
int item;
struct node *nnode;
printf("Enter the itemt ");
scanf("%d",&item);
nnode = (struct node *) malloc(sizeof(struct node));
nnode->info = item;
if( q ==NULL)
{
nnode->next = nnode;
q = nnode;
}
else
{
nnode->next = q->next;
q->next = nnode;
q = nnode;
}
}
Delete Operation
int delete_node()
{
int item;
struct node *temp;
if( q ==NULL)
{
printf("n Empty List !!!");
return 0;
}
else if( q->next== q)
{
temp = q;
q = NULL;
printf("n The deleted node is %d",temp->info);
free(temp);
}
else
{
temp= q->next;
printf("n The deleted item is %d",temp->info);
q->next = temp->next;
free(temp);
}
}
Display Function
int display()
{
struct node *temp;
if(q ==NULL)
{
printf("n The list is empty");
return 0;
}
else
{
temp= q->next;
printf("n The list is:");
while(temp!= q )
{
printf("%d ",temp->info);
temp=temp->next;
}
printf("%d",temp->info);
}
}
Doubly Linked List
• A linked list in which each node contains three fields: two pointer
fields and one data field is called doubly linked list.
• Two pointers link next node and previous nodes of a node.
• It provides bidirectional traversal.
NULL 10 5 7 NULL
start
Structure of a node
struct node
{
int info;
struct node * next;
struct node * prev;
};
struct node *start = NULL;
inserting a node at the beginning of DLL
void insert_beg()
{
int item;
struct node *nnode;
printf("Enter the itemt");
scanf("%d",&item);
nnode = (struct node *) malloc(sizeof(struct node));
nnode->info = item;
if(start == NULL)
{
nnode->next = NULL;
nnode ->prev = NULL;
start = nnode;
}
else
{
nnode->next= start;
nnode->prev= NULL;
start->prev = nnode;
start = nnode;
}
}
Inserting a Node at the End of DLL
void insert_end()
{
int item;
struct node *nnode,*temp;
printf("Enter the itemt ");
scanf("%d",&item);
nnode = (struct node *) malloc(sizeof(struct node));
nnode->info = item;
nnode->next = NULL;
if(start ==NULL)
{
start = nnode;
nnode->prev = NULL;
}
else
{
temp = start;
while(temp->next!=NULL)
temp=temp->next;
temp->next = nnode;
nnode->prev = temp;
}
}
Deletion of a Node at Beginning of DLL
int del_beg()
{
int item;
struct node *temp;
if(start ==NULL)
{
printf("n Empty List !!!");
return 0;
}
else if (start->next==NULL)
{
temp=start;
start = NULL;
free(temp);
}
else
{
temp= start;
printf("n The deleted item is %d",start->info);
start = start->next;
start->prev= NULL;
free(temp);
}
}
Delete a node at the End of DLL
int del_end()
{
int item;
struct node *temp, *hold;
if(start ==NULL)
{
printf("n Empty List !!!");
return 0;
}
else if( start->next==NULL)
{
temp = start;
printf("n Deleted item is %d",start->info);
start = NULL;
free(temp);
}
else
{
temp= start;
while(temp->next->next!=NULL)
temp=temp->next;
hold = temp->next;
temp->next = NULL;
printf("n The deleted item is %d",hold->info);
free(hold);
}
}
Display Function in DLL
int display()
{
struct node *temp;
if(start==NULL)
{
printf("n The list is empty");
return 0;
}
else
{
temp=start;
printf("n The list is:");
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->next;
}
}
}
Doubly Circular Linked List
• A circular doubly linked list is one which has the successor and
predecessor pointer in circular manner.
• That is, a circular doubly linked list is a doubly linked list where the
next field of the last node has pointer to the first node and previous
field of first node points to the last node of the list.
• The main objective of doubly circular linked list is to simplify the
insertion and deletion operations performed on doubly linked list.
10 5 7
start
last
Structure of a node
struct node
{
int info;
struct node *prev;
struct node *next;
};
struct node *start = NULL;
struct node *last = NULL;
Adding a node at the Beginning of DCLL
void insert_beg()
{
int item;
struct node *nnode;
printf("Enter the itemt ");
scanf("%d",&item);
nnode = (struct node *) malloc(sizeof(struct node));
nnode->info = item;
if(start == NULL)
{
nnode->next = nnode;
nnode ->prev = nnode;
start = nnode;
last = nnode;
}
else
{
nnode->next= start;
nnode->prev= last;
start->prev = nnode;
start = nnode;
last->next = nnode;
}
}
Adding a node at the End of DCLL
void insert_end()
{
int item;
struct node *nnode,*temp;
printf("Enter the itemt ");
scanf("%d",&item);
nnode = (struct node *) malloc(sizeof(struct node));
nnode->info = item;
if(start ==NULL)
{
start = nnode;
last = nnode;
nnode->prev = nnode;
nnode->next= nnode;
start = last = nnode;
}
else
{
last->next= nnode;
nnode->prev= last;
last= nnode;
nnode->next = start;
start->prev= nnode;
}
}
Deleting a node at the Beginning of DCLL
int del_beg()
{
int item;
struct node *temp;
if(start ==NULL)
{
printf("n Empty List !!!");
return 0;
}
else if (start==last)
{
temp=start;
start = NULL;
last = NULL;
free(temp);
}
else
{
temp= start;
printf("n The deleted item is %d",start->info);
start = start->next;
start->prev= last;
last->next= start;
free(temp);
}
}
Deleting a node at the End of DCLL
int del_end()
{
int item;
struct node *temp, *hold;
if(start ==NULL)
{
printf("n Empty List !!!");
return 0;
}
else if( start==last)
{
temp = start;
printf("n Deleted item is %d",start->info);
start = NULL;
last = NULL;
free(temp);
}
else
{
temp= last;
start->prev = last->prev;
last= last->prev;
last->next = start;
printf("n The deleted item is %d",temp->info);
free(temp);
}
}
Homework #6
1. State relative merits and demerits of contiguous list and linked list.
2. Write an algorithm to add a node at the beginning of singly linked list.
3. How can you insert a node at the end of doubly linked list? Explain.
4. Differentiate between singly linked list, doubly linked list, CLL and DCLL.
5. Write a C-function to delete a node at the end of singly linked list.
6. Write a C function to add a node at the beginning of DLL.
7. Write a C function to delete a node at the end in DCLL.
8. Write an algorithm to add a node at the beginning in the CLL.
Thank You !

Unit 5 linked list

  • 1.
  • 2.
    Unit – 5 ContentsHours Marks a. Concept and Definition b. Inserting and deleting nodes c. Linked List implementation of Stack (Push/Pop) d. Linked list implementation of Queues (Insert/Remove) e. Circular List • Stack as a Circular List (Push/Pop) • Queue as a Circular List (Insert/Remove) f. Doubly Linked List (Insert/Remove) 6 8 2
  • 3.
  • 4.
    4 What is alinked list? • If the memory is allocated at compile time of a program, then it is fixed and cannot be changed. • There is an alternative strategy to allocate memory only when it is required. This special data structure is called linked list that provides a more flexible storage system. • Linked list is a linear collection of data elements called nodes, linked to one another by means of the pointers. • Each node is divided into two parts: • The first part is data or info of the element • The second part is link or pointer to next node in the list. 10 NULL data next
  • 5.
    Example Linked List •Start is a special pointer that points to the first node of the list and next field of last node contains a special pointer known as NULL pointer. • NULL indicates the end of linked list. • If start == NULL then the list is empty. 10 data next 15 20 NULLstart
  • 6.
    Representation of Node •The node is represented by a self-referential structure. • That is, a structure that contains a reference to itself is known as self- referential structure. • node structure is given below: struct node { int data; struct node * next; };
  • 7.
    Advantages and Disadvantages •Advantages: o Linked list is dynamic data-structure that can grow and shrink during the execution of a program. This feature helps in efficient memory utilization. o Insertion and deletion are more easier and efficient. • Disadvantages: o More memory is needed to store a data item as an extra field for address is allocated. o Access to arbitrary element is a bit more cumbersome and time consuming.
  • 8.
    Operations on LinkedList The basic operations performed on the linked list are as follows: • Create: To create a linked list • Insert: To insert a node at any specified position • Delete: To delete a node from linked list • Traversing: Going through all the nodes in the linked list • Searching: This operation is used to find an element in the linked list. • Concatenation: Process of appending second list to the end of the first list.
  • 9.
    Types of LinkedList • Basically, there are three types of linked lists: • Singly Linked List • Doubly Linked List • Circular Linked List • Singly Linked List: A singly linked list is a dynamic data structure in which each node has two fields: • One for data and another is link field. • The data field stores the data value and link field stores address of next node in the list. • The first node is pointed by external pointer 'start' and node at last has a NULL pointer. 10 data next 15 20 NULL start Fig. the singly linked list with three elements 10, 15 and 20
  • 10.
    Representation of SinglyLinked List • Create a structure for a node with two fields: info and next field as given below: struct node { int info; struct node * next; }; struct node *start; void createEmptyList() { start = NULL; }
  • 11.
    Creating a node •Creation of a node consists: struct node *newnode; newnode = (struct node *) malloc(sizeof(struct node)); newnode -> info = x; newnode ->next = NULL; • We can define a function to create a node that allocates memory for a node dynamically and returns a pointer to newly created node. • struct node *getnode() { struct node *newnode; newnode = (struct node *) malloc(sizeof(struct node)); return (newnode); }
  • 12.
    Inserting a nodein Linked List • To insert a node into linked list, following three steps are performed:  Allocating a node  Assigning data to info field of node  Adjusting a pointer • The new node can be inserted : • At the beginning of linked list • At the end of the linked list • At any specified position of the lined list • Insertion requires obtaining a new node and changing tow links
  • 13.
    Inserting at thebeginning of List • Algorithm: Let *start be the pointer to first node in the current list. 1. Create a new node using malloc function i.e. newnode = (struct node *) malloc(sizeof(struct node)); 2. Assign data to the info field of new node i.e. newnode->info = x 3. Set next field of newnode to start. i.e. newnode->next = start; 4. Set the start pointer to the newnode. i.e. start = newnode; 5. end
  • 14.
    C-function to inserta node at beginning void insertBeg( int item) { struct node *newnode; newnode = (struct node *) malloc(sizeof(struct node)); newnode->info = item; newnode->next = start; start = newnode; }
  • 15.
    Insert a nodeat End • Let start be the pointer to the first node in the current list • Algorithm: 1. Create a newnode using malloc function. 2. Assign data to the info field of new node. 3. Set: newnode->next = NULL; 4. If start = NULL then set: start = newnode; else set: temp = start while(temp->next != NULL) temp = temp->next; temp->next = newnode 5. end
  • 16.
    C-function to inserta node at the End void insertEnd( int item) { struct node *newnode, *temp; newnode = (struct node *) malloc(sizeof(struct node)); newnode->info = item; if(start==NULL) start = newnode; else { temp= start; while(temp->next !=NULL) temp = temp->next; temp->next = newnode; } }
  • 17.
    Insert a nodeat specified position • Let *start be the pointer to first node. 1. Create a newnode using malloc function 2. Assign data to info of new node 3. Enter position of node at which the node to be inserted. Let position be pos. 4. if start == NULL i. print "empty list" ii. exit 5. else i. temp = start ii. for(i=1; i<pos-1; i++) { temp = temp->next; if temp = null print "can't inserted at given position" and exit } 6. Set NewNode ->next=temp->next 7. Set temp->next = NewNode 8. End
  • 18.
    Exercise • Write aC –function to Insert a node at specified position in the linked list.
  • 19.
    Deleting Nodes A nodemay be deleted: • From the beginning of the linked list • From the end of the linked list • From the specified position in a linked list
  • 20.
    Deleting a nodeat the beginning • Let *start be the pointer to first node in the current list. 1. If start == NULL then print " The list is empty" exit 2. temp = start 3. start = start - > next 4. free(temp) 5. end
  • 21.
    C-function to deletea node at beginning void deletebeg() { struct node * temp; if(start == NULL) { printf(" Empty List"); exit(1); } else { temp = start; printf("n The deleted node is %d", start->info); start = start ->next; free(temp); } }
  • 22.
    Deleting a nodeat the end • Let *start be the pointer to first node in the current list. 1. If start == NULL then print " The list is empty" exit 2. else if start -> next == NULL { temp = start start = NULL free(temp) } 3. else { temp = start while(temp->next->next != NULL) temp= temp -> next free( temp -> next ) temp - > next = NULL } 4. End
  • 23.
    C-function to deletea node at the end void deleteend() { struct node * temp; if(start == NULL) { printf(" Empty List"); exit(1); } else if (start -> next == NULL) { temp = start; printf("n The deleted node is %d", start->info); start = NULL; free(temp); } else { temp = start; while( temp->next -> next != NULL) temp = temp -> next; free(temp->next); temp->next = NULL; }
  • 24.
    Deleting a nodeat the specified position • Let *start be the pointer to first node in the current list. 1. Read position of the node to be deleted. Let it be pos. 2. If start == NULL then print " The list is empty" exit 3. else { temp = start for( i = 1; i < pos -1; i++) { temp= temp -> next if temp = null print "can't delete at given position" and exit } hold = temp->next temp - > next = hold->next free( hold ) } 4. End
  • 25.
    Exercise •Write C –function to delete a node at the specified position.
  • 26.
    Searching an itemin the linked list • To search an item in the linked list we need to find the node that contains data value. Algorithm: Let *start be the pointer to first node in the current list. 1. Read key to search. 2. If start == NULL then print " The list is empty" exit 3. else { temp = start while( temp != NULL) { if( temp->info == key) print " Key found" and break temp = temp -> next } if ( temp == NULL) print " Unsuccessful search" } 4. End
  • 27.
    Exercise •Write C –function to search an item in the linked list.
  • 28.
    C-function to displayall elements in the list void display() { struct node * temp; if(start == NULL) { printf(" Empty List"); exit(1); } else { temp = start; printf("n The list is: "); while(temp != NULL) { printf(" %dt", temp->info); temp = temp ->next; } }
  • 29.
    Linked List Implementationof Stack • Let *top be a top of a stack or pointer to the first node of the list. • struct node { int item; struct node * next; }; struct node * top = NULL;
  • 30.
    Push Function void push(intitem) { struct node *nnode; nnode=( struct node *)malloc(sizeof(struct node)); if(top==NULL) { nnode->info=item; nnode->next=NULL; top=nnode; } else { nnode->info=item; nnode->next=top; top=nnode; } }
  • 31.
    Pop function void pop() { structnode *temp; if(top==NULL) { printf("Stack contain no elements:n"); return; } else { temp=top; top=top->next; printf("n deleted item is %dt", temp->info); free(temp); } }
  • 32.
    Display Function void display() { structnode *temp; if(top==NULL) { printf("Stack is emptyn"); return; } else { temp=top; printf("Stack items are:n"); while(temp!=NULL) { printf("%dt", temp->info); temp=temp->next; } } }
  • 33.
    Linked list Implementationof Queue struct node { int info; //Declaring an info field struct node *next; // next pointer of node }; struct node *front = NULL; struct node *rear = NULL;
  • 34.
    Enqueue Function void insert() { intitem; struct node *nnode,*temp; printf("Enter the itemt "); scanf("%d",&item); nnode = (struct node *) malloc(sizeof(struct node)); nnode->info = item; nnode->next = NULL; if(front ==NULL) front=rear = nnode; else { rear->next = nnode; rear = nnode; } }
  • 35.
    Dequeue Function int del() { intitem; struct node *temp; if(front ==NULL) { printf("n Empty List !!!"); return 0; } else if(front->next==NULL) { temp= front; rear=front =NULL; printf("n The deleted item is %d", temp->info); free(temp); } else { temp=front; front = front->next; printf("n Deleted item is %dn", temp->info); free(temp); } }
  • 36.
    Display Function int display() { structnode *temp; if(front==NULL) { printf("n The list is empty"); return 0; } else { temp=front; printf("n The list is:t"); while(temp!=NULL) { printf(" %d -> ",temp->info); temp=temp->next; } } }
  • 37.
    Circular Linked List •A circular linked list is a list where next field of last node points to the very first node of list. • Circular linked list can be used to help the traverse he same list again and again if needed. A circular list is very similar to the linear list where in the circular list the pointer of the last node points not NULL but the first node. • There is not last node in the circular list. But for convention, last pointer is chosen to indicate the last node.
  • 38.
    C-Representation of CircularList • We declare structure for the circular list node in the same way as done in the linear linked list struct cnode { int info; struct node * next; }; struct cnode *start = NULL; struct cnode *last = NULL;
  • 39.
    Adding a nodeat the Beginning of of CLL 1. Create a new node as : newnode=(NodeType*)malloc(sizeof(NodeType)); 2. if start==NULL then newnode->info=item newnode->next=newnode start=newnode last = newnode end if 3. else newnode->info=item newnode->next=start start=newnode last->next=newnode else end 4. End
  • 40.
    Adding a nodeat the End of CLL 1. Create a new node as : newnode=(NodeType*) malloc(sizeof(NodeType)); 2. if start==NULL then newnode->info=item newnode->next=newnode start=newnode last = newnode end if 3. else newnode->info=item newnode->next=start last->next=newnode last = newnode else end 4. End
  • 41.
    Exercise • Write C-functionsto insert a node at: • Beginning of CLL • End of CLL
  • 42.
    Adding a nodeat the Beginning of of CLL void insert_beg() // inserting a node at the beginning { int item; struct node *nnode; printf("Enter the itemt "); scanf("%d",&item); nnode = (struct node *) malloc(sizeof(struct node)); nnode->info = item; if(start ==NULL) { nnode->next = nnode; start = nnode; last = nnode; } else { nnode->next = start; start = nnode; last->next = nnode; } }
  • 43.
    Deleting a nodeat the beginning of CLL 1. if start==NULL then print “empty list” and exit 2. else if start == last then temp = start start = last = NULL print " Deleted element: ", temp->info free(temp) end else if 3. else temp=start start=start->next print the deleted element=temp->info last->next=start; free(temp) end else 4. End
  • 44.
    Deleting a nodeat the beginning of CLL int del_beg() { int item; struct node *temp; if(start ==NULL) { printf("n Empty List !!!"); return 0; } else if(start==last) { temp = start; start=last = NULL; printf("n The deleted node is %d",temp->info); free(temp); } else { temp= start; printf("n The deleted item is %d",start->info); start = start->next; last->next= start; free(temp); } }
  • 45.
    Deleting a nodeat the End of CLL 1. if start==NULL then print “empty list” and exit 2. else if start==last set temp=start print "Deleted element", temp->info free(temp) start=last=NULL 3. else temp=start while( temp->next!=last) temp=temp->next end while hold=temp->next last=temp last->next=start print the deleted element=hold->info free(hold) end else 4. End
  • 46.
    Deleting a nodeat the End of CLL int del_end() { int item; struct node *temp,*hold; if(start ==NULL) { printf("n Empty List !!!"); return 0; } else if( start==last) { temp = start; printf("n Deleted item is %d",start->info); start = last = NULL; free(temp); } else { temp= start; while(temp->next!=last) temp=temp->next; hold = temp->next; last = temp; last->next = start; printf("n The deleted item is %d",hold->info); free(hold); } }
  • 47.
    Display Function inCLL int display() { struct node *temp; if(start==NULL) { printf("n The list is empty"); return 0; } else { temp=start; printf("n The list is:"); while(temp!=last ) { printf("%d ",temp->info); temp=temp->next; } printf("%d",temp->info); } }
  • 48.
    Stack as aCircular List • A stack is a LIFO data structure. • In a stack, the most recently inserted element is removed first out the stack. • A stack can be easily implemented using a circular list. oLet pstack be a pointer to the last node of the circular list. oFirst node is the top of the stack. oAn empty stack is represented by a null list.
  • 49.
    Structure of anode struct node { int info; struct node * next; }; struct node *pstack = NULL;
  • 50.
    Push Operation void push()// inserting a node at the beginning { int item; struct node *nnode; printf("Enter the itemt "); scanf("%d",&item); nnode = (struct node *) malloc(sizeof(struct node)); nnode->info = item; if(pstack ==NULL) { nnode->next = nnode; pstack = nnode; } else { nnode->next = pstack->next; pstack->next = nnode; } }
  • 51.
    Pop Operation int pop() { intitem; struct node *temp; if(pstack ==NULL) { printf("n Empty List !!!"); return 0; } else if(pstack->next==pstack) { temp = pstack; pstack = NULL; printf("n The deleted node is %d",temp->info); free(temp); } else { temp= pstack->next; printf("n The deleted item is %d",temp->info); pstack->next = temp->next; free(temp); } }
  • 52.
    Display Function int display() { structnode *temp; if(pstack==NULL) { printf("n The list is empty"); return 0; } else { temp=pstack->next; printf("n The list is:"); while(temp!=pstack ) { printf("%d ",temp->info); temp=temp->next; } printf("%d",temp->info); } }
  • 53.
    Queue as aCircular List • A queue is a FIFO data structure. • By using a circular list, a queue may be specified using a single pointer q to that list. • A node pointed by q is rear of the queue and the node following q is its front. q
  • 54.
    Structure of anode struct node { int info; struct node * next; }; struct node *q = NULL;
  • 55.
    Insert Operation void insert_node() { intitem; struct node *nnode; printf("Enter the itemt "); scanf("%d",&item); nnode = (struct node *) malloc(sizeof(struct node)); nnode->info = item; if( q ==NULL) { nnode->next = nnode; q = nnode; } else { nnode->next = q->next; q->next = nnode; q = nnode; } }
  • 56.
    Delete Operation int delete_node() { intitem; struct node *temp; if( q ==NULL) { printf("n Empty List !!!"); return 0; } else if( q->next== q) { temp = q; q = NULL; printf("n The deleted node is %d",temp->info); free(temp); } else { temp= q->next; printf("n The deleted item is %d",temp->info); q->next = temp->next; free(temp); } }
  • 57.
    Display Function int display() { structnode *temp; if(q ==NULL) { printf("n The list is empty"); return 0; } else { temp= q->next; printf("n The list is:"); while(temp!= q ) { printf("%d ",temp->info); temp=temp->next; } printf("%d",temp->info); } }
  • 58.
    Doubly Linked List •A linked list in which each node contains three fields: two pointer fields and one data field is called doubly linked list. • Two pointers link next node and previous nodes of a node. • It provides bidirectional traversal. NULL 10 5 7 NULL start
  • 59.
    Structure of anode struct node { int info; struct node * next; struct node * prev; }; struct node *start = NULL;
  • 60.
    inserting a nodeat the beginning of DLL void insert_beg() { int item; struct node *nnode; printf("Enter the itemt"); scanf("%d",&item); nnode = (struct node *) malloc(sizeof(struct node)); nnode->info = item; if(start == NULL) { nnode->next = NULL; nnode ->prev = NULL; start = nnode; } else { nnode->next= start; nnode->prev= NULL; start->prev = nnode; start = nnode; } }
  • 61.
    Inserting a Nodeat the End of DLL void insert_end() { int item; struct node *nnode,*temp; printf("Enter the itemt "); scanf("%d",&item); nnode = (struct node *) malloc(sizeof(struct node)); nnode->info = item; nnode->next = NULL; if(start ==NULL) { start = nnode; nnode->prev = NULL; } else { temp = start; while(temp->next!=NULL) temp=temp->next; temp->next = nnode; nnode->prev = temp; } }
  • 62.
    Deletion of aNode at Beginning of DLL int del_beg() { int item; struct node *temp; if(start ==NULL) { printf("n Empty List !!!"); return 0; } else if (start->next==NULL) { temp=start; start = NULL; free(temp); } else { temp= start; printf("n The deleted item is %d",start->info); start = start->next; start->prev= NULL; free(temp); } }
  • 63.
    Delete a nodeat the End of DLL int del_end() { int item; struct node *temp, *hold; if(start ==NULL) { printf("n Empty List !!!"); return 0; } else if( start->next==NULL) { temp = start; printf("n Deleted item is %d",start->info); start = NULL; free(temp); } else { temp= start; while(temp->next->next!=NULL) temp=temp->next; hold = temp->next; temp->next = NULL; printf("n The deleted item is %d",hold->info); free(hold); } }
  • 64.
    Display Function inDLL int display() { struct node *temp; if(start==NULL) { printf("n The list is empty"); return 0; } else { temp=start; printf("n The list is:"); while(temp!=NULL) { printf("%d ",temp->info); temp=temp->next; } } }
  • 65.
    Doubly Circular LinkedList • A circular doubly linked list is one which has the successor and predecessor pointer in circular manner. • That is, a circular doubly linked list is a doubly linked list where the next field of the last node has pointer to the first node and previous field of first node points to the last node of the list. • The main objective of doubly circular linked list is to simplify the insertion and deletion operations performed on doubly linked list. 10 5 7 start last
  • 66.
    Structure of anode struct node { int info; struct node *prev; struct node *next; }; struct node *start = NULL; struct node *last = NULL;
  • 67.
    Adding a nodeat the Beginning of DCLL void insert_beg() { int item; struct node *nnode; printf("Enter the itemt "); scanf("%d",&item); nnode = (struct node *) malloc(sizeof(struct node)); nnode->info = item; if(start == NULL) { nnode->next = nnode; nnode ->prev = nnode; start = nnode; last = nnode; } else { nnode->next= start; nnode->prev= last; start->prev = nnode; start = nnode; last->next = nnode; } }
  • 68.
    Adding a nodeat the End of DCLL void insert_end() { int item; struct node *nnode,*temp; printf("Enter the itemt "); scanf("%d",&item); nnode = (struct node *) malloc(sizeof(struct node)); nnode->info = item; if(start ==NULL) { start = nnode; last = nnode; nnode->prev = nnode; nnode->next= nnode; start = last = nnode; } else { last->next= nnode; nnode->prev= last; last= nnode; nnode->next = start; start->prev= nnode; } }
  • 69.
    Deleting a nodeat the Beginning of DCLL int del_beg() { int item; struct node *temp; if(start ==NULL) { printf("n Empty List !!!"); return 0; } else if (start==last) { temp=start; start = NULL; last = NULL; free(temp); } else { temp= start; printf("n The deleted item is %d",start->info); start = start->next; start->prev= last; last->next= start; free(temp); } }
  • 70.
    Deleting a nodeat the End of DCLL int del_end() { int item; struct node *temp, *hold; if(start ==NULL) { printf("n Empty List !!!"); return 0; } else if( start==last) { temp = start; printf("n Deleted item is %d",start->info); start = NULL; last = NULL; free(temp); } else { temp= last; start->prev = last->prev; last= last->prev; last->next = start; printf("n The deleted item is %d",temp->info); free(temp); } }
  • 71.
    Homework #6 1. Staterelative merits and demerits of contiguous list and linked list. 2. Write an algorithm to add a node at the beginning of singly linked list. 3. How can you insert a node at the end of doubly linked list? Explain. 4. Differentiate between singly linked list, doubly linked list, CLL and DCLL. 5. Write a C-function to delete a node at the end of singly linked list. 6. Write a C function to add a node at the beginning of DLL. 7. Write a C function to delete a node at the end in DCLL. 8. Write an algorithm to add a node at the beginning in the CLL.
  • 72.