Skip to content

Commit 9f0fb89

Browse files
authored
Add files via upload
1 parent fa7d765 commit 9f0fb89

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

main.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#define MAX 100
4+
5+
typedef struct {
6+
int top;
7+
int Stack[MAX];
8+
}stack;
9+
10+
void initStack(stack *s){
11+
s->top = 0;
12+
}
13+
14+
int isFull (stack *s) {
15+
if (s->top == MAX)
16+
return 1;
17+
else
18+
return 0;
19+
}
20+
21+
int isEmpty (stack *s) {
22+
if (s->top == 0)
23+
return 1;
24+
else
25+
return 0;
26+
}
27+
28+
int pop (stack *s, int *x) {
29+
if (isEmpty(s))
30+
return 0;
31+
else {
32+
*x = s->Stack[--s->top];
33+
return 1;
34+
}
35+
}
36+
37+
int push (stack *s, int x) {
38+
if (isFull(s))
39+
return 0;
40+
else {
41+
s->Stack[s->top++] = x;
42+
return 1;
43+
}
44+
}
45+
46+
int peek (stack *s, int *x) {
47+
if (isEmpty(s))
48+
return 0;
49+
else {
50+
int peaky = s->top - 1;
51+
*x = s->Stack[peaky];
52+
return 1;
53+
}
54+
}
55+
56+
void printStack(stack *s) {
57+
if (isEmpty(s))
58+
return;
59+
else {
60+
int i = s->top-1;
61+
while( i >= 0) {
62+
printf("%d ", s->Stack[i]);
63+
i--;
64+
} printf("\n");
65+
}
66+
}
67+
68+
int main(int argc, char **argv)
69+
{
70+
stack s;
71+
int x;
72+
initStack(&s);
73+
push(&s, 5);
74+
push(&s, 7);
75+
push(&s, 15);
76+
printStack(&s);
77+
pop(&s, &x);
78+
printf("poped value = %d \n", x);
79+
printStack(&s);
80+
peek(&s, &x);
81+
printf("peeked value = %d\n", x);
82+
printStack(&s);
83+
return 0;
84+
}

0 commit comments

Comments
 (0)