-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamic_Stack.c
145 lines (134 loc) · 2.84 KB
/
Dynamic_Stack.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
145
dynami_stack
//
// main.c
// stackPrac
//
// Created by Vicky_Jha on 26/10/19.
// Copyright © 2019 test. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
//#define MAX 5;
int top = -1;
struct stack
{
int *A;
int MAX_SIZE;
int size;
int *B;
};
struct stack *pt;
void newStack(int size)
{
pt = (struct stack*)malloc(sizeof(struct stack));//what is the need?
pt -> A = (int*)calloc(size , sizeof(int));
pt -> MAX_SIZE = size;
}
int isEmpty()
{
if(top == -1)
{
return 1;
}
else
return 0;
}
int isFull()
{
if(top == pt -> MAX_SIZE - 1)
return 1;
else
return 0;
}
void push()
{
int n;
if(isFull())
{
printf("Stack is FUll\n");
}
else
{
printf("Enter an element to push\n");
scanf("%d",&n);
top++;
pt -> A[top] = n;
printf("Element entered \n");
}
}
void pop()
{
if(isEmpty())
{
printf("Stack is Empty\n");
}
else{
top--;
printf("Element Popped\n");
}
}
void Top()
{
printf("%d\n",pt -> A[top]);
}
void display()
{
if(isEmpty())
{
printf("Empty\n");
}
else{
printf("Stack is :\n");
for(int i = top;i >= 0; --i)
{
printf("%d\n",pt -> A[i]);
}
printf("\n");
}
}
int main() {
int choice;
int sz,newSize;
printf("Enter Max Size of Stack\n");
scanf("%d",&sz);
newStack(sz);
printf("Enter your choice\n1 for Push\n2 for Pop\n3 for Top element\n4 for Display\n5 exit\n6 to Change MaxSize of Stack\n");
while(1)
{
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:Top();
break;
case 4:display();
break;
case 5:exit(0);
break;
case 6:printf("Enter New Max Size of the Stack\n");
scanf("%d",&newSize);
if(newSize>0)
{
if(newSize < pt -> MAX_SIZE)
{
for (int i=0;i<pt -> MAX_SIZE - newSize;i++)
top--;
// pt -> B = (int*)realloc(pt -> A, newSize*sizeof(int));
pt -> MAX_SIZE = newSize;
}
else
// pt -> B = (int*)realloc(pt -> A, newSize*sizeof(int));
pt -> MAX_SIZE = newSize;
}
else
printf("Size cannot be 0 or negative\n");
break;
default:printf("Invalid Option\n");
break;
}
}
return 0;
}