Skip to content

Commit f06554d

Browse files
authored
Merge pull request #194 from kiruba-r11/feature-1
Added Delete At the End of the Circular Doubly Linked Lists
2 parents db05b8f + fccc4a7 commit f06554d

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
3+
This is a Circular Doubly Linked List program which deletes a Node at the end of the Linked Lists.
4+
Since, it is a Ciruclar Doubly Linked List, both the forward and backward traversal is also possible.
5+
6+
*/
7+
8+
#include <iostream>
9+
10+
using namespace std;
11+
12+
/*
13+
14+
Node definition:
15+
1. Pointer to previous node.
16+
2. Integer Data
17+
3. Pointer to next node.
18+
19+
*/
20+
21+
class dcll_node {
22+
public:
23+
dcll_node* prev;
24+
int data;
25+
dcll_node* next;
26+
};
27+
28+
void createDCLL(dcll_node* &head) {
29+
30+
dcll_node* temp = head;
31+
32+
int choice;
33+
34+
do {
35+
36+
int data;
37+
38+
cout << "Enter Data : ";
39+
cin >> data;
40+
41+
dcll_node* newNode = new dcll_node();
42+
newNode->data = data;
43+
newNode->prev = NULL;
44+
newNode->next = NULL;
45+
46+
if(head == NULL) {
47+
head = newNode;
48+
temp = newNode;
49+
head->next = newNode;
50+
head->prev = newNode;
51+
} else {
52+
temp->next = newNode;
53+
newNode->prev = temp;
54+
temp = newNode;
55+
newNode->next = head;
56+
head->prev = newNode;
57+
}
58+
59+
cout << "Do you want to continue? (1/0) : ";
60+
cin >> choice;
61+
62+
} while(choice == 1);
63+
64+
}
65+
66+
void delete_at_end(dcll_node* &head) {
67+
if(head == NULL || head->next == head) {
68+
head = NULL;
69+
delete head;
70+
return;
71+
}
72+
73+
dcll_node* temp = head->prev;
74+
head->prev = head->prev->prev;
75+
head->prev->next = head;
76+
delete temp;
77+
}
78+
79+
void display(dcll_node* head) {
80+
81+
dcll_node* temp = head;
82+
cout << "The elements are : ";
83+
do {
84+
85+
cout << temp->data << " ";
86+
temp = temp->next;
87+
88+
} while(temp != head);
89+
cout << endl;
90+
}
91+
92+
int main() {
93+
94+
dcll_node* head = NULL;
95+
96+
createDCLL(head);
97+
98+
cout << "Before Deletion : " << endl;
99+
display(head);
100+
101+
delete_at_end(head);
102+
103+
cout << "After Deletion : " << endl;
104+
display(head);
105+
106+
return 0;
107+
}

0 commit comments

Comments
 (0)