Module -2 Updated
Module -2 Updated
MODULE- 2
Single Linked List
Circular Linked List
Double Linked List
Circular Double Linked List
Applications of Linked List
Sparse Matrix Representation and its performance analysis
Addition of Polynomials and its performance analysis
• There are various linked list operations that allow us to perform different
actions on linked lists.
• For example, the insertion operation adds a new element to the linked list.
When temp is NULL, we know that we have reached the end of the linked list so we
get out of the while loop.
You can add elements to either the beginning, middle or end of the linked list.
}
Delete from a Linked List
You can delete either from the beginning, end or from a particular position.
}
3. Delete from middle //java
Traverse to element before the element to be deleted public Node deleteMid()
Change next pointers to exclude the node from the chain {
if(pos == 1)
{
// c head = head.next;
for(int i=2; i< position; i++) { }
if(temp->link!=NULL) { else
temp = temp->link; {
} Node temp = head;
} int count = 1;
while(count < pos-1)
{
temp->link = temp->link->link;
temp = temp.next;
count++;
}
Node temp1 = temp.next;
temp.next = temp1.next;
}
search an element
To search an element in a Linked List, we need to traverse the entire Linked List.
Compare each node with the data to be search and continue until a match is
found.
Begin the search process from the first node as random access is not possible in
a Linked List.
}
Public DoublyLinkedList()
{
this.head = null;
this.tail = null;
this.length = 0;
}
}
}
}
DATA STRUCTURES AND ALGORITHMS | CSSE 68
Deleting a Specific Node
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 a Node pointer 'temp' and initialize with head.
Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the last node.
Step 5 - If it is reached to the last node, then display 'Given node not found in the list! Deletion not possible!!!' and terminate the fuction.
Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node or not
Step 7 - If list has only one node and that is the node which is to be deleted then set head to NULL and delete temp (free(temp)).
Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list (temp == head).
Step 9 - If temp is the first node, then move the head to the next node (head = head → next), set head of previous to NULL (head →
previous = NULL) and delete temp.
Step 10 - If temp is not the first node, then check whether it is the last node in the list (temp → next == NULL).
Step 11 - If temp is the last node then set temp of previous of next to NULL (temp → previous → next = NULL) and delete temp
(free(temp)).
Step 12 - If temp is not the first node and not the last node, then set temp of previous of next to temp of next (temp → previous → next =
temp → next), temp of next of previous to temp of previous (temp → next → previous = temp → previous) and delete temp (free(temp)).
ptr1.next = null;
ptr1.prev = ptr2;
while(ptr2 != null)
{
ptr2.prev = ptr2.next;
ptr2.next = ptr1;
ptr1 = ptr2;
ptr2 = ptr2.prev;
}
head = ptr1;
return head;
}
Node(int data)
{
this.data = data;
this.next = null;
}
}
{ }
printf("\nEmpty List\n"); else
} {
else flag=0;
{ }
printf("\nEnter item which you want to search?\n");
i++;
scanf("%d",&item);
ptr = ptr -> next;
if(head ->data == item)
}
{
}
printf("item found at location %d",i+1);
if(flag != 1)
flag=1;
{
}
printf("Item not found\n");
else
{ }
Array Representation
Linked Representation
Output:
0 0 1 1 3 3
2 4 2 3 1 2
7 9 5 7 2 3
{0 , 0 , 3 , 0 , 4 },
{0 , 0 , 5 , 7 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 2 , 6 , 0 , 0 }
void pop() {
if(top == null){
sop("\nStack is Empty!!!\n");
} else {
Node temp = top;
Sop("\nDeleted element:", temp.data);
top = temp.next;
}
}
void display() {
if(top == null) {
Sop("\nStack is Empty!!!\n");
} else {
Node temp = top;
while(temp != null){
Sop("--->“ temp.data);
temp = temp.next;
}
Sop(“NULL”);
}
}
Queue Using Linked List
The major problem with the queue implemented using an array is, It will
work for an only fixed number of data values.
That means, the amount of data must be specified at the beginning itself.
Queue using an array is not suitable when we don't know the size of data
which we are going to use.
A queue data structure can be implemented using a linked list data structure.
The queue which is implemented using a linked list can work for an unlimited
number of values.
In linked list implementation of a queue, the last inserted node is always
pointed by 'rear' and the first node is always pointed by 'front’.
The order of elements inserted is 10, 15, 22 and 50.
To implement queue using linked list, we need to set the following things
before implementing actual operations.
•Step 1 - Include all the header files which are used in the program. And
declare all the user defined functions.
•Step 2 - Define a 'Node' structure with two members data and next.
•Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.
•Step 4 - Implement the main method by displaying Menu of list of
operations and make suitable function calls in the main method to perform
user selected operation.
enQueue(value) - Inserting an element into the Queue
Step 1 - Create a newNode with given value and set 'newNode → next' to
NULL.
Step 2 - Check whether queue is Empty (rear == NULL)
Step 3 - If it is Empty then, set front = newNode and rear = newNode.
Step 4 - If it is Not Empty then, set
rear → next = newNode
rear = newNode.
deQueue() - Deleting an Element from Queue