Skip to content

Commit c2ff05c

Browse files
committed
new question added
1 parent 682d6ea commit c2ff05c

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +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)
162163

163164

164165
## Some important concepts to solve algos better
@@ -171,6 +172,8 @@ TODO:
171172
- Linear hashing is (h(k) + i)modm (where m is the size of the hash table, h(k) is the hash function that takes the key k and returns a value i is the parameter that is incremented to get different values)
172173
- Subarrays of an array are always contiguous whereas subsequence may not be contiguous
173174
- There are only two ways to make any data structure one is to use an array (where size is fixed and memory is contiguous) OR you can use the heap memory (structures and linked list). So Array and linked list or combo of these two are used to implement any data structure. In most cases linked list takes more time to do operations if data structure is implemented using it. But advantage is dynamic memory allocation
175+
- For INFIX to POSTFIX conversion, data structure used is stack. In stack all the operators are stored. For evaluation of POSTFIX stack is used to store operands
176+
- Evaluating and expression = convert from INFIX to postfix --> Evaluate POSTFIX. Time complexity: O(n)
174177

175178
# Topic1: Introduction
176179

nextquestions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@
2626
- how to accurately find the size of the hashTable
2727
- BST question5 hashing to be done
2828
- hashing question7 method3 to be done using BST
29-
- question4 hashing to be done
29+
- question4 hashing to be done
30+
- stacks and queues better implementation to be done
31+
- stacks and queues INFIX to POSTFIX & evaluating POSTFIX

stacks-and-queues/question3.c

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,75 @@ If n elements are to be inserted on to a queue implemented using two stacks and
55
Push Pop
66
Best case m+n 2m
77
Work Case 2n n+m
8-
*/
8+
*/
9+
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#define MAX 50
13+
14+
void push(int arr[],int data,int *top){
15+
if(*top == MAX-1){
16+
printf("overflow\n");
17+
return;
18+
}
19+
arr[++*top]=data;
20+
}
21+
22+
void insert(int arr[],int data, int *top){
23+
push(arr,data,top);
24+
}
25+
26+
int pop(int arr[], int *top){
27+
int data = arr[*top];
28+
*top--;
29+
return data;
30+
}
31+
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+
}
38+
}else{
39+
while(*top2 >= 0){
40+
int data = pop(arr2, *top2);
41+
*top2--;
42+
push(arr1,data,*top1);
43+
}
44+
elm = pop(arr1,top1);
45+
return elm;
46+
}
47+
}
48+
49+
int main(){
50+
int stack1[MAX],stack2[MAX];
51+
int top1 = -1, top2 = -1;
52+
int step, elm, result;
53+
54+
while(1){
55+
printf("1. PUSH element\n");
56+
printf("2. POP element\n");
57+
printf("3.EXIT \n");
58+
scanf("%d",&step);
59+
60+
switch(step){
61+
case 1: printf("enter element to be pushed\n");
62+
scanf("%d",&elm);
63+
insert(stack2,elm, &top2);
64+
break;
65+
case 2: result = delete(stack2,stack1,&top2,&top1);
66+
if(result == -1){
67+
printf("already empty\n");
68+
}else{
69+
printf("%d was popped\n", result);
70+
}
71+
72+
break;
73+
case 3: exit(1);
74+
break;
75+
}
76+
}
77+
78+
return 0;
79+
}

stacks-and-queues/question4.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*
2+
3+
*/

0 commit comments

Comments
 (0)