-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble_linked_list.c
152 lines (152 loc) · 2.66 KB
/
double_linked_list.c
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
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
struct node *prev;
int data;
struct node *ptr;
};
struct node *start=NULL;
struct node *insert()
{
struct node *new_node,*q;
int num;
printf("\nEnter -1 to stop.");
printf("\nEnter elements in double liinked list: ");
scanf("%d",&num);
while(num!=-1)
{
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
if(start==NULL)
{
start=new_node;
new_node->prev=NULL;
new_node->ptr=NULL;
q=start;
}
else
{
q->ptr=new_node;
new_node->prev=q;
new_node->ptr=NULL;
q=q->ptr;
}
scanf("%d",&num);
}
return start;
}
struct node *display()
{
struct node *z;
z=start;
while(z!=NULL)
{
printf("\t%d",z->data);
z=z->ptr;
}
return start;
}
struct node *insert_beg()
{
struct node *new_node;
int num;
printf("\nEnter no to add at beginning: ");
scanf("%d",&num);
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
new_node->prev=NULL;
new_node->ptr=start;
start->prev=new_node;
start=new_node;
return start;
}
struct node *insert_end()
{
struct node *new_node,*z;
int num;
printf("\nEnter element to add at last of list: ");
scanf("%d",&num);
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
z=start;
while(z->ptr!=NULL)
z=z->ptr;
new_node->ptr=NULL;
z->ptr=new_node;
new_node->prev=z;
return start;
}
struct node *insert_after()
{
struct node *new_node,*preptr,*z;
int num,a;
printf("\nEnter element to add: ");
scanf("%d",&num);
printf("\nEnter no after which you want to add: ");
scanf("%d",&a);
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
z=start;
preptr=z;
while(preptr->data!=a)
{
preptr=z;
z=z->ptr;
}
preptr->ptr=new_node;
new_node->prev=preptr;
new_node->ptr=z;
z->prev=new_node;
return start;
}
struct node *del_first()
{
struct node *x;
x=start;
start=start->ptr;
start->prev=NULL;
free(x);
printf("\nAfter deleting first element:\n");
return start;
}
struct node *del_after()
{
struct node *z,*preptr;
int num;
printf("\nEnter element whose next one you wanna delete: ");
scanf("%d",&num);
z=start;
preptr=z;
while(preptr->data!=num)
{
preptr=z;
z=z->ptr;
}
preptr->ptr=z->ptr;
z->ptr->prev=preptr;
return start;
}
struct node *del()
{
while(start!=NULL)
start=del_first();
return start;
}
int main()
{
insert();
display();
insert_beg();
display();
insert_end();
display();
insert_after();
display();
del_first();
display();
del_after();
display();
del();
return 0;
}