-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
45 lines (41 loc) · 666 Bytes
/
Copy pathstack.c
File metadata and controls
45 lines (41 loc) · 666 Bytes
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
#include<stdio.h>
#define capacity 3
int stack[capacity];
int top=-1;
void push(int x){
if(top<capacity-1){
top=top+1;
stack[top]=x;
printf("%d Added successfully.\n",x);
}else{
printf("Exception!No space.\n");
}
}
int pop(){
if(top>=0){
int val=stack[top];
top=top-1;
return val;
}
printf("Exception from Pop!Empty Stack\n");
return -1;
}
int peek(){
if(top>=0){
return stack[top];
}else{
printf("Exception from peek.\n");
return -1;
}
}
int main(){
printf("Implementing Stack in C.\n");
peek();
push(10);
push(20);
push(30);
printf("Pop item:%d\n",pop());
push(40);
printf("Top of Stack %d",peek());
return 0;
}