-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.c
More file actions
93 lines (83 loc) · 1.34 KB
/
Copy pathStack.c
File metadata and controls
93 lines (83 loc) · 1.34 KB
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
#include<stdio.h>
#include<limits.h>
#include<stdlib.h>
typedef struct Stack
{
int top;
unsigned cap;
int* arr;
}Stack;
int size=sizeof(Stack);
Stack* create_stack(unsigned cap)
{
Stack* stack=malloc(size);
stack->cap=cap;
stack->top=-1;
stack->arr=malloc(stack->cap*sizeof(int));
return stack;
}
int is_full(Stack* stack)
{
return stack->top==stack->cap-1;
}
int is_empty(Stack* stack)
{
return stack->top==-1;
}
void push(Stack* stack,int element)
{
if(is_full(stack))
{
return;
}
stack->arr[++stack->top]=element;
}
int pop(Stack* stack)
{
if(is_empty(stack))
{
return INT_MIN;
}
return stack->arr[stack->top--];
}
int peek(Stack* stack)
{
if(is_empty(stack))
{
printf("Stack is empty\n");
return INT_MIN;
}
return stack->arr[stack->top];
}
void display_stack(Stack* stack)
{
if(is_empty(stack))
{
printf("Stack is empty\n");
return;
}
int tmp=stack->top;
printf("\nElement present in the stack: ");
while(tmp!=-1)
{
printf("\t%d\t",stack->arr[tmp]);
tmp--;
}
}
int main(void)
{
int n;
printf("Enter size of Stack: ");
scanf("%d",&n);
Stack* stack=create_stack(n);
printf("Enter Elements of Stack\n");
for(int i=0;i<n;i++)
{
int tmp;
scanf("%d",&tmp);
push(stack,tmp);
}
printf("The Top element in stack is: %d\n",peek(stack));
printf("Element poped off: %d",pop(stack));
display_stack(stack);
}