-
Notifications
You must be signed in to change notification settings - Fork 1
/
doubly_ll.c
151 lines (148 loc) · 2.24 KB
/
doubly_ll.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
typedef struct node * NODE;
//NODE insert_left(int item,NODE head,NODE n);
NODE getnode();
void display(NODE head);
NODE delete_val(NODE head,int value);
NODE insert_front(int item,NODE head);
NODE getnode()
{
NODE p;
p=(NODE)malloc(sizeof(struct node));
if(p!=NULL)
return p;
else
{
printf("memory could not be allocated\n");
exit(0);
}
}
NODE insert_front(int item,NODE head)
{
NODE p;
p=getnode();
p->data=item;
p->next=head;
head=p;
return head;
}
/*NODE insert_left(int item,NODE head,NODE n)
{
NODE newn=getnode();
newn->data=item;
if(n==head)
{
newn->next=n;
n->prev=newn;
//printf("a1\n");
newn->prev=NULL;
//printf("b\n");
head=newn;
//printf("c\n");
return head;
//printf("d\n");
}
else
//printf("e\n");
n->prev->next=newn;
newn->prev=n->prev;
newn->next=n;
n->prev=newn;
return head;
//printf("f\n");
}*/
void display(NODE head)
{
NODE p;
if(head==NULL)
{
printf("list is empty\n");
exit(0);
}
p=head;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
NODE delete_val(NODE head,int value)
{
NODE p;
if(head==NULL)
{
printf("list is empty\n");
return head;
}
if(head->next==NULL)
{
if(head->data==value)
{
free(head);
return NULL;
}
else
{
printf("value not found\n");
return head;
}
}
p=head;
while(p!=NULL)
{
if(p->data==value)
{
if(p->prev!=NULL)
p->prev->next=NULL;
else
head=p->next;
if(p->next!=NULL)
{
p->next->prev=p->prev;
}
free(p);
return head;
}
p=p->next;
}
if(p==NULL)
{
printf("value not found\n");
return head;
}
}
int main()
{
NODE head=NULL;
int ch,e,ele;
printf("enter\n1.insert \n2.delete the given value\n3.display\n-1.exit\n");
scanf("%d",&ch);
while(ch!=-1)
{
switch(ch)
{
case 1:printf("enter the item to be inserted in the liked list\n");
scanf("%d",&e);
//head=insert_left(e,head,head);
head=insert_front(e,head);
break;
case 2:printf("enter the value you wanna delete\n");
scanf("%d",&ele);
head=delete_val(head,ele);
break;
case 3:display(head);
break;
default:printf("invalid input\n");
}
printf("enter next choice or -1 to exit\n");
scanf("%d",&ch);
}
return 0;
}