-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqStack.c
59 lines (55 loc) · 1.04 KB
/
SqStack.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
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxSize 10
typedef int ElementType;
typedef struct{
ElementType Data[MaxSize];
int top;
}SqStack;
// Init a Stack
// SqStack CreateStack(int MaxSize)
SqStack *CreateStack()
{
SqStack * ss;
ss = (SqStack *)malloc(sizeof(SqStack));
ss->top = -1;
return ss;
}
// Check whether the stack is full or not
bool IsFull(SqStack *ss)
{
return(ss->top == (MaxSize - 1));
}
// Push an element to the stack
void Push(ElementType item, SqStack *ss)
{
// printf("Not implemented\n");
ss->top ++;
ss->Data[ss->top] = item;
}
// Check the stack is full or not
bool IsEmpty(SqStack *ss)
{
return(ss->top==-1);
}
// Pop an element from the stack
ElementType Pop(SqStack *ss)
{
if(IsEmpty(ss)) return NULL;
ss->top--;
return ss->Data[ss->top + 1];
}
int main(void)
{
SqStack *ss;
ElementType elem = 1;
ss = CreateStack();
printf("IsEmpty %d\n", IsEmpty(ss));
printf("IsFull %d\n",IsFull(ss));
Push(elem, ss);
printf("%d\n", ss->Data[0]);;
printf("%d\n", Pop(ss));
free(ss);
return 0;
}