-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
55 lines (53 loc) · 800 Bytes
/
stack.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
#include <stdio.h>
typedef struct stack
{
char data[64];
int top;
} stack;
int empty(stack *p)
{
return (p->data == -1);
}
int top(stack *p)
{
return (p->data[p->top]);
}
void push(stack *p, char a)
{
p->data[++(p->top)] = a;
return;
}
void pop(stack *p)
{
if (!empty(&p))
{
p->top--;
}
return;
}
int main()
{
stack s;
s.top = -1;
char str[10] = "ABCDE";
int i, len;
len = sizeof(str);
for (i = 0; i < len; i++)
{
push(&s, str[i]);
}
printf(" string\n ");
for (i = 0; i < len; i++)
{
printf("%c", s.data[i]);
}
printf("\n");
printf(" reverse string\n ");
for (i = 0; i < len; i++)
{
printf("%c", top(&s));
pop(&s);
}
printf("\n");
return 0;
}