-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b3e0f1
commit 30b56dc
Showing
4 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,101 @@ | ||
/* | ||
Implement a stack using queues | ||
*/ | ||
It can be implemented in two ways | ||
One is while pushing, pushing in one Q and while popping, popping from this Q and pushing in other Q and then | ||
popping that one | ||
OR | ||
While pushing, pushing in empty Q and dequeing the other one and enqueuing in this one for every push this | ||
can be done. | ||
Time complexity: (depending on the method that is applied) | ||
For each push: O(n) | ||
For each pop: O(1) | ||
OR | ||
For each push: O(1) | ||
For each pop: O(n) | ||
*/ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#define MAX 5 | ||
|
||
void enqueue(int arr[],int data,int *front, int *rear){ | ||
int size = MAX; | ||
*rear = (*rear+1)%size; | ||
if(*rear == *front){ | ||
printf("overflow\n"); | ||
if(*rear != 0){ | ||
*rear = *rear-1; | ||
}else{ | ||
*rear = size-1; | ||
} | ||
return; | ||
} | ||
arr[*rear]=data; | ||
} | ||
|
||
int dequeue(int arr[], int *front, int *rear){ | ||
int data; | ||
int size = MAX; | ||
if(*front == *rear){ | ||
printf("already empty\n"); | ||
return -1; | ||
}else{ | ||
*front = (*front+1)%size; | ||
data = arr[*front]; | ||
} | ||
return data; | ||
} | ||
|
||
void push(int q1[],int elm, int *f1, int *r1){ | ||
enqueue(q1,elm,f1,r1); | ||
} | ||
|
||
int pop(int q1[],int q2[],int *f1,int *r1, int *f2, int *r2){ | ||
int temp,data; | ||
if((*f1 == *r1) && (*f2==*r2)){ | ||
printf("already empty\n"); | ||
return -1; | ||
}else{ | ||
while(*f1!=*r1){ | ||
temp = dequeue(q1,f1,r1); | ||
enqueue(q2,temp,f2,r2); | ||
} | ||
data = temp; | ||
while(*f2 != *r2){ | ||
temp = dequeue(q2,f2,r2); | ||
enqueue(q1,temp,f1,r1); | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
int main(){ | ||
int q1[MAX],q2[MAX],step,elm,result; | ||
int f1=0,r1=0,f2=0,r2=0; | ||
|
||
while(1){ | ||
printf("1. PUSH element\n"); | ||
printf("2. POP element\n"); | ||
printf("3.EXIT \n"); | ||
scanf("%d",&step); | ||
|
||
switch(step){ | ||
case 1: printf("enter element to be pushed\n"); | ||
scanf("%d",&elm); | ||
push(q1,elm, &f1,&r1); | ||
break; | ||
case 2: result = pop(q1,q2,&f1,&r1,&f2,&r2); | ||
if(result < 0){ | ||
printf("already empty\n"); | ||
}else{ | ||
printf("%d was deleted\n", result); | ||
} | ||
break; | ||
case 3: exit(1); | ||
break; | ||
} | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Design a stack such that get Minimum should be O(1) | ||
NOTE: When stack is to be designed in order to do something the core functionalities of the stack should | ||
still hold in that design. Eg: Pushing and popping can be done in O(1) time in a stack. This should be | ||
true always | ||
METHOD1: | ||
Maintain and another stack which will store minimum so far values. | ||
For each push into the main stack, the min is compared and if there is a different value it is pushed onto | ||
this stack. For every pop from the main stack if the popped off element is equal to the min, then the min is | ||
popped off as well. If the main stack contains duplicate values, then the min so far stack should also | ||
contain duplicate to make sure that popping off is consistent and works correctly. | ||
Time complexity: Both pop and push in O(1) | ||
Space complexity: O(n) //worst case | ||
METHOD2: | ||
Maintain a variable that will store the min value so far for every push done. For every push done, | ||
if there is a new minimum, instead of pushing this minimum onto the stack, subtract the min so far value from | ||
this min and push that value on the stack. Eg if minimum so far is 9 and new min is 2 push 2-9 that is -7 | ||
on the main stack. Like this make the stack. | ||
While popping off, each time compare the number on the stack with the min so far, if the number is bigger | ||
pop that number else popp of the min so far value. Also update the min so far value by subtracting the smaller | ||
value on the stack from it. Eg if the min so far was 2 (which was popped off) and value on stack was -7, | ||
new min so far value should be 2-(-7) which is 9. This way the whole algo will work. | ||
Time complexity: O(1) | ||
Space complexity: O(1) | ||
*/ |