• 数据结构与算法课后题-第三章(顺序队和链队)


    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    #include   //引入头文件
    using namespace std;
    
    typedef int Elemtype;
    
    #define Maxsize 5
    #define ERROR 0
    #define OK    1
    
    typedef struct
    {
    	Elemtype data[Maxsize];
    	int front, rear;
    	int tag;
    }SqQueue;
    
    void InitQueue(SqQueue& Q)  //初始化队列
    {
    	Q.rear = Q.front = 0;
    	Q.tag = 0;
    }
    
    bool isEmpty(SqQueue Q)  //判断队空
    {
    	if (Q.rear == Q.front&& Q.tag==0 ) return OK;
    	else return ERROR;
    }
    
    bool EnQueue(SqQueue& Q, Elemtype x)
    {
    	if (Q.front==Q.rear&&Q.tag==1)  //队满
    	{
    		cout << "堆满啦,请腾出些许空间" << endl;
    		return ERROR;  //队满报错
    	}
    	Q.data[Q.rear] = x;
    	Q.rear = (Q.rear + 1) % Maxsize;
    	Q.tag = 1;
    	return OK;
    }
    
    bool DeQueue(SqQueue& Q, Elemtype& x)  //队空报错
    {
    	if (Q.rear == Q.front&&Q.tag==0) return ERROR;
    	x = Q.data[Q.front];
    	Q.front = (Q.front + 1) % Maxsize;
    	Q.tag = 0;
    	return OK;
    }
    
    int main(void)
    {
    	SqQueue Q;
    	InitQueue(Q);
    	EnQueue(Q, 1);
    	EnQueue(Q, 2);
    	EnQueue(Q, 3);
    	EnQueue(Q, 4);
    	EnQueue(Q, 5);
    	for (int i = 0; i < Maxsize; i++)
    		printf("data[%d]=%d\n", i, Q.data[i]);
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    在这里插入图片描述

    #include   //引入头文件
    using namespace std;
    
    typedef int Elemtype;
    
    #define Maxsize 5
    #define ERROR 0
    #define OK    1
    
    //====================队列---这样是循环队列,需要牺牲一位====================//
    typedef struct
    {
    	Elemtype data[Maxsize];
    	int front, rear;
    }SqQueue;
    
    void InitQueue(SqQueue& Q)  //初始化队列
    {
    	Q.rear = Q.front = 0;
    }
    
    bool QueueEmpty(SqQueue Q)  //判断队空
    {
    	if (Q.rear == Q.front) return OK;
    	else return ERROR;
    }
    
    bool EnQueue(SqQueue& Q, Elemtype x)  //进队
    {
    	if ((Q.rear + 1) % Maxsize == Q.front) //判断队列是否满
    	{
    		cout << "堆满啦,请释放一些空间" << endl;
    		return ERROR;  //队满报错
    	}
    	Q.data[Q.rear] = x;
    	Q.rear = (Q.rear + 1) % Maxsize;
    	return OK;
    }
    
    bool DeQueue(SqQueue& Q, Elemtype& x) //退队
    {
    	if (Q.rear == Q.front) return ERROR; //判断队列是为空
    	x = Q.data[Q.front];
    	Q.front = (Q.front + 1) % Maxsize;
    	return OK;
    }
    //=====================================================================//
    
    //========================堆栈---这样是顺序堆栈========================//
    typedef 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;
    }
    //=====================================================================//
    
    void Inverse(SqStack& S, SqQueue& Q)
    {
    	int x = 0;
    	while (!QueueEmpty(Q))
    	{
    		DeQueue(Q,x);
    		Push(S, x);
    	}
    	Q.rear = Q.front = 0;
    	while (!StackEmpty(S))
    	{
    		Pop(S, x);
    		EnQueue(Q, x);
    	}
    }
    int main(void)
    {
    	SqQueue Q;
    	SqStack S;
    	InitQueue(Q);
    	InitStack(S);
    	EnQueue(Q, 1);
    	EnQueue(Q, 2);
    	EnQueue(Q, 3);
    	EnQueue(Q, 4);
    	for (int i = 0; i < Maxsize; i++)
    		printf("data[%d]=%d\n", i, Q.data[i]);
    	Inverse(S, Q);
    	for (int i = 0; i < Maxsize; i++)
    		printf("data[%d]=%d\n", i, Q.data[i]);
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
  • 相关阅读:
    思腾云计算
    基于Springboot外卖系统16:菜品修改模块+菜品信息回显+ID查询口味列表+组装数据并返回
    c shell 命令行参数处理getopts.sh
    文章聚合怎么进行文章伪原创
    编程入门自学方法,中文编程基础知识讲解
    Java露营基地预约小程序预约下单系统源码
    PDF 转Word 开源库
    【每日一练】图解:链表内指定区间反转
    MySQL中获取时间的方法
    5233: 【J1】【map】统计数字
  • 原文地址:https://blog.csdn.net/qq_41735476/article/details/133442684