-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular linked list.cpp
140 lines (139 loc) · 1.96 KB
/
circular linked list.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *next;
};
void insert(node ** head,int n)
{
node *nn=new node();
nn->data=n;
node *t=*head;
if(*head==NULL)
{
*head=nn;
nn->next=nn;
// t->data=n;
//t->next=t;
}
else
{
while(t->next!=*head)
{
t=t->next;
}
t->next=nn;
nn->next=*head;
*head=nn;
}
}
void display(node ** head)
{
node *t=*head;
do
{
cout<<(t->data)<<" ";
t=t->next;
}
while(t!=*head);
}
void split(node ** head)
{
node *sp=*head;
node *fp=*head;
while(fp->next!=*head && fp->next->next!=*head)
{
fp=fp->next->next;
sp=sp->next;
}
if(fp->next!=*head)
{
fp=fp->next;
}
fp->next=sp->next;
sp->next=*head;
node *head1=*head;
node *head2=fp->next;
display(&head1);
cout<<endl;
display(&head2);
}
void sort_insert(node ** head,int l)
{
node *w=new node();
w->data=l;
node *t=*head;
if(*head==NULL)
{
*head=w;
w->next=w;
}
else if((t->next->data)<l)
{
while((t->next->data)<l)
{
t=t->next;
}
w->next=t->next;
t->next=w;
}
}
void count(node ** head)
{
int c=0;
node *t=*head;
do
{
c++;
t=t->next;
}
while(t!=*head);
cout<<"number of nodes are "<<c<<endl;
}
void del(node ** head,int o)
{
node *t=*head;
if(t->data==o)
{
*head=t->next;
}
else
{
while(t->next->data!=o)
{
t=t->next;
}
t->next=t->next->next;
}
}
int main()
{
int m,i,n;
cout<<"Enter the size of circular ll"<<endl;
cin>>m;
node *head=NULL;
for(i=0;i<m;i++)
{
cin>>n;
insert(&head,n);
}
display(&head);
cout<<endl;
cout<<"Enter the value to insert in sorted linked list"<<endl;
int l,o;
cin>>l;
sort_insert(&head,l);
display(&head);
cout<<endl;
count(&head);
cout<<endl;
cout<<"enter the value to be deleted"<<endl;
cin>>o;
del(&head,o);
display(&head);
cout<<endl;
cout<<"the two splited linked list are:"<<endl;
split(&head);
}