Skip to content

Commit 006da04

Browse files
authored
Merge pull request #187 from kiruba-r11/feature-1
Added Insert At Begin in Circular Doubly Linked Lists
2 parents 574e3b5 + ad5661c commit 006da04

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
3+
This is a Circular Doubly Linked List program which add 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 = head;
49+
head->next = head;
50+
head->prev = head;
51+
} else {
52+
newNode->next = head;
53+
head->prev = newNode;
54+
newNode->prev = temp;
55+
temp->next = newNode;
56+
head = newNode;
57+
}
58+
59+
cout << "Do you want to continue? (1/0) : ";
60+
cin >> choice;
61+
62+
} while(choice == 1);
63+
}
64+
65+
void display(dcll_node* head) {
66+
67+
dcll_node* temp = head;
68+
69+
cout << "The elements are : ";
70+
do {
71+
cout << temp->data << " ";
72+
temp = temp->next;
73+
} while(temp != head);
74+
75+
cout << endl;
76+
}
77+
78+
int main() {
79+
80+
dcll_node* head = NULL;
81+
82+
createDCLL(head);
83+
display(head);
84+
85+
return 0;
86+
}

0 commit comments

Comments
 (0)