栈:
栈是一个特殊的线性表,只能在一端操作。
栈的操作遵循“先进后出(后进先出)”原则!
空栈:不含任何数据元素的栈,top=-1;栈中有一个元素时,top=0;
栈的抽象数据类型:
插入和删除:push,pop 同线性表。
元素具有相同的类型,相邻元素具有前驱和后继的的关系。
操作:
- 初始化操作,建立空栈S;
- 若栈存在,就销毁它;
- 将栈清空;
- 判断栈是否空:空返回true,否则返回false;
- 若栈存在非空,用e返回s的栈顶元素;
- 若栈S存在,插入新元素e到栈s成为栈顶元素;
- 删除栈s栈顶元素,并用e返回其值;
- 返回栈s的元素个数
进栈操作
int Push(STACK *S, int e) { if(S->top == MAXSIZE -1) return error; S->top++; S->data[S->top]=e; return OK; }
出栈操作
int Pop(STACK *S, int *e) { if(S->top == -1) return ERROR; *e=S->data[S->top]; S->top--; return OK; }
附对于栈的基本操作的代码:
头文件: #ifndef _SEQUENCESTACK_H #define _SEQUENCESTACK_H #define FAILURE 100001 #define SUCCESS 100002 #define TRUE 100003 #define FALSE 100004 #define SIZE 10 typedef int Elemtype; struct stack { int top; Elemtype *data; }; typedef struct stack Stack; int StackInit(Stack **s);//初始化栈 int StackEmpty(Stack *s);//判断栈是否为空 int Stackpush(Stack *s,Elemtype e);//入栈操作 int Stackgettop(Stack *s);//获取栈的栈顶元素 int Stackpop(Stack *s);//出栈操作 int Stackclear(Stack *s);//清空栈 int Stackdestory(Stack **s);//销毁栈 #endif
自定义函数: #include "Sequencestack.h" #include <stdio.h> #include <stdlib.h> //初始化栈 int StackInit(Stack **s) { (*s) = (Stack *) malloc (sizeof(Stack)); //分配一个结构体,用于保存栈的信息 if(NULL == (*s)) { return FAILURE; } (*s)->top = -1; //空栈,栈顶为-1 (*s)->data = (Elemtype *) malloc (sizeof(Elemtype) * SIZE);//为栈分配空间 if(NULL == (*s)->data) { return FAILURE; } return SUCCESS; } //判断栈是否为空 int StackEmpty(Stack *s) { return (s->top == -1) ? TRUE : FALSE; } //入栈操作 int Stackpush(Stack *s, Elemtype e) { if(NULL == s || s->top == 10) { return FAILURE; } s->data[s->top + 1] = e; s->top++; return SUCCESS; } //获取栈的栈顶元素 int Stackgettop(Stack *s) { if(s->top == -1) { return FAILURE; } return s->data[s->top]; } //出栈操作 int Stackpop(Stack *s) { if(s->top == -1) { return FAILURE; } s->top --; return SUCCESS; } //清空栈 int Stackclear(Stack *s) { if(NULL == s) { return FAILURE; } s->top = -1; return SUCCESS; } //销毁栈 int Stackdestory(Stack **s) { if(NULL == s) { return FAILURE; } free((*s)->data); free(*s); *s = NULL; return SUCCESS; }
main主函数: #include "Sequencestack.h" #include <stdio.h> int main() { Stack *stack; int ret; srand(time(NULL)); ret = StackInit(&stack); //stack是野指针,要修改stack的值(地址) if(SUCCESS == ret) { printf("Init Sequencestack Stack Success!\n"); } else { printf("Init Failure!\n"); } ret = StackEmpty(stack); if(TRUE == ret) { printf("The Stack is empty!\n"); } else { printf("The Stack is not empty!\n"); } int i; for(i = 1; i < 10; i++) { ret = Stackpush(stack,rand(time(NULL)) % 10); if(FAILURE == ret) { printf("Push Failure!\n"); } else { printf("Push Success!\n"); } } ret = Stackgettop(stack); printf("Get top element %d Success!\n", ret); ret = Stackpop(stack); if(FAILURE == ret) { printf("Pop FAILURE!\n"); } else { printf("Pop Success!\n"); } ret = Stackclear(stack); if(FAILURE == ret) { printf("Clear Failure!\n"); } else { printf("Clear Success!\n"); } ret = Stackpop(stack); if(FAILURE == ret) { printf("Pop FAILURE!\n"); } else { printf("Pop Success!\n"); } ret = Stackdestory(&stack); if(FAILURE == ret) { printf("Destory Failure!\n"); } else { printf("Destory Success!\n"); } for(i = 1; i < 10; i++) { ret = Stackpush(stack,i); if(FAILURE == ret) { printf("Push %d Failure!\n", i); } else { printf("Push %d Success!\n",i); } } return 0; }