-
Notifications
You must be signed in to change notification settings - Fork 0
/
mystack.c
55 lines (54 loc) · 1.14 KB
/
mystack.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 "mystack.h"
linkListStack*newLinkListStack()
{
linkListStack*s=(linkListStack*)malloc(sizeof(linkListStack));
s->size=0;
s->top=NULL;
return s;
}
void deLinkListStack(linkListStack *s)
{
linkListStackNode *backup=NULL;
while(s->top)
{
backup=s->top->next;
free(s->top);
s->top=backup;
}
free(s);
}
int sizeofLinkListStack(linkListStack*s)
{
return s->size;
}
STACK_ELEM peekofLinkListStack(linkListStack*s)
{
if(s->size==0)
{
printf("\nget peek failed, because linkListStack is empty!\n");
return -1;
}
return s->top->var;
}
void linkListStackPush(linkListStack *s, STACK_ELEM elem)
{
linkListStackNode *node=(linkListStackNode*)malloc(sizeof(linkListStackNode));
node->next=s->top;
node->var=elem;
s->top=node;
s->size++;
}
STACK_ELEM linkListStackPop(linkListStack *s)
{
if(s->top==NULL)
{
printf("\npop failed, because linkListStack is empty!\n");
return 1;
}
STACK_ELEM elem=s->top->var;
linkListStackNode *backup=s->top;
s->top=s->top->next;
s->size--;
free(backup);
return elem;
}