forked from nazirmohd2006/NewRepository
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircularLinkedList.cpp
More file actions
97 lines (93 loc) · 2.03 KB
/
CircularLinkedList.cpp
File metadata and controls
97 lines (93 loc) · 2.03 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
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
class CircularLinkedList
{
public:
Node *last;
CircularLinkedList()
{
last = NULL;
}
Node* createNode(int value)
{
Node *temp = new Node;
if(temp == NULL)
cout<<"Node creation failed"<<endl;
else
{
temp->data = value;
temp->next = NULL;
}
return temp;
}
void insertFront()
{
Node *temp;
int num;
cout<<"Enter any value"<<endl;
cin>>num;
temp = createNode(num);
if(last ==NULL)
{
last = temp;
temp->next = last;
}
else
{
temp->next = last->next;
last->next = temp;
}
}
void insertAt()
{
Node *current, *prev, *temp;
int data;
cout<<"Enter Element"<<endl;
cin>>data;
temp = createNode(data);
current = last->next;
prev = NULL;
if(last==NULL)
{
last = temp;
last->next = last;
cout<<"First Node created"<<endl;
}
else
{
while(current->data < temp->data && prev != last)
{
prev = current;
current = current->next;
}
prev->next = temp;
temp->next = current;
cout<<"Node inserted at their correct position"<<endl;
}
}
void display()
{
Node *current;
current = last->next;
while(current != last)
{
cout<<current->data<<" -> ";
current = current->next;
}
cout<<last->data<<endl;
}
};
int main()
{
CircularLinkedList cll;
for(int i=0; i<3; i++)
cll.insertFront();
cll.insertAt();
cll.display();
}