当前位置:   article > 正文

数据结构—链队的操作及实现(CC++)_链队列的初始化,入队,出队,取队头元素的主函数的代码

链队列的初始化,入队,出队,取队头元素的主函数的代码

数据结构—链队的操作及实现(C/C++)

1.0初始化定义

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100    //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int QElemType;    // 自己定义的类型
typedef int status;      //  上同 
using namespace std;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.1链队的结构体定义

typedef struct QNode
{
	QElemType data;   
	struct QNode *next;
}QNode,*Queueptr;
//嵌套定义
typedef struct
{
	Queueptr front; //Queueptr类型的头指针
	Queueptr rear;	//Queueptr类型的尾指针
}LinkQueue;//也就是说Queueptr类型的头指针里面包括数据域和next域
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

1.2链队列的入队操作

status ENQueue(LinkQueue *Q,QElemType e)
{
	QNode*p;
	p=new QNode;
	p->data=e; 
	p->next=NULL;
	Q->rear->next=p;
	Q->rear=p;
	return OK;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

1.3链队列的出队操作

status DeQueue(LinkQueue *Q,QElemType *e)
{
	QNode*p;
	if(Q->front==Q->rear) 
	return ERROR;
	p=Q->front->next;
	*e=p->data;
	Q->front->next=p->next;
	if(Q->rear==p)	
	Q->rear=Q->front;
	delete p;
	return OK;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

1.4取链队的队头元素

status GetHead(LinkQueue *Q)
{
	if(Q->front==Q->rear)
	{
		return ERROR;
	}
	else
	{
		return Q->front->next->data;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

1.5输出队列的元素

status printQueue(LinkQueue *Q)
{
	cout<<"当前队列:";
	Queueptr p=Q->front->next;
	while(p)
	{
		cout<<'\40'<<p->data ;
		p=p->next;
	}
	cout<<endl;
	return OK;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

1.6求链队的长度

status QueueLength(LinkQueue *Q)
{
	int Length=0;
	if(Q->front==Q->rear)
	{
		return 0;
	}
	Queueptr p=Q->front->next;
	while(p)
	{
		p=p->next;
		Length++;
	}
	return Length;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

1.7 C 主函数部分

int main()
{
	int n,m,t;
	QElemType e;
	LinkQueue Q;
	InitQueue(&Q);
	printf("请输入需要入队的次数:");
	scanf("%d",&n);
	for(int i=1 ;i<=n;i++)
	{
	printf("请输入第%d个元素的值:",i);
	scanf("%d",&m);
	ENQueue(&Q,m);
	}
	printQueue(&Q);
	printf("当前对头元素为:%d\n",GetHead(&Q));
	printf("当前链队长度为:%d\n",QueueLength(&Q));
	printf("\n");
	printf("\n");
	printf("请输入需要出队的次数:");
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
	DeQueue(&Q,&e);
	}
	printQueue(&Q);
	printf("当前对头元素为:%d\n",GetHead(&Q));
	printf("当前链队长度为:%d\n",QueueLength(&Q));
	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

1.8 C++主函数部分

int main()
{
	int n,m,t;
	QElemType e;
	LinkQueue Q;
	InitQueue(&Q);
	cout<<"请输入需要入队的次数:";
	cin>>n;
	for(int i=1 ;i<=n;i++)
	{
	cout<<"请输入第"<<i<<"个元素的值:";
	cin>>m;
	ENQueue(&Q,m);
	}
	printQueue(&Q);
	cout<<"当前对头元素为:"<<GetHead(&Q)<<endl;
	cout<<"当前链队长度为:"<<QueueLength(&Q);
	cout<<endl<<endl;
	cout<<"请输入需要出队的次数:";
	cin>>t;
	for(int i=1;i<=t;i++)
	{
	DeQueue(&Q,&e);
	}
	printQueue(&Q);
	cout<<"当前对头元素为:"<<GetHead(&Q)<<endl;
	cout<<"当前链队长度为:"<<QueueLength(&Q);
	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

1.9 C全部代码

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100    //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int QElemType;    // 自己定义的类型
typedef int status;      //  上同 
//链队列定义
typedef struct QNode
{
	QElemType data;   
	struct QNode *next;
}QNode,*Queueptr;
//嵌套定义
typedef struct
{
	Queueptr front; //Queueptr类型的头指针
	Queueptr rear;	//Queueptr类型的尾指针
}LinkQueue;//也就是说Queueptr类型的头指针里面包括数据域和next域

//链队列的初始化
status InitQueue(LinkQueue *Q)
{
	Q->front=Q->rear=(QNode*)malloc(sizeof(QNode)); //为头指针和尾指针开辟动态空间
	Q->front->next=NULL;     //初始头指针的next为空
	return OK;
}
//链队列的入队操作
status ENQueue(LinkQueue *Q,QElemType e)
{
	QNode*p;
	p=new QNode;
	p->data=e; 
	p->next=NULL;
	Q->rear->next=p;
	Q->rear=p;
	return OK;
}
//链队列的出队操作
status DeQueue(LinkQueue *Q,QElemType *e)
{
	QNode*p;
	if(Q->front==Q->rear) 
	return ERROR;
	p=Q->front->next;
	*e=p->data;
	Q->front->next=p->next;
	if(Q->rear==p)	
	Q->rear=Q->front;
	delete p;
	return OK;
}
//取链队的队头元素
status GetHead(LinkQueue *Q)
{
	if(Q->front==Q->rear)
	{
		return ERROR;
	}
	else
	{
		return Q->front->next->data;
	}
}
//输出队列的元素
status printQueue(LinkQueue *Q)
{
	printf("当前队列:");
	Queueptr p=Q->front->next;
	while(p)
	{
		printf("%4d",p->data);
		p=p->next;
	}
	printf("\n");
	return OK;
}
//求链队的长度
status QueueLength(LinkQueue *Q)
{
	int Length=0;
	if(Q->front==Q->rear)
	{
		return 0;
	}
	Queueptr p=Q->front->next;
	while(p)
	{
		p=p->next;
		Length++;
	}
	return Length;
}

int main()
{
	int n,m,t;
	QElemType e;
	LinkQueue Q;
	InitQueue(&Q);
	printf("请输入需要入队的次数:");
	scanf("%d",&n);
	for(int i=1 ;i<=n;i++)
	{
	printf("请输入第%d个元素的值:",i);
	scanf("%d",&m);
	ENQueue(&Q,m);
	}
	printQueue(&Q);
	printf("当前对头元素为:%d\n",GetHead(&Q));
	printf("当前链队长度为:%d\n",QueueLength(&Q));
	printf("\n");
	printf("\n");
	printf("请输入需要出队的次数:");
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
	DeQueue(&Q,&e);
	}
	printQueue(&Q);
	printf("当前对头元素为:%d\n",GetHead(&Q));
	printf("当前链队长度为:%d\n",QueueLength(&Q));
	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

2.0 C++全部代码

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100    //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int QElemType;    // 自己定义的类型
typedef int status;      //  上同 
using namespace std;

//链队列定义
typedef struct QNode
{
	QElemType data;   
	struct QNode *next;
}QNode,*Queueptr;
//嵌套定义
typedef struct
{
	Queueptr front; //Queueptr类型的头指针
	Queueptr rear;	//Queueptr类型的尾指针
}LinkQueue;//也就是说Queueptr类型的头指针里面包括数据域和next域

//链队列的初始化
status InitQueue(LinkQueue *Q)
{
	Q->front=Q->rear=new QNode; //为头指针和尾指针开辟动态空间
	Q->front->next=NULL;     //初始头指针的next为空
	return OK;
}
//链队列的入队操作
status ENQueue(LinkQueue *Q,QElemType e)
{
	QNode*p;
	p=new QNode;
	p->data=e; 
	p->next=NULL;
	Q->rear->next=p;
	Q->rear=p;
	return OK;
}
//链队列的出队操作
status DeQueue(LinkQueue *Q,QElemType *e)
{
	QNode*p;
	if(Q->front==Q->rear) 
	return ERROR;
	p=Q->front->next;
	*e=p->data;
	Q->front->next=p->next;
	if(Q->rear==p)	
	Q->rear=Q->front;
	delete p;
	return OK;
}
//取链队的队头元素
status GetHead(LinkQueue *Q)
{
	if(Q->front==Q->rear)
	{
		return ERROR;
	}
	else
	{
		return Q->front->next->data;
	}
}
//输出队列的元素
status printQueue(LinkQueue *Q)
{
	cout<<"当前队列:";
	Queueptr p=Q->front->next;
	while(p)
	{
		cout<<'\40'<<p->data ;
		p=p->next;
	}
	cout<<endl;
	return OK;
}
//求链队的长度
status QueueLength(LinkQueue *Q)
{
	int Length=0;
	if(Q->front==Q->rear)
	{
		return 0;
	}
	Queueptr p=Q->front->next;
	while(p)
	{
		p=p->next;
		Length++;
	}
	return Length;
}

int main()
{
	int n,m,t;
	QElemType e;
	LinkQueue Q;
	InitQueue(&Q);
	cout<<"请输入需要入队的次数:";
	cin>>n;
	for(int i=1 ;i<=n;i++)
	{
	cout<<"请输入第"<<i<<"个元素的值:";
	cin>>m;
	ENQueue(&Q,m);
	}
	printQueue(&Q);
	cout<<"当前对头元素为:"<<GetHead(&Q)<<endl;
	cout<<"当前链队长度为:"<<QueueLength(&Q);
	cout<<endl<<endl;
	cout<<"请输入需要出队的次数:";
	cin>>t;
	for(int i=1;i<=t;i++)
	{
	DeQueue(&Q,&e);
	}
	printQueue(&Q);
	cout<<"当前对头元素为:"<<GetHead(&Q)<<endl;
	cout<<"当前链队长度为:"<<QueueLength(&Q);
	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
  • 126

2.1运行截图

在这里插入图片描述

2.2参考书籍

《数据结构》(C语言版)(第2版)—严蔚敏 李冬梅 吴伟民 编著

2.3总结

多多关注!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/706990
推荐阅读
相关标签
  

闽ICP备14008679号