队列和栈(Queue & stack)

 

栈(Stack)和队列(Queue)是两种操作受限的线性表。(两种我愿称作特殊的单链表)

栈与队列的相同点:

1.都是线性结构。

2.插入操作都是限定在表尾进行。

3.都可以通过顺序结构和链式结构实现。、

4.插入与删除的时间复杂度都是O(1),在空间复杂度上两者也一样。

5.多链栈和多链队列的管理模式可以相同。

栈与队列的不同点:

1.删除数据元素的位置不同,栈的删除操作在表尾进行,队列的删除操作在表头进行。

2.应用场景不同;常见栈的应用场景包括括号问题的求解,表达式的转换和求值,函数调用和递归实现,深度优先搜索遍历等;常见的队列的应用场景包括计算机系统中各种资源的管理,消息缓冲器的管理和广度优先搜索遍历等。

3.顺序栈能够实现多栈空间共享,而顺序队列不能。

下面是顺序栈的代码实现:

#include<iostream>
using namespace std;
#define OK 1
#define ERROR -1
#define MAXSIZE 1000
typedef int Etype;
typedef int Status;
typedef struct stack {
	Etype data[MAXSIZE];
	int top;

}Qstack;
void InitStack(Qstack* S) {
	S->top = -1;//初始化栈顶指针

}
bool isEmpty(Qstack S)
{
	if (S.top == -1) {
		cout << "true!";
		return true;
	}
	else {
		cout << "false!";
		return false;
	}
}
Status Push(Qstack* S, Etype e) {
	if (S->top== MAXSIZE - 1) {
		return ERROR;
	}
	S->top++; //栈顶指针加一
    S->data[S->top]= e;//插入值
	return OK;
}
Status Pop(Qstack* S, Etype* e) {
	if (S->top == -1) {
		return ERROR;	
	}
	*e = S->data[S->top];//用e保存删除的指针
	S->top--;//栈顶指针减1
	return OK;
}
Status Pop(Qstack S) {
	if (S.top == -1) {
		return ERROR;

	}
	return S.top;
}
void DestroyStack(Qstack* S) {
	if (S->top) {
		S->top = NULL;
		free(S);
	};

}
void Traversal(Qstack S) {
	for (int i = S.top; i>-1; --i)
	{
		cout << S.data[i];
	}
}
int main() {
	int a;
	cout << "This is your stack";
	Qstack* LQStack = new Qstack;
	InitStack(LQStack);
	cout << "Which elem you want to push in the stack?"<<endl;
	Push(LQStack, 1);
	Push(LQStack, 2);
	Push(LQStack, 3);
	Push(LQStack, 4);
	cout << "lets traverse"<<endl;
	Traversal(*LQStack);
	cout << endl << "Is the stack empty?";
	isEmpty(*LQStack);
	cout << endl<< "Pop the elem out!" << endl;
	Pop(LQStack, &a);
	Pop(LQStack, &a);
	Pop(LQStack, &a);
	cout << "lets traverse" << endl;
	Traversal(*LQStack);
	cout << endl << "Is the stack  empty?"<<endl;
	isEmpty(*LQStack);
	cout <<endl<< "lets destroy the stack!"<<endl;
	DestroyStack(LQStack);
	cout << "OK!";
//of main

(上图为实现结果)

由于考虑到队列这种数据类型有虚假溢出情况(也就是空间明明够但队尾-队首!=空间长度)

所以下面是循环队列的实现:

#include<iostream>
using namespace std;
#define ERROR 0
#define OK 1
#define OVERFLOW 0
#define debug cout<<"OK";
#define FALSE 0
#define TRUE 1
#define MAXSIZE 20
#define judgee cout<<"Is the Queue Empty?"<<endl
#define judgef cout<<"Is the Queue Full?"<<endl
//约定:区分队列空和队列满
//如果队列为空:则Q->rear = Q->front;
//如果队列满,则(Q->rear+1)/MAXSIZE = Q->front;
typedef int Status;//数据类型
typedef int Etype;//元素的种类
typedef struct {
	Etype data[MAXSIZE]; //数据类型
	int rear;//头尾指针
	int front;
}LQueue;//循环链表命名


//ADT
//队列初始化
void InitQueue(LQueue* Q) {
	Q->rear = 0;
    Q->front = 0;
}
//判空队列
bool isEmpty(LQueue Q) {
	if (Q.front == Q.rear) {
		cout << "true";
	return true;}
	else{
		cout << "false"; }
	return false;
}
//判满队列
bool isFull(LQueue Q) {
	if ((Q.rear + 1) % MAXSIZE == Q.front)
	{cout << "true";
	return true;
	}
	else {
		cout << "false";
		return false;
	}
}
//清空队列
Status ClearQ(LQueue* Q) {
	InitQueue(Q);
	return OK;
}
//销毁队列
Status DestroyQ(LQueue* Q) {
	free(Q);
	return OK;
}
//入队
Status EnQueue(LQueue* Q, Etype data) {
	if ((Q->rear +1) %MAXSIZE != Q->front) {
		Q->data[Q->rear] = data;
		Q->rear = (Q->rear + 1) % MAXSIZE;
		return OK;
	}
	return ERROR;
}
//出队
Status Dequeue(LQueue* Q, Etype *data) {
	if (Q->rear != Q->front && !isFull(*Q)) {
		*data = Q->data[Q->front];
		Q->front = (Q->front+1) % MAXSIZE;
		return OK;
	}
	return ERROR;
}
//遍历打印
void Traversal(LQueue Q) {
		for (int i = Q.front; i < Q.rear; i=(i+1)% MAXSIZE) {
			cout << Q.data[i]<<" ";
		}
	}
	int main() {
		int n, data,a,size = 0;
		LQueue*  Q = new LQueue;
		InitQueue(Q);
		cout << "Initing Queue Success" << endl;
		judgee;
		isEmpty(*Q);
	    cout<<endl;
		judgef;
		isFull(*Q);
		cout << endl;
		cout << "how many num you want to insert"<<endl;
		cin >> n;
		cout << "Then input your number by Space" << endl;
		for (int i = 0; i < n; i++)
		{
			cin >> a;
			EnQueue(Q, a);
			size++;
		}
		cout << "Here come to your Traversal." << endl;
		Traversal(*Q);
		cout << endl;
		cout << "Now DeQueue!" << endl;
		Dequeue(Q,&data);
		cout << "Here come to your Traversal." << endl;
		Traversal(*Q);
		cout << endl;
		cout << "Clear your Queue"<<endl;
		ClearQ(Q);
		judgee;
		isEmpty(*Q);
		cout << endl;
		judgef;
		isFull(*Q);
		cout << endl;

		cout << "destroy your Queue" << endl;
		DestroyQ(Q);
		cout << "here is the end" << endl ;
		system("pause");

(上图为实现结果)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值