-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdoubly linked list.c.txt
More file actions
147 lines (147 loc) · 2.41 KB
/
doubly linked list.c.txt
File metadata and controls
147 lines (147 loc) · 2.41 KB
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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
struct node*next;
int data;
struct node*prev;
};
struct node*start=NULL;
void createll();
void disp();
void insertbeg();
void insertend();
void deletebeg();
void deleteend();
void deletelist();
void main()
{
int ch;
do
{
printf("\n 1.create \n 2.display \n 3.insert in beginning \n 4. insert at end \n 5.delete beginning \n 6.delete at end \n 7.delete list");
printf("\n enter your choice");
scanf("%d", &ch);
switch(ch)
{
case 1: createll();
break;
case 2: disp();
break;
case 3: insertbeg();
break;
case 4: insertend();
break;
case 5: deletebeg();
break;
case 6: deleteend();
break;
case 7: deletelist();
break;
case 8: printf("\n exit");
break;
default: printf("\n invalid choice");
}
}
while(ch!=8);
getch();
}
void createll()
{ struct node*newnode,*ptr;
int num;
printf("\n enter -1 to end");
printf("\n enter the data");
scanf("%d",&num);
while(num!=-1)
{
if(start==NULL)
{
newnode=(struct node*)malloc(sizeof (struct node));
newnode->prev=NULL;
newnode->data=num;
newnode->next=NULL;
start=newnode;}
else
{
ptr=start;
newnode=(struct node*)malloc(sizeof (struct node));
newnode->data=num;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
newnode->prev=ptr;
newnode->next=NULL;
}
printf("\n enter the data");
scanf("%d", & num);
}
getch();
}
void disp()
{
struct node*ptr;
ptr=start;
while(ptr!=NULL)
{
printf("\t %d", ptr->data);
ptr=ptr->next;
}
getch();
}
void insertbeg()
{
struct node*newnode;
int num;
printf("\n enter the data");
scanf("%d", &num);
newnode = (struct node*)malloc(sizeof (struct node));
newnode->data=num;
start->prev=newnode;
newnode->next=start;
newnode->prev=NULL;
start=newnode;
getch();
}
void insertend()
{
struct node*ptr,*newnode;
int num;
printf("\n enter the data");
scanf("%d", &num);
newnode = (struct node*) malloc (sizeof (struct node));
newnode->data=num;
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
newnode->prev=ptr;
newnode->next=NULL;
getch();
}
void deletebeg()
{
struct node*ptr;
ptr=start;
start=start->next;
start->prev=NULL;
free(ptr);
getch();
}
void deletebeg()
{
struct node*ptr;
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->prev->next=NULL;
free(ptr);
getch();
}
void deletelist()
{
while(start!=NULL)
start=deletebeg(start);
getch();
}