Skip to content

Commit 9ebf606

Browse files
committed
insert_at_end.cpp file added
1 parent d17d72a commit 9ebf606

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
3+
This is a Circular Linked List program which gets a integer data from the user and
4+
creates a node which is inserted at the end of the Linked List.
5+
6+
Since, it is a Circular Linked List, the last node would point to the head node.
7+
8+
*/
9+
10+
#include <iostream>
11+
12+
using namespace std;
13+
14+
/*
15+
16+
Node definition:
17+
1. Integer Data
18+
2. Pointer to next node.
19+
20+
*/
21+
22+
class cll_node {
23+
public:
24+
int data;
25+
cll_node* next;
26+
};
27+
28+
void createCLL(cll_node* &head) {
29+
30+
int choice;
31+
32+
cll_node* temp = head;
33+
34+
do {
35+
int data;
36+
37+
cout << "Enter Data : ";
38+
cin >> data;
39+
40+
cll_node* newNode = new cll_node();
41+
newNode->data = data;
42+
newNode->next = NULL;
43+
44+
if(head == NULL) {
45+
head = newNode;
46+
newNode->next = head;
47+
temp = head;
48+
} else {
49+
temp->next = newNode;
50+
temp = newNode;
51+
temp->next = head;
52+
}
53+
54+
cout << "Do you want to continue? (1/0) : ";
55+
cin >> choice;
56+
57+
} while(choice == 1);
58+
59+
}
60+
61+
void display(cll_node* head) {
62+
cout << "The elements are : ";
63+
if(head == NULL)
64+
return;
65+
cll_node* temp = head;
66+
67+
do {
68+
cout << temp->data << " ";
69+
temp = temp->next;
70+
} while(temp != head);
71+
cout << endl;
72+
}
73+
74+
int main() {
75+
76+
cll_node* head = NULL;
77+
78+
createCLL(head);
79+
display(head);
80+
81+
return 0;
82+
}

0 commit comments

Comments
 (0)