栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 实现栈的基本数据结构和操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 实现栈的基本数据结构和操作

C++ 实现栈的基本数据结构和操作
#include 
using namespace std;
#define MaxSize 50//定义最大常数值

typedef  int Elemtype;


typedef struct
{
	Elemtype data[MaxSize];
	int top;//栈顶元素,即存放栈顶元素在data数组中的下标
} SqStack;//顺序栈类型

void InitStack(SqStack*& s)
{
	s = new SqStack;//分配一个顺序栈空间
	s->top = -1;//栈顶指针置为-1
}

void DestroyStack(SqStack *&s)
{
	delete s;
}

bool stackempty(SqStack* s)
{
	return s->top == -1;
}

bool push(SqStack*& s, Elemtype e)
{
	if (s->top == MaxSize - 1)//当栈满的时候溢出
		return false;
	s->top++;//栈顶指针加一
	s->data[s->top] = e;//元素e放置于栈顶指针处
	return true;
}

bool pop(SqStack*& s, Elemtype& e)
{
	if (s->top == -1)//当栈为空返回假
		return false;
	e = s->data[s->top];
	s->top--;
	return true;
}

bool GetTop(SqStack* s, Elemtype& e)
{
	if (s->top == -1)
		return false;
	e = s->data[s->top];
	return true;
}


typedef struct
{
	Elemtype data[MaxSize];
	int front, rear;//队头和队尾指针
} SqQueue;//顺序队类型

void InitQueue(SqQueue*& q)
{
	q = new SqQueue;
	q->front = q->rear = -1;//队空的条件 q->front == q->rear,这里置初始值
}

void destroyqueue(SqQueue*& q)
{
	delete q;
}

bool QueueEmpty(SqQueue* q)
{
	return q->front == q->rear;
}

bool enQueue(SqQueue*& q, Elemtype e)
{
	if (q->rear == MaxSize - 1)//队满溢出
		return false;
	q->rear++;
	q->data[q->rear] = e;
	return true;
}

bool deQueue(SqQueue*& q, Elemtype& e)
{
	if (q->front == q->rear)
		return false;
	q->front++;
	e = q->data[q->front];//注意front指向的第一个元素的前面一个元素
	return true;
}

int main()
{
   
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/875313.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号