-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular_Doubly_Linked_List.cpp
131 lines (122 loc) · 1.93 KB
/
Circular_Doubly_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
#include<iostream>
using namespace std;
class node{
private:
int data;
node *next;
node *prev;
public:
node(int d)
{
data=d;
next=NULL;
prev=NULL;
}
friend class Linked_List;
};
class Linked_List
{
private:
node *head;
public:
Linked_List()
{
head=NULL;
}
void insertatbeg(int d)
{
node *n=new node(d);
node*ptr=head;
if(head==NULL)
{
head=n;
n->next=head;
n->prev=n;
}
else
{
head->prev->next=n;
n->prev=head->prev;
n->next=head;
head->prev=n;
head=n;
}
}
void insertatend(int d)
{
node *n=new node(d);
head->prev->next=n;
n->prev=head->prev;
n->next=head;
}
void print()
{
node *t=head;
do
{
cout<<t->data<<" ->";
t=t->next;
}while(t!=head);
cout<<endl;
}
void insertatK(int d,int k)
{
node *n=new node(d);
node *t=head;
node *temp=NULL;
int cnt=1;
while(head==NULL || k==1)
{
insertatbeg(d);
return;
}
while(t->next!=NULL && cnt<k)
{
temp=t;
t=t->next;
cnt++;
}
n->prev=temp;
n->next=temp->next;
temp->next=n;
t->prev=n;
}
void delatbeg()
{
node *t=head;
head->prev->next=head->next;
head->next->prev=head->prev;
head=head->next;
t->next=NULL;
delete t;
}
void delatend()
{
node *t;
t=head->prev;
t->prev->next=head;
head->prev=t->prev;
t->next=NULL;
delete t;
}
};
int main()
{
Linked_List ll;
int d,c;
char choice;
ll.insertatbeg(1);
ll.insertatbeg(2);
ll.insertatbeg(3);
ll.print();
ll.insertatend(4);
ll.print();
// ll.insertatK(10,3);
//ll.print();
ll.delatend();
ll.print();
ll.delatend();
ll.print();
// ll.delatend();
//ll.print();
}