forked from AuthorityWu/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
87 lines (80 loc) · 1.09 KB
/
stack.h
File metadata and controls
87 lines (80 loc) · 1.09 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
#ifndef STACK_H
#define STACK_H
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct Stack{
DataType data;
struct Stack *next;
}Stack;
//初始化栈
void StackInitial(Stack **s)
{
(*s)=(Stack *)malloc(sizeof(Stack));
(*s)->next=NULL;
if(!(*s)){
printf("申请内存空间失败!\n");
exit(0);
}
}
//判断栈是否为空
int StackNotEmpty(Stack *s)
{
if(s->next==NULL)
return 0;
else
return 1;
}
//入栈
int StackPush(Stack *s,DataType x)
{
Stack *p;
p=(Stack *)malloc(sizeof(Stack));
if(!p){
printf("申请内存空间失败!\n");
exit(0);
}
p->data=x;
p->next=s->next;
s->next=p;
return 1;
}
//出栈
int StackPop(Stack *s,DataType *d)
{
Stack *p=s->next;
if(s->next==NULL){
printf("堆栈已空,无数据元素出栈!\n");
return 0;
}
else{
s->next=p->next;
*d=p->data;
free(p);
return 1;
}
}
//取栈顶元素
int StackTop(Stack *s,DataType *d)
{
if(s->next==NULL){
printf("堆栈已空!\n");
return 0;
}
else{
*d=s->next->data;
return 1;
}
}
//释放申请的内存空间
void Destroy(Stack *s)
{
Stack *p,*p1;
p=s;
while(p!=NULL){
p1=p;
p=p->next;
free(p1);
}
}
#endif