-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list_queue.c
More file actions
150 lines (133 loc) · 3.03 KB
/
Copy pathlinked_list_queue.c
File metadata and controls
150 lines (133 loc) · 3.03 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
148
149
150
#include<stdio.h>
#include <stdlib.h>
typedef struct node //node of linked list
{
struct node *prev;
int info;
struct node *next;
}node_type;
void add_node(node_type **hd,node_type **tl,int num)
{
node_type *p;
p = (node_type*)malloc(sizeof(node_type));
if(p == NULL)
printf("\t** OUT OF MEMORY **\n");
else
{
p->info = num;
p->next = NULL;
if(*tl == NULL)
{
*tl = p;
*hd = p;
p->prev = NULL;
}
else
{
(*tl)->next = p;
p->prev = *tl;
*tl = p;
}
}
}
void print_forward(node_type *hd)
{
if(hd == NULL)
printf("\t** LINKED LIST EMPTY **\n");
else
{
printf("Elements in the linked list are : \n");
while(hd!=NULL)
{
printf("%d\n",hd->info);
hd = hd->next;
}
}
}
void print_reverse(node_type *tl)
{
if(tl == NULL)
printf("\t** LINKED LIST EMPTY **\n");
else
{
printf("Elements in the linked list are : \n");
while(tl != NULL)
{
printf("%d\n",tl->info);
tl = tl->prev;
}
}
}
void del_node(node_type **hd,node_type **tl,int key)
{
int loc=1;
node_type *temp;
temp = *hd;
while(temp->info!=key && temp->next!=NULL)
{
loc++;
temp=temp->next;
}
if(temp->next == NULL && temp->info==key)
{
printf("Found at location %d",loc);
(temp->prev)->next = NULL;
*tl = temp->prev;
return;
}
if(temp->info == key)
{
if(*hd == *tl)
{
*tl = NULL;
*hd = NULL;
}
else
{
(temp->prev)->next = temp->next;
(temp->next)->prev = temp->prev;
}
return;
}
printf("No such key found");
}
int main()
{
node_type *head = NULL,*tail = NULL;
int run = 1;
while(run)
{
printf("Type :- \n");
printf("1) INSERT NODE\n");
printf("2) DELETE NODE\n");
printf("3) PRINT FORWARD\n");
printf("4) PRINT BACKWARD\n");
int ch;
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter number :");
int num;
scanf("%d",&num);
add_node(&head,&tail,num);
break;
case 2: if(head == NULL)
printf("\n\t** LIST IS EMPTY **\n");
else
{
printf("Enter the key :");
int n;
scanf("%d",&n);
del_node(&head,&tail,num);
}
break;
case 3: print_forward(head);
break;
case 4: print_reverse(tail);
break;
case 5: run = 0;
break;
}
}
return 0;
}