File tree Expand file tree Collapse file tree 1 file changed +80
-0
lines changed
Data Structures/Linked Lists/Doubly Linked List Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ This is a Doubly Linked List program which gets a integer data from the user and
4
+ creates a node which is inserted at the beginning to the Linked List.
5
+
6
+ Since, it is a Doubly Linked List, both the forward and backward traversal is also possible.
7
+
8
+
9
+ */
10
+
11
+ #include < iostream>
12
+
13
+ using namespace std ;
14
+
15
+ /*
16
+
17
+ Node definition:
18
+ 1. Pointer to previous node.
19
+ 2. Integer Data
20
+ 3. Pointer to next node.
21
+
22
+ */
23
+
24
+ class dll_node {
25
+ public:
26
+ dll_node* prev;
27
+ int data;
28
+ dll_node* next;
29
+ };
30
+
31
+ void createDLL (dll_node* &head) {
32
+
33
+ int choice;
34
+
35
+ do {
36
+
37
+ int data;
38
+
39
+ cout << " Enter Data : " ;
40
+ cin >> data;
41
+
42
+ dll_node* newNode = new dll_node ();
43
+ newNode->data = data;
44
+ newNode->prev = NULL ;
45
+ newNode->next = NULL ;
46
+
47
+ if (head == NULL ) {
48
+ head = newNode;
49
+ } else {
50
+ newNode->next = head;
51
+ head->prev = newNode;
52
+ head = newNode;
53
+ }
54
+
55
+ cout << " Do you want to continue? (1/0) : " ;
56
+ cin >> choice;
57
+
58
+ } while (choice == 1 );
59
+
60
+
61
+ }
62
+
63
+ void display (dll_node* head) {
64
+ cout << " The elements are : " ;
65
+ while (head != NULL ) {
66
+ cout << head->data << " " ;
67
+ head = head->next ;
68
+ }
69
+ cout << endl;
70
+ }
71
+
72
+ int main () {
73
+
74
+ dll_node* head = NULL ;
75
+
76
+ createDLL (head);
77
+ display (head);
78
+
79
+ return 0 ;
80
+ }
You can’t perform that action at this time.
0 commit comments