File tree Expand file tree Collapse file tree 1 file changed +82
-0
lines changed
Data Structures/Linked Lists/Circular Linked Lists Expand file tree Collapse file tree 1 file changed +82
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments