数据结构示例之使用数组实现栈
2016-10-28 21:57
344 查看
以下是“使用数组实现栈”的简单示例:
1. 用c语言实现的版本
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
int stack[MaxSize];
int top = -1;
/* 入栈 */
void push(int value)
{
int i;
if (top >= MaxSize-1)
{
printf("\nThe stack is full!\n");
}
else
{
printf("Before push,the stack content is (top->bottom): \n");
if (top < 0) {
printf("It is empty.");
}
else {
for (i = top; i >= 0; --i)
{
printf("%d ", stack[i]);
}
}
++top;
stack[top] = value;
printf("\nAfter push,the stack content is (top->bottom): \n");
for (i = top; i >= 0; --i)
{
printf("%d ", stack[i]);
}
printf("\n");
}
}
/* 出栈 */
int pop()
{
int temp;
int i;
if (top < 0)
{
printf("\nThe stack is empty!\n");
return -1;
}
printf("\nBefore pop, the stack content is (top->bottom): \n");
for (i = top; i >= 0; --i)
{
printf("%d ", stack[i]);
}
temp = stack[top];
--top;
printf("\nThe pop value is [ %d ].\n", temp);
printf("After pop, the stack content is (top->bottom):\n");
if (top <0)
{
printf("It is empty.\n");
}
else {
for (i = top; i >= 0; --i)
{
printf("%d ", stack[i]);
}
}
printf("\n");
return temp;
}
int main()
{
int select;
int stack[5];
int value;
printf("(1)Input a stack data\n");
printf("(2)Output a stack data\n");
printf("(3)Exit\n");
printf("Please select your choice: ");
scanf("%d", &select);
do
{
switch (select)
{
case 1:
printf("Please input the digit: ");
scanf("%d", &value);
push(value); /* 入栈 */
break;
case 2:
value = pop(); /* 出栈 */
break;
default:
printf("Please input the right choice !\n");
break;
}
printf("\n(1)Input a stack data\n");
printf("(2)Output a stack data\n");
printf("(3)Exit\n");
printf("Please select your choice: ");
scanf("%d", &select);
} while (select != 3);
return 0;
}
运行结果如下图所示:
1. 用c语言实现的版本
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
int stack[MaxSize];
int top = -1;
/* 入栈 */
void push(int value)
{
int i;
if (top >= MaxSize-1)
{
printf("\nThe stack is full!\n");
}
else
{
printf("Before push,the stack content is (top->bottom): \n");
if (top < 0) {
printf("It is empty.");
}
else {
for (i = top; i >= 0; --i)
{
printf("%d ", stack[i]);
}
}
++top;
stack[top] = value;
printf("\nAfter push,the stack content is (top->bottom): \n");
for (i = top; i >= 0; --i)
{
printf("%d ", stack[i]);
}
printf("\n");
}
}
/* 出栈 */
int pop()
{
int temp;
int i;
if (top < 0)
{
printf("\nThe stack is empty!\n");
return -1;
}
printf("\nBefore pop, the stack content is (top->bottom): \n");
for (i = top; i >= 0; --i)
{
printf("%d ", stack[i]);
}
temp = stack[top];
--top;
printf("\nThe pop value is [ %d ].\n", temp);
printf("After pop, the stack content is (top->bottom):\n");
if (top <0)
{
printf("It is empty.\n");
}
else {
for (i = top; i >= 0; --i)
{
printf("%d ", stack[i]);
}
}
printf("\n");
return temp;
}
int main()
{
int select;
int stack[5];
int value;
printf("(1)Input a stack data\n");
printf("(2)Output a stack data\n");
printf("(3)Exit\n");
printf("Please select your choice: ");
scanf("%d", &select);
do
{
switch (select)
{
case 1:
printf("Please input the digit: ");
scanf("%d", &value);
push(value); /* 入栈 */
break;
case 2:
value = pop(); /* 出栈 */
break;
default:
printf("Please input the right choice !\n");
break;
}
printf("\n(1)Input a stack data\n");
printf("(2)Output a stack data\n");
printf("(3)Exit\n");
printf("Please select your choice: ");
scanf("%d", &select);
} while (select != 3);
return 0;
}
运行结果如下图所示:
相关文章推荐
- 不使用php api函数实现数组的交换排序示例
- [示例]NSEnumerator-使用枚举类型实现数组的逆序输出
- PHP使用数组实现矩阵数学运算的方法示例
- ES6使用Set数据结构实现数组的交集、并集、差集功能示例
- 使用JavaScript的数组实现数据结构中的队列与堆栈
- 每日一省之—使用线性探测法(仅利用数组作为底层数据结构)实现HashMap
- JavaScript学习总结(二十一)——使用JavaScript的数组实现数据结构中的队列与堆栈
- PHP中使用数组实现堆栈数据结构的代码
- 使用JavaScript的数组实现数据结构中的队列与堆栈
- PHP使用自定义方法实现数组合并示例
- 使用JavaScript的数组实现数据结构中的队列与堆栈
- 使用JavaScript的数组实现数据结构中的队列与堆栈
- java使用数组和链表实现队列示例
- Lua中使用table实现的其它5种数据结构(数组、链表、队列、集合和包、StringBuild)
- 《数据结构》使用数组实现数制的转换
- 使用JavaScript的数组实现数据结构中的队列与堆栈
- PHP中使用数组实现堆栈数据结构的代码
- PHP实现使用DOM将XML数据存入数组的方法示例
- 数据结构示例之用数组实现圆形队列
- 数据结构示例之数组模拟多栈实现