Professional Documents
Culture Documents
By:
Dabal Singh Mahara
2017
1
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) 6 8
e. Circular List
• Stack as a Circular List (Push/Pop)
• Queue as a Circular List (Insert/Remove)
f. Doubly Linked List (Insert/Remove)
2
Concept and Definition
of Linked List
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
4
Example Linked List
start 10 15 20 NULL
data next
• 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.
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 15 20 NULL
start
data next
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
{ void createEmptyList()
int info; {
struct node * next;
}; start = NULL;
}
struct node *start;
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
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 %d\t", temp->info);
free(temp);
}
}
Display Function
void display()
{
struct node *temp;
if(top==NULL)
{
printf("Stack is empty\n");
return;
}
else
{
temp=top;
printf("Stack items are:\n");
while(temp!=NULL)
{
printf("%d\t", 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
};
if(front ==NULL)
front=rear = nnode;
else
{
rear->next = nnode;
rear = nnode;
}
}
Dequeue Function
int del()
{
int item; else
struct node *temp; {
}
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;
};
};
}
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;
};
}
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.
start
NULL 10 5 7 NULL
Structure of a node
struct node
{
int info;
struct node * next;
struct node * prev;
};
}
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.
last
start
10 5 7
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() nnode ->prev = nnode;
{ start = nnode;
last = nnode;
int item;
}
struct node *nnode;
else
{
printf("\Enter the item\t "); nnode->next= start;
scanf("%d",&item); nnode->prev= last;
nnode = (struct node *) malloc(sizeof(struct node)); start->prev = nnode;
nnode->info = item; start = nnode;
if(start == NULL) last->next = nnode;
}
{
}
nnode->next = nnode;
Adding a node at the End of DCLL
void insert_end() nnode->prev = nnode;
{ nnode->next= nnode;
int item; start = last = nnode;
struct node *nnode,*temp; }
printf("\Enter the item\t "); else
scanf("%d",&item); {
nnode = (struct node *) malloc(sizeof(struct node)); last->next= nnode;
nnode->info = item; nnode->prev= last;
if(start ==NULL) last= nnode;
{ nnode->next = start;
start = nnode; start->prev= nnode;
last = nnode; }
}
Deleting a node at the Beginning of DCLL
int del_beg()
{ else
int item; {
struct node *temp;
temp= start;
if(start ==NULL)
{ printf("\n The deleted item is %d",start->info);
printf("\n Empty List !!!"); start = start->next;
return 0; start->prev= last;
}
last->next= start;
else if (start==last)
{ free(temp);
temp=start; }
start = NULL;
last = NULL;
}
free(temp);
}
Deleting a node at the End of DCLL
int del_end() else
{
int item; {
struct node *temp, *hold; temp= last;
if(start ==NULL)
{ start->prev = last->prev;
printf("\n Empty List !!!"); last= last->prev;
return 0;
} last->next = start;
else if( start==last) printf("\n The deleted item is %d",temp->info);
{
temp = start; free(temp);
printf("\n Deleted item is %d",start->info); }
start = NULL;
last = NULL;
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 !