-
Notifications
You must be signed in to change notification settings - Fork 2
/
question11.c
76 lines (67 loc) · 1.41 KB
/
question11.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
/*
Implement a queue using linked list
//The circular array case rear was always incremented before inserting element and front was incrementing
before deleting element, to save the space. In this case both rear and front can point to the same
element in the begining and while removing no need to increment front for removal. Front is incremented
after removal
*/
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *prev;
struct node *next;
} *rear = NULL,*front = NULL;
struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->prev = NULL;
temp->next = NULL;
return temp;
}
void enqueue(int data){
struct node *temp = newNode(data);
if(!rear){
rear = front = temp;
return;
}
rear->next = temp;
rear = rear->next;
}
int dequeue(){
if(!front){
return -1;
}
struct node *temp = front;
front = front->next;
if(!front){
front = rear= NULL;
}
return temp->data;
}
int main(){
int step; int elm;
int data;
while(1){
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Exit\n");
scanf("%d",&step);
switch(step){
case 1: printf("enter element\n");
scanf("%d", &elm);
enqueue(elm);
break;
case 2: data = dequeue();
if(data < 0){
printf("queue is now empty\n");
}else{
printf("%d was removed\n", data);
}
break;
case 3: exit(1);
break;
}
}
return 0;
}