-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulstack.c
More file actions
62 lines (59 loc) · 957 Bytes
/
mulstack.c
File metadata and controls
62 lines (59 loc) · 957 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
struct stack
{
int capacity;
int size;
int top;
int *arr;
};
struct stack *createstack(int n)
{
struct stack *t=(struct stack *)malloc(sizeof(struct stack));
t->arr=(int *)malloc(n*sizeof(int));
t->top=-1;
t->capacity=n;
t->size=0;
return t;
}
void push(struct stack *s,int ele)
{
if(s->size==s->capacity)
{
printf("Stack overflow");
return;
}
s->arr[++s->top]=ele;
s->size=s->size+1;
}
int pop(struct stack *s)
{
int ele;
if(s->size==0)
{
printf("Stack empty");
return INT_MIN;
}
ele=s->arr[s->top--];
s->size=s->size-1;
return ele;
}
int peek(struct stack *s)
{
if(s->size==0)
{
printf("Stack empty");
return INT_MIN;
}
return(s->arr[s->top]);
}
int main()
{
struct stack *s=createstack(5);
push(s,40);
printf("the top of the stack is %d",peek(s));
printf("the popped element is %d",pop(s));
printf("the top of the stack is %d",peek(s));
return 0;
}