Skip to content

Commit ee36115

Browse files
authored
Merge pull request #142 from kiruba-r11/feature-1
Added Delete at the end of the Doubly Linked Lists
2 parents 00f6832 + 9ab94b6 commit ee36115

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
3+
This is a Doubly Linked List program which deletes a node at the end to the Linked List.
4+
Since, it is a 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 dll_node {
22+
public:
23+
dll_node* prev;
24+
int data;
25+
dll_node* next;
26+
};
27+
28+
void createDLL(dll_node* &head) {
29+
30+
int choice;
31+
32+
dll_node* temp = head;
33+
34+
do {
35+
36+
int data;
37+
38+
cout << "Enter Data : ";
39+
cin >> data;
40+
41+
dll_node* newNode = new dll_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+
} else {
50+
temp->next = newNode;
51+
newNode->prev = temp;
52+
temp = 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 delete_at_end(dll_node* &head) {
64+
65+
if(head == NULL)
66+
return;
67+
68+
dll_node* temp = head;
69+
while(temp->next != NULL) {
70+
temp = temp->next;
71+
}
72+
73+
temp->prev->next = NULL;
74+
delete temp;
75+
return;
76+
77+
}
78+
79+
void display(dll_node* head) {
80+
cout << "The elements are : ";
81+
while(head != NULL) {
82+
cout << head->data << " ";
83+
head = head->next;
84+
}
85+
cout << endl;
86+
}
87+
88+
int main() {
89+
90+
dll_node* head = NULL;
91+
92+
createDLL(head);
93+
94+
cout << "Before Deletion : " << endl;
95+
display(head);
96+
97+
delete_at_end(head);
98+
99+
cout << "After Deletion : " << endl;
100+
display(head);
101+
102+
return 0;
103+
}

0 commit comments

Comments
 (0)