-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_linked.cpp
132 lines (129 loc) · 4.18 KB
/
circular_linked.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
#include <iostream>
using namespace std;
struct node
{
int no=0;
struct node* next=NULL;
struct node* pre=NULL;
};
node* head =NULL;
int main()
{
struct node* prev=NULL;
while (0<1)
{
struct node* n=new node;
cout<<head<<endl;
cout<<prev<<endl;
cout<<"Insert at the front:1\nInsert at the back:2 \nDelete from the front:3\nDelete from the back:4\nDisplay:5\nDisplay rev:6\nExit:7"<<endl;
int in;
cin >> in;
switch(in)
{
case 1:
{if(head==NULL)
{
head=n;
cout<<"Enter the element to insert: \n";int a;cin >> a;
n->no=a;
prev=n;
}
else
{
cout<<"Enter the element to insert: \n";int a;cin >> a;
n->no=a;
head->pre=n;
prev->next=n;
n->next=head;
n->pre=prev;
head=n;
}
break;}
case 2:
{if(head==NULL)
{
head=n;
cout<<"Enter the element to insert: \n";int a;cin >> a;
n->no=a;
prev=n;
}
else
{
cout<<"Enter the element to insert: \n";int a;cin >> a;
n->no=a;
n->pre=prev;
n->next=head;
prev->next=n;
prev=n;
head->pre=prev;
}
break;}
case 3:
{if(head==NULL) cout<<"List is empty! \n";
else if(head->next==head)
{
node* t=head;
head=NULL;
delete t;
}
else
{
struct node* t=head;
head=t->next;
head->pre=prev;
prev->next=head;
delete t;
}
break;}
case 4:
{if(head==NULL) cout<<"List is empty! \n";
else if(prev->pre==prev)
{
head=NULL;
delete prev;
}
else
{
struct node* t=prev;
prev=t->pre;
prev->next=head;
head->pre=prev;
delete t;
}
break;}
case 5:
{if(head==NULL) cout<<"List is empty! \n";
else
{
cout<<endl;
struct node* t=head;
cout<<t->no<<endl;
t=t->next;
while(t!=head)
{
cout<<t->no<<endl;
t=t->next;
}
cout<<endl;
}
break;}
case 6:
{if(head==NULL) cout<<"List is empty! \n";
else
{
cout<<endl;
struct node* t=prev;
cout<<t->no<<endl;
t=t->pre;
while(t!=prev)
{
cout<<t->no<<endl;
t=t->pre;
}
cout<<endl;
}
break;}
case 7: exit(0);
}
}
}