-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoub_ll.c
166 lines (152 loc) · 2.39 KB
/
doub_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *prev; // point the address of previous node
struct node *next; //point the address of next node
}node;
node *start ,*temp,*ptr;
void insertFirst()
{
temp=(node*)malloc(sizeof(node));
printf("Enter the data : ");
scanf("%d",&temp->data);
temp->prev=NULL;
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
temp->next=start;
start->prev=temp;
start=temp;
}
}
void insertLast()
{
temp=(node*)malloc(sizeof(node));
printf("Enter the data : ");
scanf("%d",&temp->data);
temp->prev=NULL;
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
temp->prev=ptr;
}
}
void deleteFirst()
{
if(start==NULL)
printf("\n U N D E R F L O W");
else
{
printf("\n Delete Element %d",start->data);
start=start->next;
}
}
void deleteLast()
{
if(start==NULL)
printf("\n U N D E R F L O W");
else if(start->next==NULL)
{
printf("\n Deleted Element %d",start->data);
start=NULL;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr=ptr->prev;
ptr->next->prev=NULL;
ptr->next=NULL;
}
}
void display()
{
if(start==NULL)
printf("\n List is Empty");
else
{
ptr=start;
printf("\n Elements are ..\n");
while(ptr!=NULL)
{
printf("\n %d",ptr->data);
ptr=ptr->next;
}
}
}
void search()
{
int num,p=0;
if(start==NULL)
printf("\n Search Not Possible ");
else
{
printf("\n Enter the data : ");
scanf("%d",&num);
ptr=start;
while(ptr!=NULL)
{
p++;
if(ptr->data==num)
break;
ptr=ptr->next;
}
if(ptr==NULL)
printf("\n Number not Present ");
else
printf("\n Number present at position %d",p);
}
}
void main()
{
int ch;
start=NULL;
while(1)
{
printf("\n\n 1 for Insert First ");
printf("\n 2 for Insert Last ");
printf("\n 3 for Delete First ");
printf("\n 4 for Delete Last ");
printf("\n 5 for Display ");
printf("\n 6 for Search ");
printf("\n 7 for Exit ");
printf("\n Enter choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertFirst();
break;
case 2:
insertLast();
break;
case 3:
deleteFirst();
break;
case 4:
deleteLast();
break;
case 5:
display();
break;
case 6:
search();
break;
case 7:
exit(1);
default:
printf("\n Wrong Choice ");
}
}
}