Skip to content

Commit c7f39e7

Browse files
authored
Merge pull request #127 from kiruba-r11/feature-1
Reorganised Linked Lists folder and added insert_at_end.cpp file
2 parents 3c667cd + 55ebe3b commit c7f39e7

20 files changed

+81
-0
lines changed
Binary file not shown.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 appended 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+
12+
#include <iostream>
13+
14+
using namespace std;
15+
16+
/*
17+
18+
Node definition:
19+
1. Pointer to previous node.
20+
2. Integer Data
21+
3. Pointer to next node.
22+
23+
*/
24+
25+
class dll_node {
26+
public:
27+
dll_node* prev;
28+
int data;
29+
dll_node* next;
30+
};
31+
32+
void createDLL(dll_node* &head) {
33+
34+
int choice;
35+
36+
do {
37+
38+
int data;
39+
40+
cout << "Enter Data : ";
41+
cin >> data;
42+
43+
dll_node* newNode = new dll_node();
44+
newNode->data = data;
45+
newNode->prev = NULL;
46+
newNode->next = NULL;
47+
48+
if(head == NULL) {
49+
head = newNode;
50+
} else {
51+
newNode->next = head;
52+
head->prev = newNode;
53+
head = newNode;
54+
}
55+
56+
cout << "Do you want to continue? (1/0) : ";
57+
cin >> choice;
58+
59+
} while(choice == 1);
60+
61+
62+
}
63+
64+
void display(dll_node* head) {
65+
cout << "The elements are : ";
66+
while(head != NULL) {
67+
cout << head->data << " ";
68+
head = head->next;
69+
}
70+
cout << endl;
71+
}
72+
73+
int main() {
74+
75+
dll_node* head = NULL;
76+
77+
createDLL(head);
78+
display(head);
79+
80+
return 0;
81+
}
Binary file not shown.

0 commit comments

Comments
 (0)