-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcircular_linked_list.cpp
More file actions
84 lines (72 loc) · 1.48 KB
/
circular_linked_list.cpp
File metadata and controls
84 lines (72 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
using namespace std;
class Node{
public:
int x;
Node* next;
};
class Circular_linked_list{
public:
Node* head;
Circular_linked_list(){
head = NULL;
}
void insert_at_beginning (int k){
Node* sub = new Node();
sub->x = k;
if(head==NULL){
head = sub;
sub->next = head;
return;
}
Node* temp = head;
Node* temp2 = head;
head = sub;
sub->next = temp;
while(temp->next!=temp2){
temp = temp->next;
}
temp->next = sub;
}
void insert_at_end(int k){
Node* sub = new Node();
sub->x = k;
if(head == NULL) {
head = sub;
sub->next = head;
return;
}
Node* std = head;
Node* temp = head;
while(temp->next != std){
temp = temp->next;
}
temp->next = sub;
sub->next = head;
}
void print(){
Node* std = head;
Node* temp = head;
cout << "the element of the list are : " << endl;
while(temp->next != std){
cout << temp->x << endl;
temp = temp->next;
}
cout << temp->x << endl;
}
//Almost all other functions can be done very similar as in the case of Singly_linked_list;
//So, moving on to doubly_linked_list;
};
int main(){
Circular_linked_list h;
int n;
cout << "enter the number of elements of the list: " << endl;
cin >> n;
for(int i=0;i<n;i++){
cout << "Enter the element" << endl;
int k;
cin >> k;
h.insert_at_end(k);
}
h.print();
}