-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly linklist1.cpp
214 lines (196 loc) · 2.95 KB
/
doubly linklist1.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include<iostream>
using namespace std;
struct node
{
int data;
struct node* prev;
struct node* next;
};
struct node* head;
struct node* tail;
void display()
{
struct node* temp;
temp=head;
while(temp!=NULL)
{
cout<< temp->data<< endl;
temp=temp->next;
}
}
void insert(int data)
{
struct node* newnode;
newnode=(struct node*) malloc(sizeof(struct node));
newnode->data=data;
newnode->prev=NULL;
newnode->next=NULL;
if(head==NULL)
{
head=tail=newnode;
}
else
{
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
}
}
int length()
{
struct node* temp;
int count=0;
temp=head;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
return count;
}
void insertbeg(int data)
{
struct node* newnode;
newnode=(struct node*) malloc(sizeof(struct node));
newnode->data=data;
newnode->prev=NULL;
newnode->next=NULL;
head->prev=newnode;
newnode->next=head;
head=newnode;
}
void insertend(int data)
{
struct node* newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->prev=NULL;
newnode->next=NULL;
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
}
void insertinpos(int pos, int data)
{
int i=1;
if(pos<1 || pos> length())
{
cout<< " this is invalid"<< endl;
}
else if(pos==1)
{
insertbeg(data);
}
else
{
struct node* newnode;
struct node* temp;
temp=head;
newnode=(struct node*) malloc(sizeof(struct node));
newnode->data=data;
while(i<pos-1)
{
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
newnode->next->prev=newnode;
}
}
void insertafterpos(int pos, int data)
{
int i=1;
if(pos<1 || pos>length())
{
cout<< " this is invalid" << endl;
}
else
{
struct node* newnode;
struct node* temp;
newnode=(struct node*) malloc(sizeof(struct node));
temp=head;
newnode->data=data;
while(i< pos)
{
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
newnode->next->prev=newnode;
}
}
void deletebeg()
{
struct node* temp;
if(head== NULL)
{
cout<< "the node is empty"<< endl;
}
else
{
temp=head;
head=head->next;
head->prev=NULL;
free(temp);
}
}
void deleteend()
{
struct node* temp;
if(tail==NULL)
{
cout<< "this is empty" << endl;
}
temp=tail;
tail=tail->prev;
tail->prev->next=NULL;
free(temp);
}
void deletefromposition(int position)
{
struct node* temp;
int i;
temp=head;
while(i<position)
{
temp=temp->next;
i++;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
void reverse()
{
struct node* current, *nextnode;
current=head;
while(current !=NULL)
{
nextnode=current->next;
current->next=current->prev;
current->prev=nextnode;
current=nextnode;
}
current=head;
head=tail;
tail=current;
}
int main()
{
insert(50);
insert(40);
insert(20);
insert(80);
insert(90);
insertbeg(100);
insertend(500);
insertinpos(3,1000);
insertafterpos(3,5000);
display();
return 0;
}