数据结构-----栈数组实现
来源:互联网 发布:整理通讯录的软件 编辑:程序博客网 时间:2024/05/18 01:32
//测试main.cpp
#include <stdio.h>#include <stdlib.h>#include "StackArray.h"int main(){ Stack S; initStack(S); Push(2,S); printf("%d ",Top(S)); Push(3,S); printf("%d ",Top(S)); Pop(S); printf("%d",Top(S)); return 0;}
//具体实现代码StackArray.h
#ifndef STACKARRAY_H_INCLUDED#define STACKARRAY_H_INCLUDED#define LIST_INIT_SIZE 50 // 线性表存储空间的初始分配量#define LISTINCREMENT 16 // 线性表存储空间的分配增量#define EmptyTOS -1typedef int ElemType;struct StackArray{ ElemType *elem; // 存储空间基址 int Capacity; // 当前长度 int TopOfStack; // 当前分配的存储容量(以sizeof(ElemType)为单位)};typedef StackArray *Stack;int initStack(Stack &S){ S = (Stack)malloc(sizeof(struct StackArray)); S->elem = (ElemType*)malloc(sizeof(ElemType)*LIST_INIT_SIZE); S->Capacity = LIST_INIT_SIZE; S->TopOfStack = EmptyTOS; return 1;}void DestoryStack(Stack S){ if(S != NULL) { free(S->elem); free(S); }}int IsEmpty(Stack S){ return S->TopOfStack == EmptyTOS;}int IsFull(Stack S){ return (S->TopOfStack > S->Capacity-1)?1:0;}void MakeEmpty(Stack S){ S->TopOfStack = EmptyTOS;}void Push(ElemType x,Stack S){ if(IsFull(S)) { ElemType *et=(ElemType*)realloc(S->elem,sizeof(ElemType)*LISTINCREMENT); S->elem = et; S->Capacity += LISTINCREMENT; } S->elem[++S->TopOfStack] = x;}ElemType Top(Stack S){ if(!IsEmpty(S)) { return S->elem[S->TopOfStack]; } return -1;}void Pop(Stack S){ if(!IsEmpty(S)) { S->TopOfStack--; }}#endif // STACKARRAY_H_INCLUDED
0 0
- 数据结构-----栈数组实现
- 数据结构---栈(数组实现)
- 数据结构-用数组实现栈
- 【数据结构】栈-数组的实现
- [数据结构] 栈的数组实现
- 数组实现栈的数据结构
- 【数据结构】栈的数组实现
- 简单数据结构---数组实现栈
- 【JAVA数据结构】栈(数组实现)
- 【数据结构】栈的数组实现
- 用静态数组实现栈数据结构
- 数据结构之数组实现栈结构
- 数据结构栈stack的动态数组实现
- 数据结构学习之数组栈实现
- 经典数据结构--数组实现的栈
- 利用数组实现数据结构——栈
- 简单数据结构之数组栈(C++实现)
- 【数据结构】一个数组实现两个栈
- 最小生成树变形题
- 如何用PL/SQL备份oracle数据库
- VS2008运行MFC程序,提示microsoft incremental linker已停止工作解决方法
- Android—9Path图片的使用
- 游起来吧!超妹!
- 数据结构-----栈数组实现
- wordpress构建电子商务系统之WooCommerce 中文链接优化
- 从QuickTime到Beats:回顾苹果历史上的音乐传奇
- (20)JS实现简单的图片向左无缝滚动
- 关于c语言组合数精确度问题
- MP4解析,包含moof
- 总结:数字封装与数组,字典的写法和语法糖
- HDU2578_Dating with girls(1)(二分)
- 实现删除之前弹出确认对话框