当前位置: 首页 > news >正文

数据结构题型9-顺序栈

#include <iostream>  //引入头文件
using namespace std;typedef int Elemtype;#define Maxsize 10
#define ERROR 0
#define OK    1typedef struct
{Elemtype data[Maxsize];int top;
}SqStack;void InitStack(SqStack& S)
{S.top = -1;
}
bool StackEmpty(SqStack S)
{if (S.top == -1)   //堆空return OK;else              //不空return ERROR;
}bool Push(SqStack& S, Elemtype x)
{if (S.top == Maxsize - 1)return ERROR;S.data[++S.top] = x;return OK;
}bool Pop(SqStack& S, Elemtype& x)
{if (S.top == -1)return ERROR;x = S.data[S.top--];return OK;
}bool GetTop(SqStack& S, Elemtype& x)
{if (S.top == -1)return ERROR;x = S.data[S.top];return OK;
}int main(void)
{SqStack S;InitStack(S);Push(S, 1);Push(S, 2);Push(S, 3);Push(S, 4);Push(S, 5);int top = 0;GetTop(S, top);cout << "stack top: " << top << endl;if (StackEmpty(S) == 1)cout << "Stack is empty"<< endl;elsecout << "Stack is not empty"<< endl;int x = 0;Pop(S, x);cout << "pop1: " << x << endl;Pop(S, x);cout << "pop2: " << x << endl;Pop(S, x);cout << "pop3: " << x << endl;Pop(S, x);cout << "pop4: " << x << endl;Pop(S, x);cout << "pop5: " << x << endl;if (StackEmpty(S) == 1)cout << "Stack is empty" << endl;elsecout << "Stack is not empty" << endl;return 0;
}//#include <iostream>  //引入头文件
//using namespace std;
//
//typedef int Elemtype;
//
//#define Maxsize 10
//#define ERROR 0
//#define OK    1
//
//typedef struct
//{
//	Elemtype data[Maxsize];
//	int top;
//}SqStack;
//
//
//void InitStack(SqStack& S)
//{
//	S.top = 0;
//}
//bool StackEmpty(SqStack S)
//{
//	if (S.top == 0)   //堆空
//		return OK;
//	else              //不空
//		return ERROR;
//}
//
//bool Push(SqStack& S, Elemtype x)
//{
//	if (S.top == Maxsize)
//		return ERROR;
//	S.data[S.top++] = x;
//	return OK;
//}
//
//bool Pop(SqStack& S, Elemtype& x)
//{
//	if (S.top == 0)
//		return ERROR;
//	x = S.data[--S.top];
//	return OK;
//}
//
//bool GetTop(SqStack& S, Elemtype& x)
//{
//	if (S.top == 0)
//		return ERROR;
//	x = S.data[S.top-1];
//	return OK;
//}
//
//int main(void)
//{
//	SqStack S;
//	InitStack(S);
//	Push(S, 1);
//	Push(S, 2);
//	Push(S, 3);
//	Push(S, 4);
//	Push(S, 5);
//	int top = 0;
//	GetTop(S, top);
//	cout << "stack top: " << top << endl;
//	if (StackEmpty(S) == 1)
//		cout << "Stack is empty" << endl;
//	else
//		cout << "Stack is not empty" << endl;
//	int x = 0;
//	Pop(S, x);
//	cout << "pop1: " << x << endl;
//	Pop(S, x);
//	cout << "pop2: " << x << endl;
//	Pop(S, x);
//	cout << "pop3: " << x << endl;
//	Pop(S, x);
//	cout << "pop4: " << x << endl;
//	Pop(S, x);
//	cout << "pop5: " << x << endl;
//	if (StackEmpty(S) == 1)
//		cout << "Stack is empty" << endl;
//	else
//		cout << "Stack is not empty" << endl;
//	return 0;
//}
http://www.lryc.cn/news/174864.html

相关文章:

  • 时间复杂度、空间复杂度
  • C++---多态
  • Android 滑动事件消费监控,Debug 环境下通用思路
  • Unity中Shader用到的向量的乘积
  • 帆软FineReport决策报表之页面布局
  • [Linux入门]---进程的概念
  • Leetcode—— 20.有效的括号
  • 视频播放器的技术组成
  • Stable Diffusion 系统教程 | 强大的ControlNet 控制网
  • Hadoop-sqoop
  • [论文阅读]YOLOV1:You Only Look Once:Unified, Real-Time Object Detection
  • Ubuntu 20.04 安装MySQL 8.0.34
  • MySQL 高级语句 Part1(进阶查询语句+MySQL数据库函数+连接查询)
  • Rust免杀 Shellcode加载与混淆2
  • 牛客java训练题 day1
  • 接口测试练习步骤
  • Qt/C++音视频开发56-udp推流和拉流/组播和单播推流
  • 人工智能轨道交通行业周刊-第61期(2023.9.18-9.24)
  • for...in 和 for...of 的区别
  • 高并发系统 - 接口幂等技术方案,高可用系统架构与技术选型
  • 简单的手机电脑无线传输方案@固定android生成ftp的IP地址(android@windows)
  • Unity3D 检测鼠标位置的Sprite像素颜色
  • layui input 监听事件
  • 一致性思维链(SELF-CONSISTENCY IMPROVES CHAIN OF THOUGHT REASONING IN LANGUAGE MODELS)
  • 腾讯云16核服务器配置大全_16核CPU型号性能测评
  • HTML中Input elements should have autocomplete attributes的解决方案
  • 2808. 使循环数组所有元素相等的最少秒数;1015. 可被 K 整除的最小整数;1001. 网格照明
  • Python爬虫在Web应用自动化测试中的应用
  • 苹果手机短信删除了怎么恢复?3种有效方法介绍
  • 前端JavaScript中的 == 和 ===区别,以及他们的应用场景,快来看看吧,积累一点知识。