File tree Expand file tree Collapse file tree 1 file changed +103
-0
lines changed
Data Structures/Linked Lists/Circular Linked Lists Expand file tree Collapse file tree 1 file changed +103
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ This is a Circular Linked List program which deletes a node from the end of the Linked List.
4
+
5
+ Since, it is a Circular Linked List, the last node would point to the head node.
6
+
7
+ */
8
+
9
+ #include < iostream>
10
+
11
+ using namespace std ;
12
+
13
+ /*
14
+
15
+ Node definition:
16
+ 1. Integer Data
17
+ 2. Pointer to next node.
18
+
19
+ */
20
+
21
+ class cll_node {
22
+ public:
23
+ int data;
24
+ cll_node* next;
25
+ };
26
+
27
+ void createCLL (cll_node* &head) {
28
+
29
+ int choice;
30
+
31
+ cll_node* temp = head;
32
+
33
+ do {
34
+ int data;
35
+
36
+ cout << " Enter Data : " ;
37
+ cin >> data;
38
+
39
+ cll_node* newNode = new cll_node ();
40
+ newNode->data = data;
41
+ newNode->next = NULL ;
42
+
43
+ if (head == NULL ) {
44
+ head = newNode;
45
+ newNode->next = head;
46
+ temp = head;
47
+ } else {
48
+ temp->next = newNode;
49
+ temp = newNode;
50
+ temp->next = head;
51
+ }
52
+
53
+ cout << " Do you want to continue? (1/0) : " ;
54
+ cin >> choice;
55
+
56
+ } while (choice == 1 );
57
+
58
+ }
59
+
60
+ void delete_at_end (cll_node* &head) {
61
+ if (head == NULL || head->next == head) {
62
+ head = NULL ;
63
+ delete head;
64
+ return ;
65
+ }
66
+
67
+ cll_node* temp = head;
68
+ while (temp->next ->next != head) {
69
+ temp = temp->next ;
70
+ }
71
+ cll_node* temp1 = temp->next ;
72
+ temp->next = head;
73
+ delete temp1;
74
+ }
75
+
76
+ void display (cll_node* head) {
77
+ cout << " The elements are : " ;
78
+ if (head == NULL )
79
+ return ;
80
+ cll_node* temp = head;
81
+
82
+ do {
83
+ cout << temp->data << " " ;
84
+ temp = temp->next ;
85
+ } while (temp != head);
86
+ cout << endl;
87
+ }
88
+
89
+ int main () {
90
+
91
+ cll_node* head = NULL ;
92
+
93
+ createCLL (head);
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
+ }
You can’t perform that action at this time.
0 commit comments