顺序栈, 首先是顺序存储,连续的存储空间, 那就想到了使用数组实现顺序栈的功能
栈:根据其先进后出的特性,操作数组数据,只能先操作栈顶的元素
定义一个可以指向栈的结构:
typedef struct seqstack{
int *dat; //定义指向数据的指针
int top; //指向栈顶数据,是数组下标
int len; //栈空间的确切大小,就是数组的元素个数,通过malloc动态分配空间
};
seqstack.c文件实现顺序栈的操作
#include "seqstack.h" //创建一个空栈,参数,确定顺序栈的大小 Seqstack *create_seqstack(int len) { if(len <= 0) { printf("create_seqstack len is "); return NULL; } Seqstack *stack = NULL; stack = (Seqstack*)malloc(sizeof(Seqstack));//申请存放栈结构的空间 if(stack == NULL) { printf("stack malloc is null "); return NULL; } stack->data = (u16*)malloc(sizeof(u16)*len);//申请数组空间,栈放数据的 if(stack->data == NULL) //放数据的栈申请空间没成功,要把前面申请的结构空间释放 { printf("stack->data malloc is null "); free(stack); stack = NULL; return NULL; } stack->top = -1; //栈顶 stack->length = len;//栈长度 return stack; } //判栈满 int is_full_seqstack(Seqstack *stack) { if(stack == NULL) { printf("stack full null "); return -1; } #if 0 if(stack->top == stack->length-1) { printf("stack is full "); return 1; } return 0; #endif return (stack->top==(stack->length-1)?1:0); } //判栈空 int is_empty_seqstack(Seqstack *stack) { if(stack == NULL) { printf("stack full null "); return -1; } return (stack->top == -1 ? 1 : 0 ); } //压栈,每次放在栈顶 int push_seqstack(Seqstack *stack, u16 value) { if(stack == NULL) { printf("stack full null "); return -1; } if(is_full_seqstack(stack)==1) { printf("pust stack is full ");return -1; } //不同的操作方法而已,也可以使用 stack->data[++stack->top] = value; // stack->top++; // stack->data[stack->top] = value; stack->data[stack->top+1] = value; stack->top++; return 1; } //出栈,每次栈顶先出 int pop_seqstack(Seqstack *stack)//出栈 { if(stack == NULL) { printf("stack full null "); return -1; } if(is_empty_seqstack(stack)==1) { printf("pust stack is empty ");return -1; } printf("pop data is %d ",stack->data[stack->top]); stack->top--; return 1; } //显示整个栈的内容 void show_seqstack(Seqstack *stack)//显示栈内容 { if(stack == NULL) { printf("show stack null "); return; } if(is_empty_seqstack(stack)==1) { printf("show stack is empty ");return; } int i; for(i=0;i<=stack->top;i++) { printf("%d ",stack->data[i]); } }
seqstack.h文件:
#ifndef __SEQSTACK_H #define __SEQSTACK_H #include <stdio.h> #include <stdlib.h> typedef int u16; typedef struct stack{ u16 *data; //通过地址访问 栈数据 u16 top; //栈顶 u16 length;//栈的最大空间 }Seqstack; Seqstack *create_seqstack(int len);//创建一个空的顺序栈 int is_full_seqstack(Seqstack *stack);//判满 int is_empty_seqstack(Seqstack *stack);//判空 int push_seqstack(Seqstack *stack, u16 value);//压栈 int pop_seqstack(Seqstack *stack);//出栈 void show_seqstack(Seqstack *stack);//显示栈内容 #endif
main.c测试文件:
#include "seqstack.h" int main(int argc, const char *argv[]) { Seqstack *s=NULL; s=create_seqstack(5); push_seqstack(s,1); show_seqstack(s); printf(" "); push_seqstack(s,2); show_seqstack(s); printf(" "); push_seqstack(s,3); show_seqstack(s); printf(" "); push_seqstack(s,-4); show_seqstack(s); printf(" "); push_seqstack(s,5); show_seqstack(s); printf(" "); push_seqstack(s,6); show_seqstack(s); putchar(10); printf("pop stack "); pop_seqstack(s); push_seqstack(s,10); pop_seqstack(s); pop_seqstack(s); show_seqstack(s); return 0; }
输出结果:
栈的相关使用
编号为123456789的火车经过如下轨道从左边入口处移到右边出口处(每车都必须只能进临时轨道M一次,且不能再回到左边入口处)按照从左向右的顺序,下面的结果不可能的是哪项?(演示见图2)
A. 123876549 OK
B. 321987654 OK
C. 321456798 OK
D. 987651234 No