第三章 栈与队列的基本操作

 1.栈的建立与入栈出栈操作的实现过程

#include <stdio.h>
#define MaxSize 50

//建立一个顺序栈

typedef int Elemtype;
typedef struct SqStack
{
	Elemtype data[MaxSize];
	int top;
}SqStack;//顺序栈的结构体声明

void InitSqStack(SqStack &S) {
	S.top = -1;
}//初始化顺序栈

bool StackEmpty(SqStack S) {
	if (S.top == -1) {
		printf("栈不空");
		return true;
	}
	else
	{
		printf("栈为空");
		return false;
	}
}//判断栈是否为空

bool Push(SqStack& S,Elemtype &x) {
	if (S.top == MaxSize-1) {
		printf("栈满");
		return false;
	}
	else
	{
		S.data[++S.top] = x;
		return true;
	}
}

bool Pop(SqStack& S, Elemtype& x) {
	if (S.top == -1) {
		printf("栈为空");
		return false;
	}
	x = S.data[S.top--];
	return true;
}

bool Gettop(SqStack S) {
	if (S.top == -1) {
		printf("栈为空");
		return false;
	}
	printf("栈顶元素为:%d", S.data[S.top]);
}


int main() {
	int x;
	SqStack S;
	InitSqStack(S);
	scanf_s("%d", &x);
	while (x!=-1)
	{
		Push(S, x);
		scanf_s("%d", &x);
	}
	Gettop(S);
}

2.队列的基本操作

  •  循环队列的代码实现过程

#include "stdio.h"

#define MaxSize 10

typedef int ElemType;
typedef struct SqQueue {//建立一个循环队列
	ElemType data[MaxSize];//存储MaxSize-1个元素
	int front, rear;
};

void InitQueue(SqQueue  &Q) {
	Q.front = Q.rear = 0;
}//初始化队列

bool IsEmpty(SqQueue Q) {
	return Q.rear == Q.front;
}
//循环队列入队
bool EnQueue(SqQueue& Q, ElemType &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, ElemType &x) {
	if (Q.rear == Q.front) {//判断栈是否为空
		return false;
	}
	x = Q.data[Q.front];
	Q.front = (Q.front + 1) % MaxSize;
	printf("出队的元素为:%d\n", x);
	return true;
}


int main() {
	SqQueue Q;
	InitQueue(Q);
	bool ret;
	int x;
	ret = IsEmpty(Q);
	if (ret) {
		printf("SeQueue is Empty");
	}
	else
	{
		printf("SeQueue is not Empty");
	}
	while (scanf_s("%d",&x))
	{
		if (x==-1)
		{
			break;
		}
		EnQueue(Q, x);
	}
	if (ret) {
		printf("EnQueue success\n");
	}
	else
	{
		printf("EnQueue failed\n");
	}
	ElemType n;
	while (Q.rear!=Q.front)
	{
		DeQueue(Q, n);
	}
	ElemType element;//存储出队元素
	DeQueue(Q, element);
	return 0;
}

附运行结果图

  • 链队列的代码实现

#include "stdio.h"
#include <stdlib.h>

#define MaxSize 10

typedef int ElemType;
typedef struct LinkNode {//建立一个链表队列
	ElemType data;
	struct LinkNode* next;
}LinkNode;

typedef struct {
	LinkNode* rear, * front;//链表头链表尾
}LinkQueue;

void InitQueue(LinkQueue  &Q) {
	Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));//动态为头节点尾节点分配空间
	Q.front->next = NULL;//头节点的next指针为空
}//初始化队列

bool IsEmpty(LinkQueue Q) {
	if (Q.front == Q.rear)
		return true;
	else
		return false;
}
//链队列入队
bool  EnQueue(LinkQueue& Q, ElemType &x) {
	LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
	s->data = x;
	s->next = NULL;
	Q.rear->next = s;
	Q.rear = s;
	return true;
}

bool DeQueue(LinkQueue &Q, ElemType &x) {
	if (Q.front == Q.rear) {
		return false;
	}
	LinkNode* p = Q.front->next;
	x = p->data;
	Q.front->next = p->next;
	if (Q.rear == p)
		Q.rear = Q.front;
	printf("出队的元素为:%d\n", p->data);
	free(p);
	return true;
}


int main() {
	LinkQueue Q;
	InitQueue(Q);//初始化队列
	bool ret;
	int x;
	ret = IsEmpty(Q);
	if (ret) {
		printf("SeQueue is Empty");
	}
	else
	{
		printf("SeQueue is not Empty");
	}
	while (scanf_s("%d",&x))
	{
		if (x==-1)
		{
			break;
		}
		EnQueue(Q, x);
	}
	if (ret) {
		printf("EnQueue success\n");
	}
	else
	{
		printf("EnQueue failed\n");
	}
	ElemType n;
	while (Q.rear!=Q.front)
	{
		DeQueue(Q, n);
	}
	ElemType element;//存储出队元素
	DeQueue(Q, element);
	return 0;
}

        附运行结果图,如下所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值