-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueue.c
More file actions
86 lines (70 loc) · 1.43 KB
/
Queue.c
File metadata and controls
86 lines (70 loc) · 1.43 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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct list_el
{
int val;
struct list_el * next;
}item_default={0,NULL}; //Default values
typedef struct list_el node;
node * head = NULL;
void enqueue(){
int a;
printf("Enter: ");
scanf("%d",&a);
node * linked = (node * )(malloc(sizeof(node)));
linked->val = a;
linked->next = head;
head = linked;
}
int dequeue(){
int a;
if (head == NULL)
{
return INT_MIN;
/* code */
}
else if ( head->next == NULL)
{
a = head->val;
free(head);
head = NULL;
return a;
}
else {
node * curr = head;
node * curr1 = NULL;
while(curr->next != NULL){
curr1 = curr;
curr = curr->next;
}
a = curr->val;
curr1->next = NULL;
free(curr);
return a;
/* code */
}
}
int main(int argc, char const *argv[])
{
int a,loop =1;
while(loop == 1)
{
int choice;
printf("Enter the choice 1-Enqueue, 2-Dequeue, 3-stop\n");
scanf("%d",&choice);
switch(choice){
case 1:
enqueue();
break;
case 2:
a= dequeue();
printf("%d\n",a );
break;
case 3:
loop = 0;
break;
}
}
return 0;
}