数据结构 - C语言版 - 顺序栈 所有基本操作
2013-11-11 17:58
543 查看
C语言 - 顺序栈 所有基本操作
//结构体 定义为 数组 的方式 #include <stdio.h> #include <stdlib.h> #define MAXSIZE 5 typedef struct Stack{ int data[MAXSIZE]; int top; }Stack; Stack *Init_SeqStack() { Stack *s; s=malloc(sizeof(Stack)); s->top = -1; return s; } void Push_Stack(Stack * S) { int i; for(i=0; i<MAXSIZE; i++) { if(S->top== (MAXSIZE-1) ) { printf("栈满,无法继续输入!\n"); break; } S->top++; scanf("%d", &S->data[S->top] ); } } void Pop_Stack(Stack * S) { while(S->top != -1) { printf("%d ",S->data[S->top]); S->top--; } } int main(void) { Stack *S; S = Init_SeqStack(); Push_Stack(S); Pop_Stack(S); printf("\n"); system("pause"); return 0; }
//结构体 定义为 数组基地址 的方式 #include <stdio.h> #define MAXSIZE 100 typedef int SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; }SqStack; SqStack S; int InitStack(SqStack *S) { S->base= malloc(sizeof(SElemType[MAXSIZE])); if(!S->base) return 0; S->top = S->base; S->stacksize=MAXSIZE; return 1; } int Push(SqStack *S,SElemType e) { if((S->top)-(S->base) == S->stacksize) return 0; else { *S->top++=e; return 1; } } int Pop(SqStack *S,SElemType *e) { if(S->top==S->base) return 0; else { *e = *--S->top; return 1; } } main() { int a; InitStack(&S); Push(&S,1); Push(&S,2); Push(&S,3); Push(&S,4); Push(&S,5); Pop(&S,&a); printf("%d\n",a); Pop(&S,&a); printf("%d\n",a); Pop(&S,&a); printf("%d\n",a); Pop(&S,&a); printf("%d\n",a); Pop(&S,&a); printf("%d\n",a); }
相关文章推荐
- C语言版数据结构中顺序表的基本操作定义和初始化
- 数据结构 - C语言版 - 线性表 所有基本操作
- 数据结构 - C语言版 - 单链表 所有基本操作
- 数据结构 - C语言版 - 串 所有基本操作
- 数据结构 - C语言版 - 双向链表 所有基本操作
- 数据结构 - C语言版 - 链栈 所有基本操作
- 数据结构 串(顺序存储)的基本操作
- 数据结构的串的顺序存储的基本操作(学习历程)
- c语言版数据结构(奇迹冬瓜)-串(定长顺序存储各项操作)
- 严蔚敏数据结构单链表的所有基本操作
- 数据结构-顺序串的基本操作
- 数据结构--顺序栈的基本操作
- 严蔚敏数据结构顺序表的所有基本操作
- 数据结构-循环顺序队列的基本操作
- 数据结构 - 顺序栈的基本操作(C语言)
- 数据结构-顺序表的基本操作
- 数据结构系列-线性表的顺序存储及基本操作
- 栈的基本操作(附带一个数据结构、C语言版)
- SqListFunc顺序链表的12个基本操作的C语言代码(数据结构)
- 数据结构 - 顺序表的基本操作(C语言)