-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathStack Implementation.c
99 lines (93 loc) · 1.58 KB
/
Stack Implementation.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
#include<stdio.h>
#include<stdlib.h>
int top=0;
int stack[];
void traverse();
void peek();
void pop();
void push();
int isFull();
int isEmpty();
void main(){
int i=0,data,n=0;
printf("Enter The Size:\n");
scanf("%d",&n);
while(1){
printf("1: Push\n2: Pop\n3: Peek\n4: Traverse\n5: Exit\n");
scanf("%d",&i);
switch(i){
case 1: {
printf("Enter The Data\n");
scanf("%d",&data);
push(data,n);
break;
}
case 2: {
pop();
break;
}
case 3:{
peek();
break;
}
case 4:{
traverse();
break;
}
case 5:{
exit(0);
}
default: printf("Come Again!!");
}
}
}
void push(int data, int n){
if(isFull(n+1)){
printf("Stack is Full\n\n");
}
else{
stack[++top]=data;
printf("\n%d is pushed\n\n",data);
}
}
int isFull(int n){
return (top==n-1)?1:0;
}
void pop(){
int data;
if(isEmpty()){
printf("Stack is Empty\n");
}
else{
if(top==0){
printf("Stack is Empty\n");
}
else{
data=stack[top--];
printf("%d is popped\n",data);
}
}
}
int isEmpty(){
return (top==-1)?1:0;
}
void peek(){
if(isEmpty()){
printf("Cannot Peek Further\n");
}
else{
printf("Peek = %d\n",stack[top]);
}
}
void traverse(){
int i;
if(isEmpty()){
printf("Cannot be Traversed\n");
}
else{
for(i=top;i>0;i--){
printf("%d ",stack[i]);
}
printf("\n\n");
}
}