-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
StackUsingQueue.c
144 lines (124 loc) · 2.64 KB
/
StackUsingQueue.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
/* C Program to implement a Stack using Queue.Here we will try to realise STACK
using the properties OR functions of QUEUE and by making pop operation
costly*/
#include <malloc.h>
#include <stdio.h>
struct Node {
int data;
struct Node *next;
};
/*Maintaining a front,rear pointer
where insertion takes place at rear end and deletion at front
and O(1) time is taken for insertion by maintaining a rear pointer
*/
struct Node *front = NULL, *rear = NULL;
// Store size of queue
int qsize = 0;
// check whether queue is empty or not
int isEmpty(struct Node *q) {
if (q == NULL) {
// return true i.e Queue is empty
return 1;
}
return 0;
}
// Enqueue i.e insert at the REAR end
int enQueue(int item) {
struct Node *q;
// creation of new node
q = (struct Node *)malloc(sizeof(struct Node));
q->data = item;
q->next = NULL;
/* If queue is empty
i.e front is pointing to NULL update front*/
if (front == NULL) {
front = q;
} else {
(rear)->next = q;
}
rear = q;
// increase current queue size
qsize++;
return 0;
}
// Dequeue i.e delete at the FRONT end
int deQueue() {
int delElement;
/*Queue is empty
dequeue not possible*/
if (isEmpty(front)) {
return -1;
} else {
// store the dequeued element
delElement = front->data;
(front) = (front)->next;
if ((front) == NULL) {
(rear) == NULL;
}
// reduce the current queue size
qsize--;
}
return delElement;
}
void push(int data) { enQueue(data); }
int pop() {
int i, element;
/* dequeue upto n-1 elements in the queue
and enqueue the respective dequeued element
at the REAR end of the queue*/
for (i = 0; i < qsize - 1; i++) {
element = deQueue();
enQueue(element);
}
/* dequeue the element which will be first value of the STACK
and return the element*/
element = deQueue();
if (element == -1) {
printf("\n Stack is empty");
return 0;
}
return element;
}
void display(struct Node *temp) {
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
int main() {
int i, val, size;
printf("Enter the size: ");
scanf("%d", &size);
printf("Enter the numbers:\n");
for (i = 0; i < size; i++) {
scanf("%d", &val);
push(val);
}
printf("Stack size:%d\n", qsize);
printf("Popped Elements are:\n");
while (qsize > 0) {
printf("\n%d ", pop());
}
printf("\nStack size:%d", qsize);
return 0;
}
/*
Sample input/output:
Enter the size: 5
Enter the numbers:
1
2
3
4
5
Stack Size:5
Popped Elements are:
5
4
3
2
1
Stack Size:0
Time complexity: O(1) for push, O(n) for pop
Space complexity: O(n)
*/