You are on page 1of 4

Algorithm to segregate even and odd nodes of list

1. Find the end of list, call it initialEnd


2. Till first even node is found,
2.a move odd nodes to end
3. Set head as first even node as head of list.
4. Scan through remaining nodes of list
4.a If node is odd, move to end
4.b If node is even, skip it
Since this loop will work till initialEnd node, this node will not be
processed in loop.
5. Save next of initialEnd, it will head of odd list
5. Special case of end node, if node is even, skip it.
If node is odd, move to end.

Algorithm to reverse doubly linked list


1. Check if there is no node or only one node, return
2. While there is node in ddl,
2.a Save previous node of current node prev = current->prev
2.b Save next node of current node next = current->next
2.c Change current node's next to point to prev current->next = prev
2.d Change current node's prev to point to next current->prev = next
2.e move current to next node. current = next
3. Change headRef to point to last current node.

Insertion in Doubly linked list


There are four place a new node can be inserted into DLL.

1. Head of list
2. Tail of list
3. Before a given node
4. After a given node

1. Insert in doubly linked list : At front

To insert a node at head, make sure that head pointer is change after insertion. Also in DLL,
previous pointer of last head node points to new node, next of new node points to last head node
and previous of new node points to NULL. Below these steps are clarified:

1. Create a new node


2. Make next of new node to point to old head
3. Make previous of old head to point to new node
4. Change headRef to point to new node
5. Make previous of new node as NULL

2. Insert in doubly linked list : At end

To insert at end, we obviously need to traverse to end of list. There is special case that needs to
be handle, that is if this is first node of doubly linked list. Extra thing to do is point previous of
first node to NULL.
1. Create new node with data
2. Check if this is first node, make previous to point to NULL, change
headRef
3. Else traverse till last node of list.
4. Make next of last node point to new node.
5. Make previous of new node point to last node.
6. Make next of new node point to NULL

3. Insert in doubly linked list : After a given node

This is very similar case to insert at the end. Only thing not to be done is to check if this is first
node.Rest all remains same.

1. Create new node with data


2. Save next of givenNode (nextNode)
3. Make next of givenNode point to new node.
4. Make previous of nextNode to point new node
5. Make previous of new node point to givenNode.
6. Make next of new node point to nextNode

4. Insert in doubly linked list : Before a given node

Again similar case to 3, only next and previous pointers to point to correct nodes.

1. Create new node with data


2. Save previous of givenNode (prevNode)
3. Make next of new Node point to givenNode
4. Make previous of givenNode point to new node.
4. Make next of prevNode point to new node.
6. Make previous of new node to point to prevNode

Insertion in circular linked list


Circular linked list and applications

Circular linked lists applications

In last few posts, we discussed about singly and doubly linked lists. This post is about circular
linked list. Singly linked list is a list where each node points to its next node, there is no link
from a node to previous, last node points to NULL node. Additional pointer needs to be kept in
order to traverse linked list from start. Example of singly linked list given below:

What is circular linked list?


Circular linked list is a singly linked list with an additional thing. In circular linked list, next
pointer of last node does not point to NULL. Next of last node of list again points to the head
node of list. This in turn means there is no end node in circular linked list. Example of circular
linked list is shown below:

How to find what is head node of circular linked list? There are two ways :

1.Keeping an external pointer as in case of singly linked list, which points to head node.

2. Keeping a dummy node, header. This node is separated from regular nodes of list by keeping a
SENTINEL value in it. Other way is to have a flag to indicate that it is a head node.

Insertion in circular linked list


Node definition of circular list is same as singly linked list. There are three cases when where we
can insert a node in circular linked list.

1. Insert at start of list.

We have an extra pointer which points to head node of the list. When node is added at front of
list, that pointer is to be changed. Also, in circular linked list, last node points to head node,
hence that link also should be changed.

1. Create a new node.


2. If this if first node, point it's next to itself
head->next = head
3. Else, scan list and go to last node while(current != *headRef)
4. Change next node of new node to point to earlier head.
newNode->next = *headRef
5. Change head to point to new node.
*headRef = newNode
6. Change next pointer of last node to point to new node
lastNode->next = newNode

Since head node is changing, function call will be headRef and not head pointer only. Below is
code for above algorithm to add a node at start of circular linked list

2. Insert at the middle of circular linked list.

This insertion is same as singly linked list, as there is no change in head or tail of circular list.
Just scan to the required position, add new node.

next = prev->next;
prev->next = newNode;
newNode->next = next;

3. Insert at the end of circular linked list


This case is very similar to above case, only difference will be that next pointer of last node will
be head node. No implementation change.

Delete node from circular linked list

To remove a node, care should be taken when removing head or last nodes. When last node is
removed, previous to last node should now start pointing to head node. When head node is
removed, last node should now be pointing to new head node. Below is code to remove node.

Applications of circular linked lists


Implementation of waiting and context switch queues in operating system. When there
are multiple processes running on operating system and there is mechanism to provide
limited time slots for each process, waiting process can form a circular linked list. Task at
head of list is given CPU time, once time allocated finishes, task is taken out and added
to list again at the end and it continues.
Circular linked list is useful in implementation of queues using lists. Remember in post:
Stack and Queue implementation using linked list, we had two pointers, one to point to
head and other to point to end of list, because in queue, addition happens at end and
removal at head. With circular list, that can be done using only one pointer.

You might also like