Skip to content

Commit 7b3e0f1

Browse files
committed
new question added
1 parent c2ff05c commit 7b3e0f1

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ TODO:
159159
- [Implement a stack using arrays & linked-list](/stacks-and-queues/question1.c)
160160
- [Implement a queue using circular array](/stacks-and-queues/question2.c)
161161
- [Implement a queue using two stacks](/stacks-and-queues/question3.c)
162-
- [](/stacks-and-queues/question4.c)
162+
- [Implement a stack using queues](/stacks-and-queues/question4.c)
163163

164164

165165
## Some important concepts to solve algos better

stacks-and-queues/question3.c

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
Implement a queue using two stacks
33
4+
It means ENQUEUE and DEQUEUE should use push and pop operations inside it.
5+
46
If n elements are to be inserted on to a queue implemented using two stacks and m have to be deleted
57
Push Pop
68
Best case m+n 2m
@@ -11,39 +13,39 @@ Work Case 2n n+m
1113
#include <stdlib.h>
1214
#define MAX 50
1315

14-
void push(int arr[],int data,int *top){
15-
if(*top == MAX-1){
16+
void push(int arr[],int data, int *ptr){
17+
if(*ptr == MAX-1){
1618
printf("overflow\n");
1719
return;
18-
}
19-
arr[++*top]=data;
20+
}
21+
arr[++(*ptr)] = data;
2022
}
2123

22-
void insert(int arr[],int data, int *top){
23-
push(arr,data,top);
24+
int pop(int arr[],int *ptr){
25+
if(*ptr == -1){
26+
printf("underflow\n");
27+
return -1;
28+
}
29+
int data = arr[(*ptr)--];
30+
return data;
2431
}
2532

26-
int pop(int arr[], int *top){
27-
int data = arr[*top];
28-
*top--;
29-
return data;
33+
void enqueue(int arr[],int data, int *top1){
34+
push(arr,data,top1);
3035
}
3136

32-
int delete(int arr2[], int arr1[],int *top2, int *top1){
33-
int elm;
34-
if(*top2==-1){
35-
if(*top1==-1){
36-
return -1; //this can be an error code
37-
}
37+
int dequeue(int arr1[], int arr2[],int *top1, int *top2){
38+
int data, result;
39+
if(*top2 == -1 && *top1 == -1){
40+
return -1;
3841
}else{
39-
while(*top2 >= 0){
40-
int data = pop(arr2, *top2);
41-
*top2--;
42-
push(arr1,data,*top1);
42+
while(*top1 != -1){
43+
data = pop(arr1,top1);
44+
push(arr2,data,top2);
4345
}
44-
elm = pop(arr1,top1);
45-
return elm;
46+
result = pop(arr2,top2);
4647
}
48+
return result;
4749
}
4850

4951
int main(){
@@ -60,15 +62,14 @@ int main(){
6062
switch(step){
6163
case 1: printf("enter element to be pushed\n");
6264
scanf("%d",&elm);
63-
insert(stack2,elm, &top2);
65+
enqueue(stack1,elm, &top1);
6466
break;
65-
case 2: result = delete(stack2,stack1,&top2,&top1);
66-
if(result == -1){
67+
case 2: result = dequeue(stack1, stack2,&top1, &top2);
68+
if(result < 0){
6769
printf("already empty\n");
6870
}else{
69-
printf("%d was popped\n", result);
70-
}
71-
71+
printf("%d was deleted\n", result);
72+
}
7273
break;
7374
case 3: exit(1);
7475
break;

stacks-and-queues/question4.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/*
2+
Implement a stack using queues
23
34
*/

0 commit comments

Comments
 (0)