-
Notifications
You must be signed in to change notification settings - Fork 255
/
Queue using LinkedList.c
154 lines (140 loc) · 2.23 KB
/
Queue using LinkedList.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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{int data;
struct node *next;
}*p,*tmp,*tmp1;
void insert_end(int);
void delete_beg();
void display();
void isEmpty();
int main()
{
int val,n;
p=NULL;
do
{printf("\n************************* MENU ************************");
printf("\n1.ENQUEUE");
printf("\n2.DEQUE");
printf("\n3.IS EMPTY");
printf("\n4.DISPLAY");
printf("\n5.EXIT");
printf("\nenter ur choice : ");
scanf("%d",&n);
switch(n)
{case 1: printf("\nenter the value ");
scanf("%d",&val);
insert_end(val);
break;
case 2:
delete_beg();
break;
case 3:
isEmpty();
break;
case 4: display();
break;
case 5: exit(0);
break;
default: printf("\n Wrong Choice!");
break;
}
printf("\ndo u want to cont... ");
}while('y'==getch());
}
void insert_end(int ele)
{
tmp=p;
tmp1=(struct node*)malloc(sizeof(struct node));
tmp1->data=ele;
tmp1->next=NULL;
if(p==NULL)
p=tmp1;
else
{
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=tmp1;
}
}
void insert_beg(int ele)
{
tmp=p;
tmp1=(struct node*)malloc(sizeof(struct node));
tmp1->data=ele;
tmp1->next=p;
p=tmp1;
}
void isEmpty(){
if(p==NULL)
printf("Queue is Empty");
else
{
printf("Queue is Not Empty");
}
}
void ldelete(int ele)
{
tmp=p;
struct node *pre=tmp;
while(tmp!=NULL)
{if(tmp->data==ele)
{ if(tmp==p)
{p=tmp->next;
free(tmp);
return;
}
else
{pre->next=tmp->next;
free(tmp);
return;
}
}
else
{ pre=tmp;
tmp=tmp->next;
}
}
printf("\n no match found!! ");
}
void delete_beg()
{
tmp=p;
if(p==NULL)
printf("\n no element to be deleted!! ");
else
{
printf("\nelement deleted - %d", p->data);
p=p->next;
}
}
void delete_end()
{
tmp=p;
struct node* pre;
if(p==NULL)
printf("\n no element to be deleted!! ");
else if(p->next==NULL)
{
printf("\nelement deleted - %d", p->data);
p=NULL;
}
else
{
while(tmp->next!=NULL){
pre=tmp;
tmp=tmp->next;
}
pre->next=NULL;
printf("\nelement deleted - %d", tmp->data);
}
}
void display()
{
tmp=p;
while(tmp!=NULL)
{printf("\n %d",tmp->data);
tmp=tmp->next;
}
}