Doubly and Circular Linked List
Doubly and Circular Linked List
Introduction
• Sequence of elements in which every element has links to
its previous element and next element in the sequence.
• Each node contains three fields: data, link to the next node,
and link to the previous node.
Pointer to the previous Pointer to the next
prev data next node in sequence
node in sequence
• Disadvantages:
– Requires more space.
– List manipulations are slower.
– Greater chances of having bugs.
Creation struct node
{ int data;
• Define node structure. struct node *next, *prev; };
• Declare a NULL initialized head node pointer to
create an empty list. struct node *head = NULL;
• Dynamically allocate memory for a node and
initialize all members of a node.
struct node *temp =
(struct node *) malloc (sizeof(struct node));
int num;
scanf("%d",&num);
temp -> data = num;
temp -> prev = temp -> next = NULL;
class Node {
public:
int data;
Node* next;
Node* prev;
int main() {
Node(int num) DoublyLinkedList dll;
{ data= num,
next= nullptr,
prev= nullptr
}
};
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList()
{
head= nullptr}
Contd…
head = temp;
• Link the new node temp in the existing empty list.
• Again dynamically allocate memory for a node and
initialize all members of a node.
*temp = (struct node *) malloc (sizeof(struct node));
scanf("%d",&num);
temp -> data = num;
temp -> prev = temp -> next = NULL;
• Link the new node temp in the existing list at head.
temp -> next = head; head -> prev = temp;
head = temp;
• This process is repeated for all the nodes. A node can
inserted anywhere in the list.
Linked List
Search an element in the list
• Algorithm search(head, num)
• Input: Pointer to the first node (head) and a value to search (num).
• Output: Appropriate message will be displayed.
1. If (head == NULL)
2. Print [List is Empty].
3. Return.
4. Initialize a node pointer (temp) with head.
5. while (temp is not NULL AND temp[data] is not equal to value)
6. temp = temp[next]
7. if (temp is NULL)
8. Print [Element not found].
9. Else
10. Print [Element found].
Search an element in the list
(same as SLL)
void deleteAtBegin() {
if (!head) {
std::cout << "List is Empty. Nothing to
delete." << std::endl;
return;
}
Node* temp = head;
head = head->next;
if (head) {
head->prev = nullptr;
}
delete temp;
}
Delete from end of the list
• Algorithm deleteEnd(head)
• Input: Pointer to the first node (head).
• Output: The last node gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize a node pointer (temp) with head.
5. while (temp[next] is not NULL)
6. temp = temp[next]
7. if (temp == head)
8. head = NULL
9. else
10. (temp[prev])[next] = NULL
11. Release the memory location pointed by temp.
12. end if
Delete from end of the list
// Delete at the end
void deleteAtEnd() {
if (!head) {
std::cout << "List is Empty. Nothing to
delete." << std::endl;
return;
}
Node* current = head;
while (current->next) {
current = current->next;
}
if (current->prev) {
current->prev->next = nullptr;
} else {
head = nullptr;
}
delete current;
}
Delete a specific node from the list
• Algorithm deleteSpecific(head,num)
• Input: Pointer to the first node (head) and a value num to be
deleted.
• Output: The node with value num gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize a node pointer (temp) with head.
5. while (temp != NULL AND temp[data] != value)
6. temp = temp[next]
7. if (temp is NULL)
8. Print [Element not found].
9. Return.
Contd…
10. else if (temp == head)
11. deleteBeg(head)
12. else if (temp[next] == NULL)
13. deleteEnd(head)
14. else
15. (temp[prev])[next] = temp[next]
16. (temp[next])[prev] = temp[prev]
17. Release the memory location pointed by temp.
18. end if (line 7).
19. end if (line 1).
Delete a specific node from the list
void deleteAfterValue(int value) {
Node* current = head;
while (current) {
if (current->data == value) {
Node* temp = current->next;
if (temp) {
current->next = temp->next;
if (temp->next) {
temp->next->prev = current;
}
delete temp;
} else {
std::cout << "No element to delete after " << value << "."
<< std::endl;
}
return;
}
current = current->next;
}
std::cout << "Value " << value << " not found in the list." <<
std::endl;
Circular Linked List
Introduction
• The first element points to the last element and the
last element points to the first element.
• There is no NULL node.
• While traversal, get back to a node from where you
have started.
• Pointer to any node can serve as a handle to the
complete list.
• Both singly and doubly linked lists can be circular.
Contd…
• Singly linked list as circular
HEAD
5. head = temp
Contd…
6. else
7. Initialize a node pointer (temp1) with head.
8. while (temp1[next] != head AND temp1[data] != value)
9. temp1 = temp1[next]
10. if (temp1[next] == head AND temp1[data] != value)
11. print [Node is not present in the list]
12. else
13. temp[next] = temp1[next]
14. temp1[next] = temp
15. end if (line 10).
16. End if (line 3).
Delete from beginning of the list (singly)
• Algorithm deleteBeg(head)
• Input: Pointer to the first node (head).
• Output: The first node gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize node pointers (temp and temp1) with head.
5. while (temp1[next] is not equal to head)
6. temp1 = temp1[next]
7. if (temp1 == head)
8. head == NULL
9. else
10. temp1[next] = head[next].
11. head = head[next]
1032-02.-2017 Release the memory locatiLoinknedpListointed by temp. 4
1
Delete from beginning of the list (singly
// Delete at the beginning
void deleteAtBegin() {
if (!head) {
std::cout << "List is Empty. Nothing to
delete." << std::endl;
return;
}
Node* temp = head;
if (head->next == head) {
head = nullptr;
} else {
Node* current = head;
while (current->next != head) {
current = current->next;
}
head = head->next;
current->next = head;
}
delete temp;
}
Delete from end of the list (singly)
• Algorithm deleteEnd(head)
• Input: Pointer to the first node (head).
• Output: The last node gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize a node pointer (temp) with head.
5. while (temp[next] is not head)
6. initialize a node pointer (pre) with temp.
7. temp = temp[next]
8. if (temp == head)
9. head = NULL
10. else
11. pre[next] = head
1032-02.-2017 Release the memory locatiLoinknedpListointed by temp.
Delete from end of the list (singly)
// Delete at the end
void deleteAtEnd() {
if (!head) {
std::cout << "List is Empty. Nothing to
delete." << std::endl;
return;
}
Node* temp = head;
if (head->next == head) {
head = nullptr;
} else {
Node* current = head;
Node* prev = nullptr;
while (current->next != head) {
prev = current;
current = current->next;
}
prev->next = head;
}
delete temp;
}
Delete a specific node from the list (singly)
• Algorithm deleteSpecific(head,num)
• Input: Pointer to the first node (head) and a value num to be deleted.
• Output: The node with value num gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize a node pointer (temp) with head.
5. while (temp[next] != head AND temp[data] != value)
6. initialize a node pointer (pre) with temp.
7. temp = temp[next]
8. if (temp[data] != value)
9. Print [Element not found].
10. Return.
Contd…
11. else if (temp == head)
12. deleteBeg(head)
13. else if (temp[next] == head)
14. deleteEnd(head)
15. else
16. pre[next] = temp[next]
17. Release the memory location pointed by temp.
18. end if (line 8).
19. end if (line 1).
Insertion at beginning of the list (doubly)
• Algorithm insertBeg(head, num)
• Input: Pointer to the first node (head) and a new value to insert
(num).
• Output: Node with value num gets inserted at the first position.
1. Create a node pointer (temp).
2. temp[data] = num. temp
3. if (head == NULL)
data
4. temp[next] = temp[prev] = temp.
5. else
6. temp[next] = head.
7. temp[prev] = head[prev].
8. head[prev] = temp.
9. (temp[prev])[next] = temp.
03-0
02.-201h
7 ead = temp. Linked List 52
Insertion at end of the list (doubly)
• Algorithm insertEnd(head, num)
• Input: Pointer to the first node (head) and a new
value to insert (num).
• Output: Node with value num gets inserted at the
last position.
1. Create a node pointer (temp).
2. temp[data] = num
3. If (head == NULL) HEAD
4. temp[prev] = temp temp
5. temp[next] = temp data
6. head = temp
Contd…
7. else
8. temp[next] = head
9. temp[prev] = head[prev]
10. (head[prev])[next] = temp
11. head[prev] = temp
Insertion after a specific value in the list (doubly)
• Algorithm insert(head, num, value)
• Input: Pointer to the first node (head) and a new value to insert
(num) after an existing value.
• Output: Node with value num gets inserted after node with value.
1. Create a node pointer (temp).
2. temp[data] = num
HEAD
3. If (head == NULL) temp
4. temp[prev] = temp[next] = temp
data
5. head = temp
6.Else
7. Initialize a node pointer (temp1) with head.
8. while (temp1[next] != head AND temp1[data] != value)
9. temp1 = temp1[next]
Contd…
10. if (temp1[next] == head AND temp1[data] != value)
11. print [Node is not present in the list]
12. else
13. temp[prev] = temp1
14. temp[next] = temp1[next]
15. temp1[next] = temp
16. (temp[next])[prev] = temp
17. end if (line 10).
18. End if (line 3).
Delete from beginning of the list (doubly)
• Algorithm deleteBeg(head)
• Input: Pointer to the first node (head).
• Output: The first node gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize a node pointer (temp) with head.
5. if (temp[next] == head)
6. head == NULL
7. else
8. (head[prev])[next] = head[next]
9. (head[next])[prev] = head[prev]
10. head = head[next]
13-102.-2017 Release the memory locaLintkieo
0 d Ln
ist pointed by temp.
Delete from end of the list (doubly)
• Algorithm deleteEnd(head)
• Input: Pointer to the first node (head).
• Output: The last node gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize a node pointer (temp) with head.
5. while (temp[next] is not head)
6. temp = temp[next]
7. if (temp == head)
8. head = NULL
9. else
10. (temp[prev])[next] = head
11. head[prev] = temp[prev]
12. Release the memory location pointed by temp.
133-02.-20e17nd if
0
Delete a specific node from the list (doubly)
• Algorithm deleteSpecific(head,num)
• Input: Pointer to the first node (head) and a value num to be
deleted.
• Output: The node with value num gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize a node pointer (temp) with head.
5. while (temp[next] != head AND temp[data] != value)
6. temp = temp[next]
7. if (temp[data] != value)
8. Print [Element not found].
9. Return.
Contd…
10. else if (temp == head)
11. deleteBeg(head)
12. else if (temp[next] == head)
13. deleteEnd(head)
14. else
15. (temp[prev])[next] = temp[next]
16. (temp[next])[prev] = temp[prev]
17. Release the memory location pointed by temp.
18. end if (line 7).
19. end if (line 1).