C/C++ 栈和队列

顺序栈的实现

#include<iostream>
#define MaxSize 20
using namespace std;
typedef struct {
	int data[MaxSize];
	int top;
}SqStack;
void InitStack(SqStack &S) {	//栈初始化
	S.top = -1;
}
bool StackEmpty(SqStack S) {	//栈是否为空
	if (S.top == -1)	return true;
	return false;
}
bool Push(SqStack& S, int x) {	//栈中添加元素
	if (S.top + 1 == MaxSize)	return false;
	S.data[++S.top] = x;
	return true;
}
bool Pop(SqStack& S, int& x) {	//栈中取出元素
	if (S.top == -1)	return false;
	x = S.data[S.top];
	S.top--;
	return true;
}
bool GetTop(SqStack S, int& x) {	//读栈顶元素
	if (S.top == -1)	return false;
	x = S.data[S.top];
	return true;
}
void Destroy(SqStack& S) {
	S.top = -1;
}
int main() {
	int N = 24;
	SqStack S;
	InitStack(S);
	int x;
	for (int i = 0; i < N; i++) {
		x = i * 10 + 7;
		if (Push(S, x)) {
			cout << "入栈成功,元素为:"<<x << endl;
		}
		else {
			cout << "入栈失败"<<endl;
		}
	}
	if (GetTop(S, x)) {
		cout << "此时栈顶元素为" << x << endl;
	}
	else {
		if (StackEmpty(S)) {
			cout << "此时栈为空" << endl;
		}
	}
	for (int i = 0; i < N; i++) {
		if (Pop(S, x)) {
			cout << "出栈成功,出栈元素为:" << x << endl;
		}
		else {
			cout << "出栈失败" << endl;
		}
	}
	if (GetTop(S, x)) {
		cout << "此时栈顶元素为" << x << endl;
	}
	else {
		if (StackEmpty(S)) {
			cout << "此时栈为空" << endl;
		}
	}
	Destroy(S);
	return 0;
}

链栈的实现

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct LinkNode{
	int data;
	struct LinkNode* next;
}*LiStack;
void InitStack(LiStack& S) {
	S->next = NULL;
}
bool EmptyStack(LiStack S) {
	if (S->next == NULL)	return true;
	return false;
}
void Push(LiStack& S, int x) {
	S->data = x;
	LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
	p->next = S;
	S = p;
}
bool Pop(LiStack& S, int& x) {
	if (S->next == NULL)	return false;
	LinkNode*p = S->next;
	x = p->data;
	S->next = p->next;
	free(p);
	return true;
}
bool GetTop(LiStack S, int& x) {
	if (S->next == NULL)	return false;
	x = S->next->data;
}
void Destroy(LiStack& S) {
	while (S) {
		LinkNode* p = S;
		S = S->next;
		free(p);
	}
}
int main() {
	LinkNode* S=(LinkNode*)malloc(sizeof(LinkNode));
	InitStack(S);
	int x;
	int N = 12;
	if (EmptyStack(S))	cout << "栈为空" << endl;
	else cout << "栈非空" << endl;
	for (int i = 0; i < N; i++) {
		x = i * 12 * 7;
		Push(S, x);
		cout << "入栈元素为:" << x<<endl;
	}
	if (EmptyStack(S))	cout << "栈为空" << endl;
	else cout << "栈非空" << endl;
	if (GetTop(S, x))	cout << "栈顶元素为:" << x << endl;
	else cout << "栈为空" << endl;
	for (int i = 0; i < N + 5; i++) {
		if (Pop(S, x)) {
			cout << "出栈元素为:" << x << endl;
		}
		else{
			cout << "栈为空,出栈失败" << endl;
		}
	}
	if (GetTop(S, x))	cout << "栈顶元素为:" << x << endl;
	else cout << "栈为空" << endl;
	Destroy(S);
	return 0;
}

循环队列

#include<iostream>
#define MaxSize 12
using namespace std;
typedef struct {
	int data[MaxSize];
	int front, rear;
}SqQueue;
void InitQueue(SqQueue& Q) {	//队列初始化
	Q.front = Q.rear = 0;
}
bool isEmpty(SqQueue Q) {	//队列判空
	if (Q.front == Q.rear)	return true;
	return false;
}
bool EnQueue(SqQueue& Q, int x) {	//入队操作
	if ((Q.rear + 1) % MaxSize == Q.front)	return false;
	Q.data[Q.rear] = x;
	Q.rear = (Q.rear + 1) % MaxSize;
	return true;
}
bool DeQueue(SqQueue& Q, int &x) {	//出队操作
	if (isEmpty(Q))		return false;
	x = Q.data[Q.front];
	Q.front = (Q.front + 1) % MaxSize;
	return true;
}
int main() {
	SqQueue Q;
	InitQueue(Q);
	int N = 15;
	int x;
	for (int i = 0; i < N; i++) {
		x = i * 18 + 12;
		if (EnQueue(Q, x))		cout << "入队成功,元素为:" << x<<endl;
		else					cout << "入队失败"<<endl;
	}
	for (int i = 0; i < N -8; i++) {
		if (DeQueue(Q, x))		cout << "出队成功,元素为:" << x << endl;
		else					cout << "出队失败" << endl;
	}

	for (int i = 0; i < N-3; i++) {
		x = i ;
		if (EnQueue(Q, x))		cout << "入队成功,元素为:" << x << endl;
		else					cout << "入队失败" << endl;
	}

	for (int i = 0; i < N ; i++) {
		if (DeQueue(Q, x))		cout << "出队成功,元素为:" << x << endl;
		else					cout << "出队失败" << endl;
	}
	return 0;
}

队列的链式存储

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct LinkNode {
	int data;
	struct LinkNode* next;
}LinkNode;
typedef struct {
	LinkNode* front, *rear;
}LinkQueue;
void InitQueue(LinkQueue& Q) {
	Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
	Q.front->next = NULL;
}
bool IsEmpty(LinkQueue Q) {
	if (Q.front==Q.rear)	return true;
	return false;
}
void EnQueue(LinkQueue& Q,int x) {
	Q.rear->data = x;
	LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
}
bool DeQueue(LinkQueue& Q, int& x) {
	if (Q.front == Q.rear)	return false;
	x = Q.front->data;
	LinkNode* p = Q.front;
	Q.front = Q.front->next;
	free(p);
	return true;
}
int N = 15;
int main() {
	LinkQueue Q;
	InitQueue(Q);
	int x;
	for (int i = 0; i < N; i++) {
		x = 3 * i + 13;
		cout << "入队元素:" << x << endl;
		EnQueue(Q, x);
	}
	for (int i = 0; i < N; i++) {
		DeQueue(Q, x);
		cout << "出队元素:" << x << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值