0% found this document useful (0 votes)
37 views12 pages

R23 - DS - Unit II-1

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

R23 - DS - Unit II-1

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

Unit II[R23]

Linked Lists: Singly linked lists: representation and operations, doubly linked lists and
circular linked lists, comparing arrays and linked lists, Applications of linked lists.

Introduction of Linked Lists:


 It is a linear collection of data elements called nodes.
 It is a data structure used to implement other data structures like stacks and queues etc.
 This linked list is viewed as a sequence of nodes in which each node contains one or more
data fields and one pointer (address) field.
 In C, we can implement this linked list by using following structure of node.
struct node
{
int data;
struct node *next;
}
 The data field is used to store the data of element and address field is used to store address of
next node. So it is a pointer field to store address of node.

 We can traverse the entire list by using this start pointer.


 If start == NULL then the list is empty.
Types of Linked Lists: Generally we can have 4 types of linked lists, which include
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List
4. Circular Doubly Linked List.

1. Singly Linked List:


 A singly linked list is a linear data structure in which the elements are not stored in
contiguous memory locations and each element is connected only to its next element using a
pointer.
 It is a simple type of linked list in which all nodes are linked together in a sequential manner.
 So, sometimes it is also called a linear linked list.
 Traversal of this linked list is possible in only one way. i.e from start to end

Representation of Singly Linked List in Memory:


We can represent a singly linked list in 2 ways which include
1. Array Representation or Static Representation
2. Linked Representation or Dynamic Representation

1. Array representation: In this representation a singly linked list is represented by using two
dimensional arrays. When a singly linked list can be represented using an array then each
element of the array represents a node. Each element would contain the data and an index would
represent the link to the next node. However, it may require resizing the array when adding or
removing elements, but that can be inefficient. Consider the following example, where the word
“HELLO” is stored as a singly linked list as follows

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 1


 Start is used to store the address of first node.
 In this example start = 1, so the first node stores at address 1, which is H.
 The corresponding next field stores the address of next node, which is 4.
 So, we will look at 4 to fetch the next element of the list, i. e E.
 We repeat this process until we reach a position where next field contains -1, which denotes
last element of the list.
 Remember that nodes of the linked list need not be consecutive memory locations.

2. Linked Representation: This is the most common representation of a singly linked list. In
this representation, each element of the list, called a node, consists of two parts, i.e the data and a
pointer. The data field is used to hold the value associated with the node, and a pointer (or
reference) is used to link the next node in the sequence. Each node is an instance of a node class
or structure. This representation becomes an efficient representation because we can perform
insertion and deletion of nodes without resizing the list. In this representation a SLL is shown as

 Start pointer stores the address of the first node in the list.
 The last node is not having the next node, so the address field of last node is NULL,

Operations of SLL: The basic operations on single linked list are


1. Insertion
2. Deletion
3. Traversing
1. Insertion: we can do this operation in 3 ways, like
a) Insertion at the beginning of list
b) Insertion at the end of the list
c) Insertion at specific location of the list
a) Insertion at the beginning of list: we can use the following steps to insert a node at
beginning of the linked list
Step 1: Create a new node with given value.
Step 2: Check whether list is empty or not.
Step 3: If it is empty then set node ->next = NULL and Start=node.
Step 4: If it is not empty then set node -> next = Start and Start = node.
This operation can be implemented by using the following function in C language
void addatb()
{
NODE *n=(NODE *)malloc(sizeof(NODE));
printf("\nenter data to insert ");
scanf("%d",&n->data);
if(h==NULL)

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 2


n->next=NULL;
else
n->next=h;
h=n;
}
b) Insertion at the end of list: we can use the following steps to insert a node at end of list
Step 1: Create a new node with given value and node -> next = NULL
Step 2: Check whether the list is empty or not
Step 3: If it is empty then set Start = node
Step 4: If it is not empty then set temp = Start
Step 5: Keep moving temp to its next node until reaches last node, i.e temp -> next ==NULL
Step 6: Set temp -> next = node.
This operation can be implemented by using the following function in C language
void addate()
{
NODE *p=h,*n;
while(p->next!=NULL)
p=p->next;
n=(NODE *)malloc(sizeof(NODE));
printf("\nEnter data to insert ");
scanf("%d",&n->data);
n->next=NULL;
p->next=n;
}

c) Insertion at the specific location of the list: we can use the following steps to insert a node
at specific location of the list.
Step 1: Create a new node with given value
Step 2: Check whether list is empty or not
Step 3: If it is empty then node -> next = NULL and set Start = node
Step 4: If it is not empty then set temp = Start
Step 5: Keep moving temp to its next node until it reaches node after which we want to
insert new node. Every time check whether temp reaches last node or not. If it is
reached last node then display “given node is not found to insert”.
Step 6: Finally, set node -> next = temp -> next and temp -> next = node.
This operation can be implemented by using the following function in C language
void addatp()
{
NODE *p=h,*n;
int x;
n=(NODE *)malloc(sizeof(NODE));
printf("\nEnter data to insert ");
scanf("%d",&n->data);
printf("\nEnter data after which to insert ");
scanf("%d",&x);
while(p->data!=x)
p=p->next;
n->next=p->next;
p->next=n;
}
2. Deletion: We can perform this operation in 3 ways
a) Deletion of beginning node of list
b) Deletion of the end node of the list
M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 3
c) Deletion of specific node of the list
a) Deletion of beginning node: we can use the following steps to delete a node at beginning of
the linked list
Step 1: Check whether list is empty or not.
Step 2: If it is empty then display that “List is empty and deletion is not possible”
Step 3: If it is not empty then set temp = Start and Start = Start -> next.
Step 4: free(temp)
This operation can be implemented by using the following function in C language
void delatb()
{
NODE *t;
if(h==NULL)
printf("List is empty");
else
{
t=h;
h=t->next;
free(t);
}
}
b) Deletion of the end node: we can use the following steps to delete a node at end of list
Step 1: Check whether the list is empty or not
Step 2: If it is empty then display that “List is empty and deletion is not possible”
Step 3: If it is not empty then set temp = Start
Step 4: Keep moving temp to its next node until reaches last but one node,
i.e temp -> next -> next ==NULL
Step 5: Check whether the list contains nodes, if not then set Start = NULL
Step 6: Set t = temp -> next and Set temp -> next = NULL.
Step 7: free(t)
This operation can be implemented by using the following function in C language
void delate()
{
NODE *p=h,*t;
if(p==NULL)
printf("\nList is empty");
else if(p->next==NULL)
h=NULL;
else
while(p->next->next!=NULL)
p=p->next;
t=p->next;
p->next=NULL;
free(t);
}
c) Deletion of specific node: we can use the following steps to delete a specific node of the
linked list
Step 1: Check whether list is empty or not
Step 2: If it is empty then display that “List is empty and deletion is not possible”
Step 4: If it is not empty then set temp1 = Start
Step 5: Keep moving temp1 to its next node until it reaches node which we want to
delete. Every time set temp2 = temp1 before moving temp1 to its next node.
Step 6: Every time check whether temp reaches last node or not. If it is reached last node

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 4


then display “given node is not found to delete”.
Step 7: Once it reached the exact node to delete and check whether list has only one node.
Step 8: It list has only one node then set Start = NULL and free(temp1)
Step 9: Otherwise, set temp2 -> next = temp -> next and free(temp1)
This operation can be implemented by using the following function in C language
void delatp()
{
NODE *p=h;
int x;
printf("\nEnter element to be deleted ");
scanf("%d",&x);
while(p->next->data!=x)
p=p->next;
p->next=p->next->next;
}
3. Tracing: we can use the following steps to trace the elements of linked list
Step 1: Check whether list is empty or not.
Step 2: If it is empty then display that “ List is empty”
Step 3: If it is not empty then set temp = Start
Step 4: Keep displaying temp -> data until temp reaches last node.
This operation can be implemented by using the following function in C language
void dis()
{
NODE *p=h;
if(p==NULL)
printf("List is empty");
else
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("NULL");
}
Advantages and Disadvantages of Singly linked list:
Advantages:
 Insertions and Deletions can be done easily
 It does’t need movement elements in insertion and deletion operations
 Size is not fixed and also there is no memory wastage
 We can increase the size of list as our requirements
 Elements may or may not be stored in consecutive locations.
Disadvantages:
 It requires more space to store the address of next node
 Different amount of time is required to access the elements of list
 We can traverse the list in only one way i.e from start to end
 If we have to go to a particular element then we need to go through all elements that come
before that element
 It is not easy to sort the elements of list
2. Doubly Linked List:
 In the doubly linked list, we can store in each node not only the address of next node but also
the address of previous node in the linked list.

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 5


 So, traversal of this linked list is possible in both ways. i.e from start to end as well as from
end to start.
 It is more complex type of linked list which contains a pointer to next node as well as a
pointer to previous node of the list
 So we can access both access the successor node(next node) as well as predecessor
node(previous node) for any given arbitrary node in the list.
 In C, the structure of node in doubly linked list is given as
struct node
{
struct node *prev;
int data;
struct node *next;
}
 If we have single node in the list then pre and next values are NULL
 Otherwise, the prev field of first is NULL and next field of last is NULL

Representation of Doubly Linked List in Memory: similar to SLL, a DLL is also represented
by using 2 ways like
1. Array Representation
2. Linked List Representation

1. Array representation: In this representation a doubly linked list is represented by using


multi-dimensional arrays. When a doubly linked list can be represented using an array then each
element of the array represents a node. Each element would contain one data and 2 indices which
would represent the links to previous and the next nodes. However, it may require resizing the
array when adding or removing elements, but that can be inefficient. Consider the following
example, where the word “HELLO” is stored as a singly linked list as follows

2. Linked List representation: In this representation, a DLL can be shown as

Operations on Doubly linked list: we can perform the following operations on this list.
1. Insertion
2. Deletion
3. Traversing
1. Insertion: It can be performed in 3 ways
a) Insertion at beginning of list
b) Insertion at end of the list
c) Insertion at specific location of the list
a) Insertion at beginning: we can use following steps to insert node at beginning of list

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 6


Step 1: Create a new node with given value and node -> prev = NULL
Step 2: Check whether list is empty or not.
Step 3: If it is empty then set node -> next = NULL and set Start = node
Step 4: If it is not empty then set node -> next = Start and Start = node
This operation can be implemented by using the following function in C language
void addatb()
{
NODE *n=(NODE *)malloc(sizeof(NODE));
printf("\nenter data to insert ");
scanf("%d",&n->data);
n->prev=NULL;
if(h==NULL)
n->next=NULL;
else
{
n->next=h;
h->prev=n;
}
h=n;
}
b) Insertion at last: we can use following steps to insert node at last of list
Step 1: Create a new node with given value and node -> next = NULL
Step 2: Check whether the list is empty or not.
Step 3: If it is empty then set node -> prev = NULL and Start = node
Step 4: If it is not empty then set temp = Start
Step 5: Keep on moving the temp to its next node until it reaches the last node
i.e temp -> next ==NULL
Step 6: Set temp -> next = node and node -> prev = temp
This operation can be implemented by using the following function in C language
void addate()
{
NODE *p=h,*n;
while(p->next!=NULL)
p=p->next;
n=(NODE *)malloc(sizeof(NODE));
printf("\nEnter data to insert ");
scanf("%d",&n->data);
n->next=NULL;
p->next=n;
n->prev=p;
}
c) Insertion at specific location: We can use the following steps to insert node at specific
location
Step 1: Create a new node with given value
Step 2: Check whether list is empty or not
Step 3: If it is empty then set pre and next fields of node as NULL and set Start = node
Step 4: If it is not empty then set temp1 to Start
Step 5: Keep moving temp1 to its next node until it reaches the node after which we want to
insert
Step 6: Every time check whether temp1 reaches the last node. If it reached the last node then
display that “given node is nor found, insertion is not possible”
Step 7: Finally, set temp2= temp1 -> next, temp1 -> next = node, node ->prev = temp1 and

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 7


node -> next = temp2, temp2 - >prev =node
This operation can be implemented by using the following function in C language
void addatp()
{
NODE *p=h,*n;
int x;
n=(NODE *)malloc(sizeof(NODE));
printf("\nEnter data to insert ");
scanf("%d",&n->data);
printf("\nEnter data after which to insert ");
scanf("%d",&x);
while(p->data!=x)
p=p->next;
n->next=p->next;
n->prev=p;
p->next->prev=n;
p->next=n;
}
2. Deletion: It can be in 3 ways
a) Deletion at beginning of list
b) Deletion at last of list
c) Deletion at specific location of list
a) Deletion at beginning of list: we can use the following steps to delete a node at beginning
Step 1: Check whether list is empty or not
Step 2: If it is empty then display that “Deletion is not possible” and terminate function
Step 3: If it is not empty then set temp = Start
Step 4: Check whether list is having only one node
Step 5: If it is true then Start = NULL and free(temp)
Step 6: If it is false then set Start = temp -> next and Start -> prev = NULL and free(temp)
This operation can be implemented by using the following function in C language
void delatb()
{
NODE *t;
if(h==NULL)
printf("List is empty");
else
{
t=h;
h=t->next;
h->prev=NULL:
free(t);
}
}
b) Deletion at last of list: we can use the following steps to delete a node at end
Step 1: Check whether list is empty or not
Step 2: If it is empty then display that “Deletion is not possible” and terminate function
Step 3: If it is not empty then set temp = Start
Step 4: Check whether list is having only one node.
Step 5: If it is true then set Start = NULL and free(temp)
Step 6: If it is false then keep moving temp until it reaches last node in the list,
i.e temp -> next =NULL
Step 7: set temp -> prev -> next = NULL and free(temp)

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 8


This operation can be implemented by using the following function in C language
void delate()
{
NODE *p=h,*t;
if(p==NULL)
printf("\nList is empty");
else if(p->next==NULL)
h=NULL;
else
while(p->next!=NULL)
p=p->next;
t=p;
p->prev->next=NULL;
free(t);
}
c) Deletion at specific location: we can use following steps to delete a specific node
Step 1: Check whether list is empty or not
Step 2: If it is empty then display that “Deletion is not possible” and terminate function
Step 3: If it is not empty then set temp = Start
Step 4: Keep moving temp until it reaches the exact node to be deleted
Step 5: Check whether list is having only one node.
Step 6: If it is true then set Start = NULL and free(temp)
Step 7: If it is false then set temp -> next -> prev = temp -> prev and
temp -> prev -> next = temp -> next
This operation can be implemented by using the following function in C language
void delatp()
{
NODE *p=h,*t;
int x;
printf("\nEnter element to be deleted ");
scanf("%d",&x);
while(p->data!=x)
p=p->next;
t=p;
p->prev->next=p->next;
p->next->prev=p->prev;
free(t);
}
3. Traversing: we can use the following steps to traverse the elements of doubly linked list
Step 1: Check whether list is empty or not.
Step 2: If it is empty then display that “ List is empty”
Step 3: If it is not empty then set temp = Start
Step 4: Keep displaying temp -> data until temp reaches last node.
This operation can be implemented by using the following function in C language
void dis()
{
NODE *p=h;
if(p==NULL)
printf("List is empty");
else
{
while(p!=NULL)
{

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 9


printf("%d<=>",p->data);
p=p->next;
}
printf("NULL");
}
}
3. Circular Linked Lists:
A circular linked list is a variation of the linked list data structure where the last node of the
list points back to the first node instead of pointing to null (or None). This creates a circular
structure where traversal can start from any node and continue until the traversal reaches the
starting node again.
In a circular linked list:
 Each node has a reference or pointer to the next node in the sequence.
 The last node reference points back to the first node, forming a circle.
 There is no NULL reference to indicate the end of the list, instead, the traversal
continues until we reached the starting node of the list.
This circular linked lists can be singly circular, where each node has a single pointer to the next
node or doubly circular where each node has pointer to both the next and previous nodes of the
list.
The operations on CLL can be implemented by using the following functions in C language
struct node
{
int data;
struct node *next;
}*s=NULL;
void creat()
{
int len,x,i;
NODE *p,*n;
printf("\nEnter howmany nodes with which to create list ");
scanf("%d",&len);
for(i=1;i<=len;i++)
{
n=(NODE *)malloc(sizeof(NODE));
printf("\nEnter data to insert ");
scanf("%d",&x);
n->data = x;
if(s==NULL)
{
s=n;
n->next=n;
}
else
{
n->next=s;
p->next=n;
}
p=n;
}
}
void dis()
{
NODE *p=s;
if(p==NULL)

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 10


printf("List is empty ");
else
{
while(p->next!=s)
{
printf("%d->",p->data);
p=p->next;
}
printf("%d",p->data);
}
}
void insert()
{
NODE *n,*t;
int x;
n=(NODE *)malloc(sizeof(NODE));
printf("enter data to insert ");
scanf("%d",&n->data);
printf("\nEnter node after which we want to insert ");
scanf("%d",&x);
t=s;
while(t->data!=x)
t=t->next;
n->next=t->next;
t->next=n;
}
void delet()
{
NODE *t=s;
int x;
printf("\nEnter node to delete ");
scanf("%d",&x);
while(t->next->data!=x)
t=t->next;
if(t->next==s)
s=t->next->next;
t->next=t->next->next;
}

Arrays Vs Linked List:


Arrays Linked List
Arrays will allocate memory in sequential Linked List will allocate memory for the elements
Order in random
Memory is allocated at compile time Memory is allocated at runtime
These are fixed size and can’t be changed These are dynamic, i.e they can grow and shrink
during the execution during the execution
Sometimes memory space is wasted Memory is not wasted at any time because there is
no pre-allocation, memory is allocate only when it
is required
Insertions and deletions are very difficult Insertions and deletions are easy and efficient.

Applications of Singly Linked List:


The Singly Linked Lists can be used to represent
1. Polynomials
2. Sparse matrix.

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 11


1. Polynomials: Polynomials are the expressions containing number of terms with non-zero
coefficients and exponents.
P(x) = a0+a1x+a2x2+…………….+anxn
Where ai is non-zero coefficient and n is the non-negative integer.
 In the linked representation of polynomial, each term is considered as node with 3 fields,
1. Coefficient field
2. Exponent field
3. Next node address field.
 The coefficient field holds the coefficient value of term, exponent field holds the exponent
value of term and next field holds the address of the next term in the polynomial.
 In C, the polynomial node is given as
struct polynode
{
int coeff;
int exp;
struct polynode *Next;
}
2. Sparse Matrix:
 If most of the elements of a matrix have value 0 then that matrix is called as Sparse matrix.
 The node representation of sparse matrix is
Row number Column number Value
Pointer to non zero value in column(Down) Pointer to non zero value in row (right)

For example consider the following sparse matrix,

This sparse matrix can be represented using linked list for every row and column. Because a
value is in exactly one row and one column, it will appear in both lists exactly once. The
following is the multi linked list representation of above sparse matrix.

M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 12

You might also like