顺序栈的一般操作
2014-03-11 22:28
239 查看
#include <stdio.h> //define the size of array #define MAXSIZE 100 //define new data type: stack struct stack{ int data[MAXSIZE]; int top; }; //defint var seqStack typedef struct stack seqStack; //create a stack int create_stack(seqStack* S){ //initial the top to 0 S->top = 0; printf("create stack comlete!\n"); return 0; } int push_stack(seqStack* S, int element){ //is overflow? if(S->top > 100) return -1; //data[top] = element, at the beginning data[0] = element S->data[S->top] = element; //top + 1, point to a new empty position S->top++; printf("push %d into stack comlete!\n", element); return 0; } //print every element of the stack int show_stack(seqStack S){ int i; //top point to the new empty position, so use top-1 for(i=(S.top-1); i>=0; i--){ printf("%d\n",S.data[i]); } printf("show stack from top to bottom comlete!\n"); return 0; } //print the top element and top-1 int pop_stack(seqStack* S){ //is stack empty if (S->top == 0) { printf("the stack is empty!\n"); return 0; } //good: --(S->top) will modify the value of top directly printf("%d\n", S->data[--(S->top)]); printf("pop stack from top complete!\n"); return 0; } int main(){ seqStack S; //create stack by seqstack address create_stack(&S); //push 15 into stack push_stack(&S, 15); //push 27 into stack push_stack(&S, 27); //push 89 into stack push_stack(&S, 89); //print stack from top to bottom show_stack(S); //return 89 pop_stack(&S); //return 27 pop_stack(&S); //return 15 pop_stack(&S); //stack already empty, so return pop_stack(&S); return 0; }
相关文章推荐
- 顺序队列的操作
- 线性表的顺序存储格式基本操作:初始化、插入、删除、查找、打印
- 一般的数据库SQLite操作
- 基于静态数组的顺序表的一些基本操作(2)
- 队列之顺序队基本操作
- 顺序栈、链栈基本操作
- 数据结构(3)-线性表顺序结构的合并操作
- 栈的顺序存储的初始化、判断栈空、入栈、出栈、读取栈顶元素、栈置空、求栈长操作
- C++顺序栈基本操作
- 顺序循环队列的基本操作
- c语言学习之顺序表操作
- 算法4-9:BST顺序操作
- 数据结构-线性表的顺序存储实现及各种操作代码
- 区别顺序表的清空和销毁两个操作
- 顺序表的基本操作
- 顺序表的基本操作
- UI方法调用顺序和UI常用操作
- UML和模式应用学习笔记(6)——系统顺序图、系统操作和层
- MyBatis执行一次数据库操作的原理以及顺序
- STL 容器和迭代器连载4_顺序容器的操作1