【数据结构】顺序结构实现栈
来源:互联网 发布:东方财富软件使用方法 编辑:程序博客网 时间:2024/05/16 08:16
1.相関概念
栈:一种特殊的线性表,其只允许在在固定的一端进行插入和删除元素操作。
在对数据进行插入和删除操作的一端称为栈顶,另一端则称为栈底。不含任何元素的栈称为空栈,栈又称为后进先出的线性表。
栈的特点:后进先出(LIFO)
2.顺序结构实现栈
代码如下:
- stack.h
//顺序结构实现栈#ifndef __STACK_H__#define __STACK_H__#include <iostream>#include <assert.h>#include <stdlib.h>using namespace std;#define STACK_DEFAULT_SIZE 10 //栈默认大小typedef int ElemType;typedef struct Stack{ ElemType* base; int top; int capacity;}Stack;void menu(); //菜单void InitStack(Stack* s); //初始化栈bool IsFull(Stack* s); //判断栈是否已满bool IsEmpty(Stack* s); //判断栈是否为空bool Push(Stack* s, ElemType x); //入栈bool Pop(Stack* s, ElemType* x); //出栈int Lenth(Stack* s); //求栈长bool GetTop(Stack* s, ElemType *x); //获取栈顶元素void ShowStack(Stack* s); //打印链表内容void Clear(Stack* s); //清空栈void Destroy(Stack* s); //销毁栈#endif //__STACK_H__
- stack.cpp
//顺序结构实现栈#include "stack.h"void menu() //菜单{ cout << "*******************************************" << endl; cout << "************* 【SeqStack】 **************" << endl; cout << "*** [1]Push [2]Pop [3]lenth ***" << endl; cout << "*** [4]GetTop [5]ShowStack [6]Clear ***" << endl; cout << "************* 【HaHaHa】 **************" << endl; cout << "*******************************************" << endl;}void InitStack(Stack* s) //初始化栈{ ElemType* p = (ElemType*)malloc(sizeof(ElemType)* STACK_DEFAULT_SIZE); assert(p != NULL); s->base = p; s->capacity = STACK_DEFAULT_SIZE; s->top = 0;}bool IsFull(Stack* s) //判断栈是否已满{ return s->top == s->capacity;}bool IsEmpty(Stack* s) //判断栈是否为空{ return s->top == 0;}bool Push(Stack* s, ElemType x) //入栈{ if (IsFull(s)) { cout << "栈已满!" << endl; return false; } s->base[s->top++] = x; return true;}bool Pop(Stack* s, ElemType* x) //出栈{ if (IsEmpty(s)) { cout << "栈为空!" << endl; return false; } *x = s->base[--s->top]; return true;}int Lenth(Stack* s) //求栈长{ return s->top;}bool GetTop(Stack* s, ElemType *x) //获取栈顶元素{ if (IsEmpty(s)) { cout << "栈为空!" << endl; return false; } *x = s->base[--s->top]; return true;}void ShowStack(Stack* s) //打印链表内容{ int i = 0; for (i = s->top - 1; i >= 0; --i) { cout << s->base[i] << "—>"; } cout << "NULL" << endl;}void Clear(Stack* s) //清空栈{ s->top = 0; s->capacity = 0;}void Destroy(Stack* s) //销毁栈{ Clear(s); free(s->base); s->base = NULL;}
- main.cpp
//顺序结构实现栈#include "stack.h"int main(){ Stack s; ElemType item; int select = 1; InitStack(&s); while (select) { menu(); cout << "请选择: " << endl; cin >> select; switch (select) { case 1: cout << "请输入: "; while (cin >> item, item != -1) { Push(&s, item); } break; case 2: Pop(&s, &item); cout << item << endl; break; case 3: cout << "栈长为: " << Lenth(&s) << endl; break; case 4: GetTop(&s, &item); cout << "栈顶元素为: " << item << endl; break; case 5: ShowStack(&s); break; case 6: Clear(&s); break; default: break; } } Destroy(&s); return 0;}
这样,顺序结构实现栈就ok了。
宝宝们,可以参考一下,还请指点。
阅读全文
1 0
- 【数据结构】顺序结构实现栈
- 数据结构:栈的顺序结构及实现(C++)
- 数据结构--C语言实现栈的顺序存储结构
- 数据结构——栈的顺序结构以及实现
- 常用数据结构之顺序结构List实现
- 【数据结构】二叉树顺序结构实现_BiTreeArray
- 数据结构线性表顺序结构的实现
- 数据结构--顺序存储结构及抽象实现
- 数据结构 栈的顺序存储结构
- 数据结构三栈的顺序存储结构
- 数据结构:栈的顺序存储结构
- 【数据结构基础】栈--顺序存储结构
- java 数据结构之 顺序存储结构 栈
- 数据结构之线性结构---栈 顺序储存
- 数据结构:栈的顺序存储结构
- 数据结构之顺序栈实现
- 企业级数据结构-顺序栈实现
- 数据结构---栈的顺序实现
- 软件开发人员的简历项目经验怎么写?
- iOS 如何优化项目
- 用docker搭建全栈式应用(三)管理篇
- Find Bottom Left Tree Value问题及解法
- 如何快速搭建自己的github.io博客
- 【数据结构】顺序结构实现栈
- 如何给自己的博客网站加入评论系统
- <Android 基础(四十)> FlexboxLayout
- 数论 高斯消元模板
- 如何使用特定的SSH Key提交GIT
- 欢迎使用CSDN-markdown编辑器
- 企业级后台接口开发。环境基于spring+springboot+springmvc+mybatis。测试 junit4
- 上传图片的格式验证
- 19. Remove Nth Node From End of List