-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ225ImplementStackUsingQueues.c
More file actions
131 lines (104 loc) · 2.84 KB
/
Q225ImplementStackUsingQueues.c
File metadata and controls
131 lines (104 loc) · 2.84 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
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
/*
Description:
Implement a last-in-first-out (LIFO) stack using only two queues.
The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
*/
/*
References:
https://www.geeksforgeeks.org/queue-set-1introduction-and-array-implementation/
https://leetcode.com/problems/implement-stack-using-queues/discuss/1855829/week_9-Implement-Stack-using-Queues-C
*/
//implementing queues
struct Queue {
int front, rear, size;
unsigned capacity;
int* array;
};
struct Queue* createQueue(unsigned capacity){
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity-1;
queue->array = (int*)malloc(queue->capacity * sizeof(int));
return queue;
}
int isFull(struct Queue* queue){
return (queue->size == queue->capacity);
}
int isEmpty(struct Queue* queue){
return (queue->size == 0);
}
void enqueue(struct Queue* queue, int item){
if(isFull(queue)){
return;
}
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
}
int dequeue(struct Queue* queue){
if(isEmpty(queue)){
return INT_MIN;
}
int item = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size = queue->size - 1;
return item;
}
int front(struct Queue* queue){
if(isEmpty(queue)){
return INT_MIN;
}
return queue->array[queue->front];
}
int rear(struct Queue* queue){
if(isEmpty(queue)){
return INT_MIN;
}
return queue->array[queue->rear];
}
//Stack using queue
typedef struct {
int size;
struct Queue *queue;
} MyStack;
MyStack* myStackCreate() {
MyStack *obj = (MyStack *)malloc(sizeof(MyStack));
obj->queue = createQueue(100); //creating a queue with size initialised to 100
return obj;
}
void myStackPush(MyStack* obj, int x) {
if(isFull(obj->queue)){
return;
}
/*
Enqueue element to the back of the queue
Then, shift all the elements in front of the newly inserted element to the back of the queue using dequeue+enqueue
*/
enqueue(obj->queue, x);
for(int i = 1; i < obj->queue->size; i++){
enqueue(obj->queue, dequeue(obj->queue));
}
}
int myStackPop(MyStack* obj) {
int popElement;
if(isEmpty(obj->queue)){
return;
}
//since we've made newly inserted element first in queue during the push operation, simply dequeue to pop
return dequeue(obj->queue);
}
int myStackTop(MyStack* obj) {
return front(obj->queue);
}
bool myStackEmpty(MyStack* obj) {
return isEmpty(obj->queue);
}
void myStackFree(MyStack* obj) {
while(obj->queue->size > 0){
dequeue(obj->queue);
}
}