0% found this document useful (0 votes)
2 views41 pages

Eee DS (Unit 3)

The document provides an overview of linked lists, including single linked lists, circular linked lists, and doubly linked lists, along with their operations such as insertion, deletion, and traversal. It explains the structure of nodes, how to manage memory dynamically, and the specific steps to perform various operations on these data structures. Additionally, it covers header linked lists and their types, emphasizing the organization and manipulation of data in linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views41 pages

Eee DS (Unit 3)

The document provides an overview of linked lists, including single linked lists, circular linked lists, and doubly linked lists, along with their operations such as insertion, deletion, and traversal. It explains the structure of nodes, how to manage memory dynamically, and the specific steps to perform various operations on these data structures. Additionally, it covers header linked lists and their types, emphasizing the organization and manipulation of data in linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

UNIT-III

Syllabus:
Linked Lists: Introduction, Single Linked Lists, Circular Linked Lists, Doubly Linked Lists, Multiple linked
lists, Applications
Linked Stacks and Linked Queues: Introduction, Operations on linked stacks and linked queues, Dynamic
memory management, Implementation of linked representations, Applications.

Single Linked List


What is Linked List?
When we want to work with unknown number of data values, we use a linked list data structure to
organize that data. Linked list is a linear data structure that contains sequence of elements such that each
element links to its next element in the sequence. Each element in a linked list is called as "Node".
What is Single Linked List?
Simply a list is a sequence of data, and linked list is a sequence of data linked with each other.
DEFINITION :
Single linked list is a sequence of elements in which every element has link to its next element in
the sequence.
In any single linked list, the individual element is called as "Node". Every "Node" contains two fields,
data and next.
The data field is used to store actual value of that node and next field is used to store the address of the
next node in the sequence .
The graphical representation of a node in a single linked list is as follows...

NOTE
 In a single linked list, the address of the first node is always stored in a reference node known
as "front" (Some times it is also known as "head").
Always next part (reference part) of the last node must be NULL.

Example

Operations
In a single linked list we perform the following operations...
1. Insertion
2. Deletion
3. Display
Node Representation:

Operations on linked list


• Create
• Insert
• Insert at first position
• Insert at last position
• Insert at middle of the list
• Delete
• Delete node at first position
• Delete node at last position
• Delete node at middle of the list
• Traverse list (Print list)
Before we implement actual operations, first we need to setup empty list. First perform the following steps
before implementing actual operations.
 Step 1: Include all the header files which are used in the program.
 Step 2: Declare all the user defined functions.
 Step 3: Define a Node structure with two members data and next
 Step 4: Define a Node pointer 'head' and set it to NULL.
 Step 4: Implement the main method by displaying operations menu and make suitable function
calls in the main method to perform user selected operation.
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the single linked list...
 Step 1: Create a newNode with given value.
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set newNode→next = NULL and head =newNode.
 Step 4: If it is Not Empty then, set newNode→next = head and head =newNode.
Inserting At End of the list
We can use the following steps to insert a new node at end of the single linked list...
 Step 1: Create a newNode with given value and newNode → next as NULL.
 Step 2: Check whether list is Empty (head == NULL).
 Step 3: If it is Empty then, set head = newNode.
 Step 4: If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5: Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
 Step 6: Set temp → next = newNode.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the single linked list...
 Step 1: Read the position to insert the node.
 Step 2: Initialize c to 1 and assign head to p.
 Step 3: Move upto the position where we want to insert the newNode by assigning p->next to p and
incrementing c.
 Step 4: Create a newNode with given value.
 Step 5: Finally, Set ‘newNode->next=p->next’ and ‘p->next=newNode’;
Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the single linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
 Step 3: If it is Not Empty define a Node pointer 'temp' and initialize with head. then, Check
whether list has only one Node (temp → next == NULL)
 Step 4: set head = NULL and delete temp.
 Step 5: If list has more than one Node set head = temp → next, and delete temp.
Deleting from End of the list
We can use the following steps to delete a node from end of the single linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
 Step 3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
 Step 4: Check whether list has only one Node (temp1 → next == NULL)
 Step 5: If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function.
(Setting Empty list condition)
 Step 6: If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the
same until it reaches to the last node in the list. (until temp1 → next == NULL)
 Step 7: Finally, Set temp2 → next = NULL and delete temp1.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the single linked list...
 Step 1: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
 Step 2: Read the position to delete the node
 Step 3: Initialize c to 1 and assign head to p.
 Step 3: Move upto the position-1 where we want to delete the node by assigning p->next to p and
incrementing c.
 Step 4: Finally, Set ‘p->next=temp->next’;
 Step 5: Delete the node by free(temp);
Traversing / Displaying a Single Linked List
We can use the following steps to display the elements of a single linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!!' and terminate the function.
 Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4: Keep displaying temp → data with an arrow (--->) until temp reaches to the last node
 Step 5: Finally display temp → data with arrow pointing to NULL (temp → data ---> NULL).
Searching an element in SLL:
Circular Linked List
What is Circular Linked List?
In single linked list, every node points to its next node in the sequence and the last node points NULL. But
in circular linked list, every node points to its next node in the sequence but the last node points to the
first node in the list. Circular linked list is a sequence of elements in which every element has link to its
next element in the sequence and the last element has a link to the first element in the sequence.
That means circular linked list is similar to the single linked list except that the last node points to the first
node in the list
Example

Operations
In a circular linked list, we perform the following operations...
1. Insertion
2. Deletion
3. Display
Before we implement actual operations, first we need to setup empty list. First perform the following steps
before implementing actual operations.
 Step 1: Include all the header files which are used in the program.
 Step 2: Declare all the user defined functions.
 Step 3: Define a Node structure with two members data and next
 Step 4: Define a Node pointer 'head' and set it to NULL.
 Step 4: Implement the main method by displaying operations menu and make suitable function
calls in the main method to perform user selected operation.
Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the circular linked list...
 Step 1: Create a newNode with given value.
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set head = newNode and newNode→next =head .
 Step 4: If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'.
 Step 5: Keep moving the 'temp' to its next node until it reaches to the last node (until 'temp →
next == head').
 Step 6: Set 'newNode → next =head', 'head = newNode' and 'temp → next= head'.
Inserting At End of the list
We can use the following steps to insert a new node at end of the circular linked list...
 Step 1: Create a newNode with given value.
 Step 2: Check whether list is Empty (head == NULL).
 Step 3: If it is Empty then, set head = newNode and newNode → next =head.
 Step 4: If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5: Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next == head).
 Step 6: Set temp → next = newNode and newNode → next = head.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the circular linked list...
 Step 1: Read the position to insert the node.
 Step 2: Assign head to q, temp.
 Step 3: Move upto the position where we want to insert the newNode by assigning q->next to q
 Step 4: Create a newNode with given value.
 Step 5: Finally, Set ‘newNode->next=q->next’ and ‘q->next=newNode’;
Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the circular linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
 Step 3: If it is Not Empty define a Node pointer 'temp' and initialize with head. then, Check
whether list has only one Node (temp → next == head)
 Step 4: set head = NULL and delete temp.
 Step 5: If list has more than one Node set head = temp → next, and delete temp.

Deleting from End of the list


We can use the following steps to delete a node from end of the circular linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
 Step 3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
 Step 4: Check whether list has only one Node (temp1 → next == head)
 Step 5: If it is TRUE. Then, set head = NULL and delete temp1. And terminate from the function.
(Setting Empty list condition)
 Step 6: If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the
same until temp1 reaches to the last node in the list. (until temp1 → next == head)
 Step 7: Set temp2 → next = head and delete temp1.
Deleting a Specific Node from the list
We can use the following steps to delete a specific node from the circular linked list...
 Step 1: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
 Step 2: Read the position to delete the node
 Step 3: Initialize c to 1 and assign head to p.
 Step 3: Move upto the position-1 where we want to delete the node by assigning p->next to p and
incrementing c.
 Step 4: Finally, Set ‘p->next=temp->next’;
 Step 5: Delete the node by free(temp);

Displaying a circular Linked List


We can use the following steps to display the elements of a circular linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty, then display 'List is Empty!!!' and terminate the function.
 Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4: Keep displaying temp → data with an arrow (--->) until tempreaches to the last node
 Step 5: Finally display temp → data with arrow pointing to head → data.

Circular Doubly Linked List


• A circular doubly linked list or a circular two way linked list is a more complex type of linked list
which contains a pointer to the next as well as previous node in the sequence.
• The difference between a doubly linked and a circular doubly linked list is same as that exists
between a singly linked list and a circular linked list. The circular doubly linked list does not
contain NULL in the previous field of the first node and the next field of the last node. Rather, the
next field of the last node stores the address of the first node of the list, i.e;”head”. Similarly, the
previous field of the first field stores the address of the last node.
Header Linked List
 The header linked list is a list that has a header node at the beginning of the linked list. and the
head always refers to the header node of the list.
 The info part of the header node is always None.
 Therefore we can use this part to store some useful information about the linked list. like storing
the number of nodes in a list and some of the value of linked list etc.
 The linked part of the header node is always referred to as the first node of the linked list.
 Types of Header linked list
Grounded header linked
list. Circular header linked
list.
 Grounded header linked list which stores NULL in the next field of the last node

 Circular header linked list which stores the address of the header node in the next field of the last
node. Here, the header node will denote the end of the list.

You might also like