This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p20_1511.c
119 lines (105 loc) · 1.97 KB
/
p20_1511.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
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 20/1511
// Queue Implementation
// Dynamic Memory Allocation
// Please review.
// Code does not meet required standard.
// Use SLL to implement !
#include<stdio.h>
#include<stdlib.h>
void enqueue();
void dequeue();
void display();
void exit_free();
struct node{
int data;
struct node *prev;
struct node *next;
};
struct node *FRONT = NULL;
struct node *REAR = NULL;
struct node *temp;
void main(){
int ch;
while(1){
printf("\n\n*** QUEUE OPTIONS ***");
printf("\n1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT");
printf("\nEnter choice: ");
scanf("%d", &ch);
switch(ch){
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: display(); break;
default: exit_free(); return;
}
}
}
// Insertion at end
// Choice 2
void enqueue(){
struct node *a = malloc(sizeof(struct node));
printf("\tEnter data: ");
scanf("%d", &(a->data));
if(FRONT == NULL && REAR == NULL){
a->prev = NULL;
a->next = NULL;
FRONT = a;
REAR = a;
}
else{
REAR->next = a;
a->prev = REAR;
a->next = NULL;
REAR = a;
}
printf("\tSuccessfully enqueued %d", a->data);
}
// Deletion of beginning node
// Choice 4
void dequeue(){
if(FRONT == NULL && REAR == NULL){
printf("\n\tQUEUE is Empty");
return;
}
temp = FRONT;
printf("\n\tSuccessfully dequeued %d", temp->data);
if(FRONT != REAR){
FRONT = FRONT->next;
FRONT->prev = NULL;
}
else{
FRONT = NULL;
REAR = NULL;
}
free(temp);
}
// Display DLL - FRONT to REAR
// Choice 7
void display(){
if(FRONT == NULL && REAR == NULL){
printf("\n\tQUEUE is Empty");
return;
}
printf("\n\tCuurent QUEUE >>>");
for(temp = FRONT; temp != NULL; temp = temp->next){
printf(" %d ", temp->data);
}
}
// Final de-allocation of DLL
// de-allocation from FRONT
// Choice 9
void exit_free(){
struct node *loc;
if(FRONT != NULL){
temp = FRONT;
loc = temp->next; // Next node
while(loc != NULL){
free(temp);
temp = loc;
loc = loc->next;
}
free(temp);
}
}