数据结构栈的操作
来源:互联网 发布:淘宝起名字 编辑:程序博客网 时间:2024/05/27 09:46
C++ Code
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <io.h>
#include <math.h>
#include <time.h>
#define OK 1
#define ERROR 0
#define TURE 1
#define FALSE 0
#define MAXSIZE 20 /* 用于快速排序时判断是否选用插入排序阙值 */
/*********顺序栈结构********/
typedef struct
{
int data[MAXSIZE];
int top;
}SqStack;
int visit(int c)
{
printf("%d", c);
return OK;
}
/*******构造一个空栈****/
int InitStack(SqStack *S)
{
S->top = -1;
return OK;
}
/*****把S设为空栈****/
int ClearStack(SqStack *S)
{
S->top = -1;
return OK;
}
/*****若栈为空栈则返回TRUE,否则返回FALSE***/
int StackEmpty(SqStack S)
{
if (S.top == -1)
return TURE;
else
return FALSE;
}
/*********返回S的长度********/
int StackLength(SqStack S)
{
return S.top + 1;
}
/*******若栈顶不为空,则用e返回S的栈顶元素,并返回OK, 否则返回FALSE****/
int GetTop(SqStack S, int *e)
{
if (S.top == -1)
return FALSE;
*e = S.data[S.top];
return OK;
}
/**********插入栈顶元素e为新的栈顶元素*******/
int Push(SqStack *S, int e)
{
if (S->top == MAXSIZE - 1)
return ERROR;
S->top++;
S->data[S->top] = e;
return OK;
}
/*********若栈不为空,则删除栈顶的元素,用e返回其值,并返回OK, 否则返回FALSE***/
int Pop(SqStack *S, int *e)
{
if (S->top == -1)
return FALSE;
*e = S->data[S->top];
S->top--;
return OK;
}
/********从栈顶依次对栈中的每个元素显示******/
int StackTravse(SqStack S)
{
int i;
for (i = 0; i <= S.top; i++)
{
printf("%d", S.data[i]);
}
printf("\n");
return OK;
}
int main()
{
int j;
int e;
SqStack S;
if (InitStack(&S)==OK)
for (j = 1; j <= 10; j++)
{
Push(&S, j);
}
printf("栈中的元素为\n");
StackTravse(S);
Pop(&S, &e);
printf("弹出来的元素为e=%d\n",e);
printf("栈顶是否为空:%d(1:空,0:非空)\n", StackEmpty(S));
GetTop(S,&e);
printf("栈顶的元素为%d 栈的长度为%d\n", e, StackLength(S));
ClearStack(&S);
printf("清空后栈是否为空:%d(1: 空, 0 : 非空)\n", StackEmpty(S));
}
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <io.h>
#include <math.h>
#include <time.h>
#define OK 1
#define ERROR 0
#define TURE 1
#define FALSE 0
#define MAXSIZE 20 /* 用于快速排序时判断是否选用插入排序阙值 */
/*********顺序栈结构********/
typedef struct
{
int data[MAXSIZE];
int top;
}SqStack;
int visit(int c)
{
printf("%d", c);
return OK;
}
/*******构造一个空栈****/
int InitStack(SqStack *S)
{
S->top = -1;
return OK;
}
/*****把S设为空栈****/
int ClearStack(SqStack *S)
{
S->top = -1;
return OK;
}
/*****若栈为空栈则返回TRUE,否则返回FALSE***/
int StackEmpty(SqStack S)
{
if (S.top == -1)
return TURE;
else
return FALSE;
}
/*********返回S的长度********/
int StackLength(SqStack S)
{
return S.top + 1;
}
/*******若栈顶不为空,则用e返回S的栈顶元素,并返回OK, 否则返回FALSE****/
int GetTop(SqStack S, int *e)
{
if (S.top == -1)
return FALSE;
*e = S.data[S.top];
return OK;
}
/**********插入栈顶元素e为新的栈顶元素*******/
int Push(SqStack *S, int e)
{
if (S->top == MAXSIZE - 1)
return ERROR;
S->top++;
S->data[S->top] = e;
return OK;
}
/*********若栈不为空,则删除栈顶的元素,用e返回其值,并返回OK, 否则返回FALSE***/
int Pop(SqStack *S, int *e)
{
if (S->top == -1)
return FALSE;
*e = S->data[S->top];
S->top--;
return OK;
}
/********从栈顶依次对栈中的每个元素显示******/
int StackTravse(SqStack S)
{
int i;
for (i = 0; i <= S.top; i++)
{
printf("%d", S.data[i]);
}
printf("\n");
return OK;
}
int main()
{
int j;
int e;
SqStack S;
if (InitStack(&S)==OK)
for (j = 1; j <= 10; j++)
{
Push(&S, j);
}
printf("栈中的元素为\n");
StackTravse(S);
Pop(&S, &e);
printf("弹出来的元素为e=%d\n",e);
printf("栈顶是否为空:%d(1:空,0:非空)\n", StackEmpty(S));
GetTop(S,&e);
printf("栈顶的元素为%d 栈的长度为%d\n", e, StackLength(S));
ClearStack(&S);
printf("清空后栈是否为空:%d(1: 空, 0 : 非空)\n", StackEmpty(S));
}
0 0
- 数据结构--栈的操作
- 数据结构 -- 栈的操作
- 数据结构----栈的操作
- 数据结构栈的操作
- 数据结构栈的操作
- 数据结构-栈的操作
- 【数据结构】链式栈的操作
- 【数据结构】 栈的基本操作
- 数据结构栈的基本操作
- 栈的基本操作-数据结构
- 【数据结构】栈的基本操作
- [数据结构]栈的基本操作
- 数据结构--栈的基本操作
- 数据结构--栈的基本操作
- 数据结构:栈的基本操作
- 数据结构-栈的基本操作
- 数据结构栈的一系列操作
- 数据结构--栈的基本操作
- 自增id算法snowflake
- c++实验6-数组合并
- Android开发:ListView、AdapterView、RecyclerView全面解析
- 自定义Listview适配器的优化
- 我没有解决的迷之错误
- 数据结构栈的操作
- 同一个服务器上的不同项目要注意session的死锁问题
- Eclipse安装Swing插件
- 【OpenCV】一种基于阈值的图片中的文字分割
- 动态规划专题总结
- 使用ngrok暴露内网服务到公网
- SIP协议学习
- 未来发展的方向
- onethink后台上传图片,同一张图只能上传一次的问题