-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.c
113 lines (107 loc) · 2.09 KB
/
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
//
// main.c
// test
//
// Created by Vicky_Jha on 23/11/19.
// Copyright © 2019 test. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int arr[MAX];
int top = -1;
void push(void);
void pop(void);
void peek(void);
void traverse(void);
int isEmpty(void);
int isFull(void);
int main(int argc, const char * argv[]) {
int ch;
while (1)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.peek\n");
printf("4.traverse\n");
printf("5.isEmpty\n");
printf("6.isFull\n");
printf("7.Quit\n");
printf("Enter your choice:\n");
scanf("%d",&ch);
switch (ch)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : peek();
break;
case 4 : traverse();
break;
case 5 : if(isEmpty())
printf("Empty\n");
else
printf("Not empty\ncurrent size: %d\n",top+1);
break;
case 6:if(isFull())
printf("Full\n");
else
printf("Not Full\ncurrent size: %d\n",top+1);
break;
case 7:exit(1);
break;
default : printf("Invalid input\n\n");
break;
}
}
}
void push()
{
if(!isFull())
{
int n;
printf("Enter an element to push:\n");
scanf("%d",&n);
top++;
arr[top] = n;
printf("Element %d Pushed\n",n);
}
else
printf("Overflow\n");
}
void pop()
{
if(!isEmpty())
{
printf("Element %d Popped\n",arr[top]);
top--;
}
else
printf("Underflow\n");
}
void peek()
{
printf("Top element is %d\n",arr[top]);
}
void traverse()
{
for (int i = 0; i <= top; i++)
{
printf("%d\n",arr[i]);
}
}
int isEmpty()
{
if(top == -1)
return 1;
else
return 0;
}
int isFull()
{
if(top == MAX)
return 1;
else
return 0;
}