-
Notifications
You must be signed in to change notification settings - Fork 2
/
doubly linked list.c
169 lines (165 loc) · 3.52 KB
/
doubly 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node*next;
struct node*previous;
}*newnode,*head,*tem;
int node_count=0;
// add node at last
void add()
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data: ");
scanf("%d",&(newnode->data));
newnode->next=0;
newnode->previous=0;
if (head==0)
{
head=newnode;
temp=newnode;
node_count++;
}
else
{
temp->next=newnode;
newnode->previous=temp;
temp=newnode;
node_count++;
}
printf("\n\n");
return;
}
// display node
void display()
{
struct node*display=head;
if (head==0)
{
printf("List is empty\n\n");
}
else
{
printf("\nYour Node is: ");
while(display!=0)
{
printf("%d->",display->data);
display=display->next;
}
printf("\n\n");
}
printf("\n\n");
return;
}
// number of nodes printing
void number_of_node()
{
printf("The number of nodes till now is: %d\n",node_count);
printf("\n\n");
return;
}
// delete a node
void del()
{
int flag=1;
if(head==0)
{
printf("List is empty\n\n");
return;
}
int index;
printf("enter a node index to delete a node, remember node index starts from 0: ");
scanf("%d",&index);
struct node*t=head;
if(index==0)
{
head=head->next;
free(t);
node_count--;
}
else if(index==(node_count-1))
{
t=temp;
temp=temp->previous;
temp->next=0;
free(t);
node_count--;
}
else if(index>0 && index<node_count)
{
while (index!=0)
{
t=t->next;
index-=1;
}
t->next->previous=t->previous;
t->previous->next=t->next;
free(t);
node_count--;
}
else
{
printf("Index out of range\n\n");
return;
}
printf("node deleted\n\n");
return;
}
// insert at middle
void mid()
{
int index;
printf("enter index number after which you want to insert node: ");
scanf("%d",&index);
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data: ");
scanf("%d",&(newnode->data));
newnode->next=0;
newnode->previous=0;
struct node*t2=head;
while(index!=0)
{
t2=t2->next;
index-=1;
}
newnode->previous=t2;
newnode->next=t2->next;
t2->next=newnode;
newnode->next->previous=newnode;
printf("\n\n");
node_count++;
return;
}
int main()
{
int choice;
while (choice!=0)
{
printf("Enter your choice:\n Press 1 to add node at last\n Press 2 display node\n Press 3 to delete a node\n Press 4 to insert node at middle\n Press 8 to display number of nodes\n Press 9 to clear screen\n Press 0 to exit\n");
scanf("%d",&choice);
switch(choice)
{
case 0:
break;
case 1:
add();
break;
case 2:
display();
break;
case 3:
del();
break;
case 4:
mid();
break;
case 8:
number_of_node();
break;
case 9:
system("cls");
break;
}
}
}