DSA Lec 15+16 (Linked List Part 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 87

1

CSC331- Data Structure & Algorithms

Lahore Garrison University


Lec15+16
Instructor: Sahar Moin
Preamble (Past Lesson Brief)
• Linked List
• Types of Linked List

2 Lahore Garrison University


Doubly Linked List

• A doubly linked list is a linear data structure, in which the elements are stored
in the form of a node. Each node contains three sub-elements.
• A data part that stores the value of the element, the previous part that stores
the pointer to the previous node, and the next part that stores the pointer to the
next node
Doubly Linked List
• The first node also known as HEAD is always used as a reference to traverse
the list.
• The previous of head node and next of last node points to NULL.
• A doubly linked list can be visualized as a chain of nodes, where every node
points to previous and next node.
Implementation of Doubly Linked List

//node structure class LinkedList {


public:
struct Node { Node* head;
public:
int data;
//constructor to create an empty LinkedList
Node* next; LinkedList(){
head = NULL;
Node* prev; }
};
};
Traverse a Doubly Linked List
void PrintList() {
Node* temp = head;
if(temp != NULL) {
cout<<"The list contains: ";
while(temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next; }
cout<<endl;
} else {
cout<<"The list is empty.\n";
}
Run Code
Insert a new node at the start of the Doubly Linked List
• In this method, a new node is inserted at the start of the doubly linked list.
• For example - if the given List is 10->20->30 and a new element 100 is
added at the start, the List becomes 100->10->20->30.
• Inserting a new node at the start of the doubly linked list is very easy.
• First, a new node with given element is created.
• It is then added at the start of the list by linking the head node to the new
node.
The function push_front is created for this
purpose. It is a 5-step process
void push_front(int newElement) { //4. Check the list is empty or not,
// if empty make the new node as head
//1. allocate node if(head == NULL) {
Node* newNode = new Node(); head = newNode;
//2. assign data element } else {
newNode->data = newElement;
//5. Adjust the links and make the new
//3. assign null to the next and prev // node as head
// of the new node head->prev = newNode;
newNode->next = head;
newNode->next = NULL;
head = newNode;
newNode->prev = NULL; }
Run the code
Insert a new node at the end of the Doubly Linked List

• In this method, a new node is inserted at the end of the doubly linked list.
• For example - if the given List is 10->20->30 and a new element 100 is added
at the end, the List becomes 10->20->30->100.
• Inserting a new node at the end of the doubly linked list is very easy. First, a
new node with given element is created. It is then added at the end of the list
by linking the last node to the new node.
The function push_back is created for this purpose.
It is a 6-step process.
//4. Check the list is empty or not,
// if empty make the new node as head
void push_back(int newElement) { if(head == NULL) {
//1. allocate node head = newNode;
Node* newNode = new Node(); } else {
//2. assign data element
//5. Else, traverse to the last node
newNode->data = newElement; Node* temp = head;
//3. assign null to the next and prev while(temp->next != NULL)
temp = temp->next;
// of the new node
newNode->next = NULL; //6. Adjust the links
newNode->prev = NULL; temp->next = newNode;
newNode->prev = temp;
}
}
Insert a new node at a given position in the
Doubly Linked List
• In this method, a new element is inserted at the specified position in the
doubly linked list.
• For example - if the given list is 10->20->30 and a new element 100 is added
at position 2, the list becomes 10->100->20->30.
• First, a new node with given element is created.
• If the insert position is 1, then the new node is made to head.
• Otherwise, traverse to the node that is previous to the insert position and
check if it is null or not.
• In case of null, the specified position does not exist.
• In other case, update the links. The below figure describes the process, if the
insert node is other than the head node
The function push_at is created for this purpose. It is
a 6-step process.
//1. allocate node to new element
//2. check if the position is > 0
//3. if the position is 1, make new node as head
//4. Else, make a temp node and traverse to the
// node previous to the position
//5. If the previous node is not null, adjust
// the links
//6. When the previous node is null append the node at the end
The function push_at is created for this purpose. It is
a 6-step process
void push_at(int newElement, int position) { //3. if the position is 1, make new node as head
newNode->next = head;
//1. allocate node to new element head->prev = newNode;
Node* newNode = new Node(); head = newNode;
} else {
newNode->data = newElement;
newNode->next = NULL; //4. Else, make a temp node and traverse to
// the node previous to the position
newNode->prev = NULL; Node* temp = head;
for(int i = 1; i < position-1; i++) {
//2. check if the position is > 0
if(temp != NULL) {
if(position < 1) { temp = temp->next;
}
cout<<"\nposition should be >= 1.";
}
} else if (position == 1) {
The function push_at is created for this purpose. It is
a 6-step process
//5. If the previous node is not null, adjust the links
if(temp != NULL) {
newNode->next = temp->next;
newNode->prev = temp;
temp->next = newNode;
if(newNode->next != NULL)
newNode->next->prev = newNode;
} else {
//6. When the previous node is null
cout<<"\nThe previous node is null.";} }}
Run Code to insert a node at
nth position in a doubly linked
list
Delete the first node of the Doubly Linked List

• In this method, the first node of the doubly linked list is deleted. For example
- if the given list is 10->20->30->40 and the first node is deleted, the list
becomes 20->30->40.
• Deleting the first node of the Doubly Linked List is very easy.
• If the head is not null then create a temp node pointing to head and move
head to the next of head.
• Then delete the temp node.
• If the new head is not null,
make the prev of it as null.
The function pop_front is created for this purpose.
It is a 4-step process.

//3. delete temp node


void pop_front() {
delete(temp);
if(head != NULL) {
//1. if head is not null, create a //4. If the new head is not null, then
// make prev of the new head as null
// temp node pointing to head
if(head != NULL)
Node* temp = head; head->prev = NULL;
//2. move head to next of head
}

head = head->next;
head
}
Delete the last node of the Doubly Linked List
• In this method, the last node of the doubly linked list is deleted. For example
- if the given list is 10->20->30->40 and the last node is deleted, the list
becomes 10->20->30.
• Deleting the last node of the Doubly Linked List involves checking the head
for empty.
• If it is not empty, then check the head next for empty.
• If the head next is empty, then release the head, else traverse to the second
last node of the list.
• Then, link the next of second last node to NULL and delete the last node.
The function pop_back is created for this purpose. It
is a 3-step process.
void pop_back() {
//3. Change the next of the second
if(head != NULL) {
// last node to null and delete the
//1. if head in not null and next of head // is null, release the // last node
head
Node* lastNode = temp->next;
if(head->next == NULL) {
temp->next = NULL;
head = NULL; delete(lastNode);
} else { }
//2. Else, traverse to the second last element of the list }
Node* temp = head;
}
while(temp->next->next != NULL)

temp = temp->next;
Delete a node at the given position in the Doubly Linked List
• In this method, a node at the specified position in the doubly linked list is
deleted. For example - if the given list is 10->20->30 and the 2nd node is
deleted, the list becomes 10->20.
• First, the specified position must be greater than equal to 1.
• If the specified position is 1 and head is not null, then make the head next as
head and delete the previous head.
• Else, traverse to the node that is previous to the specified position.
• If the specified node and previous to the specified node are not null then
adjust the link. In other case, the specified node will be already null.
The function pop_at is created deletion at nth
position. It is a 5-step process
//1. check if the position is > 0
//2. if the position is 1 and head is not null, make head next as head
and delete previous head
//3. Else, make a temp node and traverse to the node previous to the
position
//4. If the previous node and next of the previous is not null, adjust
links
//5. Else the given node will be empty.
//3. Else, make a temp node and traverse to the
void pop_at(int position) { // node previous to the position
//1. check if the position is > 0 Node* temp = head;
for(int i = 1; i < position-1; i++) {
if(position < 1) { if(temp != NULL) {
temp = temp->next;} }
cout<<"\nposition should be >= 1.";
} else if (position == 1 && head != NULL) { //4. If the previous node and next of the previous
// is not null, adjust links
//2. if the position is 1 and head is not null, make
if(temp != NULL && temp->next != NULL) {
// head next as head and delete previous head Node* nodeToDelete = temp->next;
temp->next = temp->next->next;
Node* nodeToDelete = head; if(temp->next->next != NULL)
head = head->next; temp->next->next->prev = temp->next;
delete(nodeToDelete);
delete(nodeToDelete); } else {
if(head != NULL)
//5. Else the given node will be empty.
head->prev = NULL; cout<<"\nThe node is already null.";} }}
} else {
Delete all nodes of the Doubly Linked List
• Deleting all nodes of a doubly linked list requires traverse through the list and
deleting each node one by one.
• It requires creating a temp node pointing to the head then move the head to
head next.
• After that delete the temp node.
• Repeat the process till the head becomes null.
Delete all nodes of the Doubly Linked List
The function deleteAllNodes is created for this purpose. It is a 2-step process.
void deleteAllNodes() {
//1. create a temp node
Node* temp = new Node();
//2. if the head is not null make temp as head and move head to head next, then delete the temp,
continue the process till head becomes null
while(head != NULL) {
temp = head;
head = head->next;
delete(temp); }
cout<<"All nodes are deleted successfully.\n"; }
Lab Tasks (Internal Marks)
There is doubly linked list containing n elements i.e. 10>20>30>40
1. Insert an element at the nth position
2. Delete an element at the 3rd position
3. Search in the doubly linked list if element 30 is present or not
Search an element in the Doubly Linked List

• Searching an element in a doubly linked list requires creating a temp node


pointing to the head of the list.
• Along with this, two more variables are required to track search and track
index of the current node.
• If the temp node is not null at the start, then traverse the list to check if
current node value matches with the search value.
• If it matches then update search tracker variable and stop traversing the list,
else keep on traversing the list.
• If the temp node is empty at the start, then the list contains no item.
void SearchElement(int searchValue) { if(temp != NULL) {
while(temp != NULL) {
//1. create a temp node pointing to head i++;
if(temp->data == searchValue) {
Node* temp = head; found++;
break;
//2. create two variables: found - to track }
temp = temp->next;
// search, idx - to track current index }
if (found == 1) {
int found = 0; cout<<searchValue<<" is found at index =
"<<i<<".\n";
int i = 0; } else {
cout<<searchValue<<" is not found in the list.\
//3. if the temp node is not null check the }
} else {
// node value with searchValue, if found
//4. If the temp node is null at the start,
// update variables and break the loop, else // the list is empty
cout<<"The list is empty.\n";
// continue searching till temp node is not null }
}
Circular Singly Linked List
Circular Singly Linked List
• A circular singly linked list is a linear data structure, in which the elements
are stored in the form of a node.
• Each node contains two sub-elements.
• A data part that stores the value of the element and
• the next part that stores the pointer to the next node.
Circular Singly Linked List
• The first node also known as HEAD (LAST) is always used as a reference to
traverse the list.
• The last node points to HEAD. (LAST)
• A circular singly linked list can be visualized as a chain of nodes, where
every node points to the next node. Along with this, next of the last node is
linked to the head node.
Implementation of Circular Singly Linked List
//node structure
struct Node {
int data;
Node* next;
};
class LinkedList {
public:
Node* head;
public:
//constructor to create an empty LinkedList
LinkedList(){
head = NULL;}};
Implementation of Circular Singly Linked List
//node structure // test the code
int main() {
struct Node { //create an empty LinkedList
LinkedList MyList; //Add third node.
int data; //Add first node. Node* third = new Node();
Node* first = new Node(); third->data = 30;
Node* next; first->data = 10; //linking with second node
}; //linking with head node
second->next = third;
MyList.head = first;
class LinkedList { //linking next of the node with //linking next of the node with
head //head
public: first->next = MyList.head; third->next = MyList.head;
//Add second node.
Node* head; Node* second = new Node();
return 0;
public: second->data = 20;
//linking with first node }
LinkedList(){ first->next = second;
//linking next of the node with
head = NULL;}}; //head
second->next = MyList.head;
Traverse a Circular Singly Linked List
• A circular singly linked list can be traversed from any node of the list using a
temp node.
• Keep on moving the temp node to the next one and displaying its content.
Stop the traversal, after reaching the starting node.
//display the content of the list

void PrintList() {

Node* temp = head;


Traverse a Circular if(temp != NULL) {
Singly Linked List
cout<<"The list contains: ";

while(true) {

cout<<temp->data<<" ";

temp = temp->next;

if(temp == head)

break; }

cout<<endl;

} else {

cout<<"The list is empty.\n"; }


Implementation of Circular Singly Linked List
//node structure // test the code
int main() {
struct Node { //create an empty LinkedList
LinkedList MyList; //Add third node.
int data; //Add first node. Node* third = new Node();
Node* first = new Node(); third->data = 30;
Node* next; first->data = 10; //linking with second node
}; //linking with head node
second->next = third;
MyList.head = first;
class LinkedList { //linking next of the node with //linking next of the node with
head //head
public: first->next = MyList.head; third->next = MyList.head;
//Add second node. //print the content of list
Node* head; Node* second = new Node();
MyList.PrintList();
public: second->data = 20;
//linking with first node return 0;
LinkedList(){ first->next = second; }
//linking next of the node with
head = NULL;}}; head
second->next = MyList.head;
Insertion in Circular
Singly Linked List
Insertion in Circular Linked List
A node can be added in three ways:
• Insertion in an empty list
• Insertion at the beginning of the list
• Insertion at the end of the list
• Insertion in between the nodes
Implementation
• A circular singly linked list is a linear data structure, in which the elements
are stored in the form of a node. Each node contains two sub-elements. A data
part that stores the value of the element and the next part that stores the
pointer to the next node
• The first node also known as HEAD is always used as a reference to traverse
the list. The last node points to HEAD. A circular singly linked list can be
visualized as a chain of nodes, where every node points to the next node.
Along with this, next of the last node is linked to the head node.
Implementation of Circular Singly Linked List
//node structure
struct Node {
int data;
Node* next;
};
class LinkedList {
public:
Node* head;
public:
//constructor to create an empty LinkedList
LinkedList(){
head = NULL;};
Circular Singly Linked List Traversal
• Traversing through a circular singly linked list is very easy. It requires
creating a temp node pointing to the head of the list.
• If the temp node is not null, display its content and move to the next node
using temp next.
• Repeat the process till the temp node reaches the starting node (head of the
list). If the temp node is empty at the start, then the list contains no item.
• A circular singly linked list can be traversed from any node as the starting
point. Traverse the list till reaches the same node.
void PrintList() {
//1. create a temp node pointing to head
Node* temp = head; if(temp == head)
//2. if the temp node is not null continue break; }
cout<<endl;
// displaying the content and move to the
}
// next node till the temp becomes head else {
if(temp != NULL) {
//3. If the temp node is null at the start,
cout<<"The list contains: "; // the list is empty
while(true) { cout<<"The list is empty.\n";
}
cout<<temp->data<<" ";
}
temp = temp->next;
Insert a new node at the start of the Circular Singly
Linked List
• In this method, a new node is inserted at the start of the circular singly linked
list. For example - if the given List is 10->20->30 and a new element 100 is
added at the start, the List becomes 100->10->20->30.
• Inserting a new node at the start of the circular singly linked list is very easy.
First, a new node with given element is created. It is then added at the start of
the list by linking the head node and last node to the new node.
void push_front(int newElement) {
//1. allocate node //5. Else, traverse to the last node
Node* newNode = new Node(); Node* temp = head;
//2. assign data element while(temp->next != head)
newNode->data = newElement; temp = temp->next;
//3. assign null to the next of new node
newNode->next = NULL; //6. Adjust the links
//4. Check the list is empty or not, temp->next = newNode;
// if empty make the new node as head newNode->next = head;
if(head == NULL) { head = newNode;
head = newNode;
}
newNode->next = head;
}
} else {
Insert a new node at the end of the Circular
Singly Linked List
• In this method, a new node is inserted at the end of the circular singly linked
list. For example - if the given List is 10->20->30 and a new element 100 is
added at the end, the List becomes 10->20->30->100.
• Inserting a new node at the end of the circular singly linked list is very easy.
First, a new node with given element is created. It is then added at the end of
the list by linking the last node and head node to the new node.
void push_back(int newElement) {
//1. allocate node
//5. Else, traverse to the last node
Node* newNode = new Node();
Node* temp = head;
//2. assign data element
while(temp->next != head)
newNode->data = newElement; temp = temp->next;
//3. assign null to the next of new node
newNode->next = NULL; //6. Adjust the links
//4. Check the list is empty or not, temp->next = newNode;
// if empty make the new node as head newNode->next = head;
if(head == NULL) {
}
head = newNode;
}
newNode->next = head;
} else {
Insert a new node at a given
position in the Circular Singly Linked List
• In this method, a new element is inserted at the specified position in the circular
singly linked list. For example - if the given list is 10->20->30 and a new element
100 is added at position 2, the list becomes 10->100->20->30.
• First, create two nodes:
• 1. newNode with given element and
• 2. temp to traverse through the list.
• After that count the number of elements in the list to check whether the insertion
position is valid or not (It must lie in the range of [1, n + 1], where n is number of
elements in the list).
• If the insertion position is valid and equal to 1 then make the newNode as head and
adjust links accordingly. If the insertion position is valid and greater than 1 then
traverse to the specified position and insert the newNode and adjust links
accordingly.
The function push_at is created for this purpose. It is a 5-step
process.
void push_at(int newElement, int position) { //3. check if the insertion position is valid
if(position < 1 || position > (NoOfElements+1)) {
//1. allocate node to new element and create a temp node to
//traverse the list cout<<"\nInavalid position.";
} else if (position == 1) {
Node* newNode = new Node();
//4. if the position is 1, make next of the
newNode->data = newElement; // new node as head and new node as head
newNode->next = NULL; if(head == NULL) {
head = newNode;
Node* temp = head;
head->next = head;
int NoOfElements = 0; } else {
//2. Find the number of elements in the list while(temp->next != head) {
temp = temp->next;
if(temp != NULL) {
}
NoOfElements++; newNode->next = head;
temp = temp->next;}
head = newNode;
temp->next = head;
while(temp != head) {
}
NoOfElements++;

temp = temp->next;}
} else {
//5. Else, traverse to the node previous to
// the given position, make newNode next
// as temp next and temp next as newNode.
temp = head;
for(int i = 1; i < position-1; i++)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode; }}
• In this method, the first node of the circular singly linked list is deleted. For
example - if the given list is 10->20->30->40 and the first node is deleted, the
list becomes 20->30->40.
• Deleting the first node of the circular singly linked list is very easy. If the list
contains one node, delete the node.
• If the list contains more than one node,
• then create two nodes - temp and firstNode both pointing to the head.
• Using temp node, traverse to the last node of the list. Make next of head as
the head node and next of last node as head. Finally, delete the first node.
The function pop_front is created for this purpose. It is
a 4-step process.
void pop_front() {
//3. using temp node, traverse to the last node
if(head != NULL) { while(temp->next != head) {
//1. the list contains one node, delete temp = temp->next;
// make the head null }
if(head->next == head) {
//4. Make head as next of head,
head = NULL; // Make next of last node as head,
} else { // delete the firstNode
head = head->next;
//2. if the list contains more than one node,
temp->next = head;
// create two nodes - temp and firstNode, both free(firstNode);
// pointing to head node }
Node* temp = head;
}
}
Node* firstNode = head;
Delete the last node of the Circular Singly Linked List

• In this method, the last node of the circular singly linked list is deleted. For
example - if the given list is 10->20->30->40 and the last node is deleted, the
list becomes 10->20->30.
• Deleting the last node of the Circular Singly Linked List involves checking
the head for empty. If it is not empty and contains only one node then delete
the head node. If the list contains more than one node then traverse to the
second last node of the list and link it with the head. Finally, delete the last
node.
void pop_back() {

if(head != NULL) {

//1. if head in not null and next of head

// is head, release the head


//3. Change the next of the second
// last node to head and delete the
if(head->next == head) {
// last node
head = NULL; Node* lastNode = temp->next;
} else { temp->next = head;
//2. Else, traverse to the second last
free(lastNode);
}
// element of the list
}
Node* temp = head; }
while(temp->next->next != head)

temp = temp->next;
Delete a node at the given position in the Circular Singly
Linked List
• In this method, a node at the specified position in the circular singly linked list is
deleted. For example - if the given list is 10->20->30 and the 2nd node is deleted,
the list becomes 10->20.
• First, create two nodes temp and nodeToDelete to traverse through the list and track
the node to delete respectively. After that count the number of elements in the list to
check whether the specified position is valid or not (It must lie in the range of [1, n],
where n is number of elements in the list). If the specified valid position is 1 and
head is the only element in the list, then make the head as null. If the specified valid
position is 1 and list contains more than one elements, then make next of head as
new head and adjust links accordingly. If the specified valid position is greater than
1 then traverse to the node previous to the given position and delete the given node
and adjust links accordingly.
void pop_at(int position) {
//1. create two nodes - temp and nodeToDelete //3. check if the specified position is valid
if(position < 1 || position > NoOfElements) {
// to traverse and track the node to delete cout<<"\nInavalid position.";
Node* nodeToDelete = head; } else if (position == 1) {

Node* temp = head; //4. if the position is 1 and head is the only element
// in the list, then make it null, else make next
int NoOfElements = 0;
// of head as new head and adjust links accordingly
//2. Find the number of elements in the list if(head->next == head) {
head = NULL;
if(temp != NULL) { } else {
NoOfElements++; while(temp->next != head)
temp = temp->next;
temp = temp->next;} head = head->next;
temp->next = head;
while(temp != head) { free(nodeToDelete);
NoOfElements++; }
} else {
temp = temp->next;}
//5. Else, traverse to the node previous to
// the given position and delete the given
// node and adjust links accordingly
temp = head;
for(int i = 1; i < position-1; i++)
temp = temp->next;
nodeToDelete = temp->next;
temp->next = temp->next->next;
free(nodeToDelete); }}
Delete all nodes of the Circular Singly Linked List

• Deleting all nodes of a circular singly linked list requires traverse through the
list and deleting each node one by one. It requires creating a current node
pointing to the next of head. Delete the current node and move to the next
node using temp node. Repeat the process till the current node becomes head.
At last, delete the head
void deleteAllNodes() {
if(head != NULL) {
//3. Delete the head
//1. if head is not null create a temp node free(head);
// and current node pointed to next of head head = NULL;
}
Node *temp, *current;
cout<<"All nodes are deleted successfully.\
current = head->next; }
//2. if current node is not equal to head, delete the
// current node and move current to next node using temp,
// repeat the process till the current reaches the head
while(current != head) {
temp = current->next;
free(current);
current = temp;}
Lab Tasks (Internal Marks)
There is circular linked list containing n elements i.e. 10>20>30>40
Insert an element at the nth position
Delete an element at the 3rd position
Search in the circular linked list if element 30 is present or not
Search an element in the Circular Singly Linked
List
• Searching an element in a circular singly linked list requires creating a temp
node pointing to the head of the list.
• Along with this, two more variables are required to track search and track
index of the current node.
• If the temp node is not null at the start, then traverse the list to check if
current node value matches with the search value.
• If it matches then update search tracker variable and stop traversing the list,
else keep on traversing the list.
• If the temp node is empty at the start, then the list contains no item.
if(temp != NULL) {
void SearchElement(int searchValue) { while(true) {
i++;
//1. create a temp node pointing to head if(temp->data == searchValue) {
Node* temp = head; found++;
break;
//2. create two variables: found - to track }
temp = temp->next;
// search, idx - to track current index
if(temp == head) {break;}
int found = 0; }
if (found == 1) {
int i = 0; cout<<searchValue<<" is found at index =
//3. if the temp node is not null check the "<<i<<".\n";
} else {
// node value with searchValue, if found cout<<searchValue<<" is not found in the list.\n";
}
// update variables and break the loop, else } else {
// continue searching till temp node is not head
//4. If the temp node is null at the start,
// the list is empty
cout<<"The list is empty.\n";}}
Circular Doubly Linked List
Circular Doubly Linked List

• A circular doubly linked list is a linear data structure, in which the elements
are stored in the form of a node.
• Each node contains three sub-elements.
• A data part that stores the value of the element, the previous part that stores
the pointer to the previous node, and the next part that stores the pointer to the
next node
Circular Doubly Linked List
• The first node also known as HEAD is always used as a reference to traverse
the list.
• Last element contains link to the first element as next and the first element
contains link of the last element as previous.
• A circular doubly linked can be visualized as a chain of nodes, where every
node points to previous and next node.
Implementation of Circular Doubly Linked List
//node structure
struct Node {
int data;
Node* next;
Node* prev;
};
class LinkedList {
public:
Node* head;
public:
//constructor to create an empty LinkedList
LinkedList(){
head = NULL;}};
Circular Doubly Linked List Traversal
• Traversing through a circular doubly linked list is very easy. It requires
creating a temp node pointing to the head of the list. If the temp node is not
null, display its content and move to the next node using temp next. Repeat
the process till the temp node reaches the starting node (head of the list). If
the temp node is empty at the start, then the list contains no item.
• A circular doubly linked list can be traversed from any node as the starting
point. Traverse the list till reaches the same node.
• The function PrintList is created for this purpose. It is a 3-step process.
• //1. create a temp node pointing to head
• //2. if the temp node is not null continue displaying the content and move to
the next node till the temp becomes head
• /3. If the temp node is null at the start, the list is empty
void PrintList() {
//1. create a temp node pointing to head
}
Node* temp = head; cout<<endl;
//2. if the temp node is not null continue } else {
// displaying the content and move to the
// next node till the temp becomes head //3. If the temp node is null at the start,
if(temp != NULL) { // the list is empty
cout<<"The list contains: "; cout<<"The list is empty.\n";
while(true) {
}
}
cout<<temp->data<<" ";
temp = temp->next;
if(temp == head)
break;
Insert a new node at the start of the Circular Doubly
Linked List
• In this method, a new node is inserted at the start of the circular doubly linked
list. For example - if the given List is 10->20->30 and a new element 100 is
added at the start, the List becomes 100->10->20->30.
• Inserting a new node at the start of the circular doubly linked list is very easy.
First, a new node with given element is created. It is then added at the start of
the list by linking the head node and last node to the new node
void push_front(int newElement) //4. Check the list is empty or not,
{ // if empty make the new node as head
//1. allocate node if(head == NULL) {
head = newNode;
Node* newNode = new Node(); newNode->next = head;
//2. assign data element newNode->prev = head;
} else {
newNode->data = newElement;
//5. Else, traverse to the last node
//3. assign null to the next and prev
Node* temp = head;
// of the new node while(temp->next != head)
temp = temp->next;
newNode->next = NULL;
newNode->prev = NULL;
//6. Adjust the links
temp->next = newNode;
newNode->prev = temp;
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}
Insert a new node at the end of the Circular Doubly
Linked List

• In this method, a new node is inserted at the end of the circular doubly linked
list. For example - if the given List is 10->20->30 and a new element 100 is
added at the end, the List becomes 10->20->30->100.
• Inserting a new node at the end of the circular doubly linked list is very easy.
First, a new node with given element is created. It is then added at the end of
the list by linking the last node and head node to the new node.
void push_back(int newElement) {
//4. Check the list is empty or not,
//1. allocate node // if empty make the new node as head
if(head == NULL) {
Node* newNode = new Node(); head = newNode;
newNode->next = head;
//2. assign data element newNode->prev = head;
} else {
newNode->data = newElement; //5. Else, traverse to the last node
//3. assign null to the next and prev Node* temp = head;
while(temp->next != head)
// of the new node temp = temp->next;
//6. Adjust the links
newNode->next = NULL; temp->next = newNode;
newNode->next = head;
newNode->prev = NULL; newNode->prev = temp;
head->prev = newNode;} }
Insert a new node at a given position in the
Circular Doubly Linked List
• In this method, a new element is inserted at the specified position in the
circular doubly linked list.
• For example - if the given list is 10->20->30 and
• a new element 100 is added at position 2,
• the list becomes 10->100->20->30.
• First, create two nodes:
• 1. newNode with given element and
• 2. temp to traverse through the list.
• After that count the number of elements in the list to check whether the insertion
position is valid or not (It must lie in the range of [1, n + 1], where n is number of
elements in the list).
• If the insertion position is valid and equal to 1 then make the newNode as head and
adjust links accordingly.
• If the insertion position is valid and greater than 1 then traverse to the specified
position and insert the newNode and adjust links accordingly.
while(temp != head) {
void push_at(int newElement, int position) {
NoOfElements++;
//1. allocate node to new element and temp = temp->next;
// create a temp node to traverse the list
}

Node* newNode = new Node(); //3. check if the insertion position is valid
newNode->data = newElement; if(position < 1 || position >
(NoOfElements+1)) {
newNode->next = NULL; cout<<"\nInavalid position.";
Node* temp = head; } else if (position == 1) {
int NoOfElements = 0; //4. if the position is 1, make next of the
//2. Find the number of elements in the list // new node as head and new node as head
if(head == NULL) {
if(temp != NULL) {
head = newNode;
NoOfElements++; head->next = head;
head->prev = head;
temp = temp->next }
} else {
while(temp->next != head) { } else {

temp = temp->next; //5. Else, traverse to the node previous to


// the given position, make newNode next
} // as temp next and temp next as newNode.
temp->next = newNode; temp = head;
for(int i = 1; i < position-1; i++)
newNode->prev = temp; temp = temp->next;
newNode->next = temp->next;
newNode->next = head;
newNode->next->prev = newNode;
head->prev = newNode; newNode->prev = temp;
temp->next = newNode;
head = newNode; }
} }
Delete the first node of the Circular Doubly Linked List

• In this method, the first node of the circular doubly linked list is deleted. For
example - if the given list is 10->20->30->40 and the first node is deleted, the
list becomes 20->30->40.
• Deleting the first node of the circular doubly linked list is very easy. If the list
contains one node, delete the node. If the list contains more than one node,
then create two nodes - temp and firstNode both pointing to the head. Using
temp node, traverse to the last node of the list. Make next of head as the head
node and update all links. Finally, delete the first node.
void pop_front() {
//3. using temp node, traverse to the last
if(head != NULL) { node
//1. the list contains one node, while(temp->next != head) {
//delete make the head null
temp = temp->next;
}
if(head->next == head) {
head = NULL; //4. Make head as next of head, and
} else { // update links
//2. if the list contains more than one node, head = head->next;
head->prev = temp;
// create two nodes - temp and firstNode, both
temp->next = head;
// pointing to head node free(firstNode);
Node* temp = head; }
Node* firstNode = head; }
}
Delete the last node of the Circular Doubly Linked List

• In this method, the last node of the circular doubly linked list is deleted. For
example - if the given list is 10->20->30->40 and the last node is deleted, the
list becomes 10->20->30.
• Deleting the last node of the Circular Doubly Linked List involves checking
the head for empty. If it is not empty and contains only one node then delete
the head node. If the list contains more than one node then traverse to the
second last node of the list and link it with the head. Finally, delete the last
node.
void pop_back() {
if(head != NULL) {
//1. if head in not null and next of head
// is head, release the head //3. Update links of head and second
// last node, and delete the last node
if(head->next == head) {
Node* lastNode = temp->next;
head = NULL; temp->next = head;
head->prev = temp;
} else {
free(lastNode);
//2. Else, traverse to the second last }
}
// element of the list }
Node* temp = head;
while(temp->next->next != head)
temp = temp->next;
Delete a node at the given position in the Circular
Doubly Linked List
• In this method, a node at the specified position in the circular doubly linked
list is deleted. For example - if the given list is 10->20->30 and the 2nd node
is deleted, the list becomes 10->20.
Delete a node at the given position in the Circular Doubly
Linked List

• First, create two nodes temp and nodeToDelete to traverse through the list and
track the node to delete respectively. After that count the number of elements
in the list to check whether the specified position is valid or not (It must lie in
the range of [1, n], where n is number of elements in the list). If the specified
valid position is 1 and head is the only element in the list, then make the head
as null. If the specified valid position is 1 and list contains more than one
elements, then make next of head as new head and adjust links accordingly. If
the specified valid position is greater than 1 then traverse to the node previous
to the given position and delete the given node and adjust links accordingly.
void pop_at(int position) { //3. check if the specified position is valid
//1. create two nodes - temp and nodeToDelete if(position < 1 || position > NoOfElements) {
cout<<"\nInavalid position.";
// to traverse and track the node to delete } else if (position == 1) {
Node* nodeToDelete = head;
//4. if the position is 1 and head is the only element
Node* temp = head; // in the list, then make it null, else make next
// of head as new head and adjust links accordingly
int NoOfElements = 0;
if(head->next == head) {
//2. Find the number of elements in the list head = NULL;
} else {
if(temp != NULL) { while(temp->next != head)
NoOfElements++; temp = temp->next;
head = head->next;
temp = temp->next; } temp->next = head;
while(temp != head) { head->prev = temp;
free(nodeToDelete);
NoOfElements++; }
temp = temp->next;} } else {
//5. Else, traverse to the node previous to the given position and delete the given
// node and adjust links accordingly
temp = head;
for(int i = 1; i < position-1; i++)
temp = temp->next;
nodeToDelete = temp->next;
temp->next = temp->next->next;
temp->next->prev = temp;
free(nodeToDelete);
}}
Lab Tasks (Internal Marks)
There is doubly circular linked list containing n elements i.e. 10>20>30>40
Insert an element at the nth position
Delete an element at the 3rd position
Search in the doubly circular linked list if element 30 is present or not

You might also like