数据结构:队列的链表结构(含完整代码,可复制)

1.输出队列

cpp 复制代码
void outlin(LinkQueue qq)
{
	p=qq.front->next;
	while(p!=NULL)
	{printf(" data=%4d\n",p->data);
	p=p->next;}
	printf("\n outend \n\n");
}

2.入队一个元素

cpp 复制代码
void insert(LinkQueue *qe,int x)
{
	s=(NodeType *)malloc(sizeof(NodeType));
	s->data=x;s->next=NULL;
	qe->rear->next=s;
	qe->rear=s;
}

3.出队一个元素

cpp 复制代码
void  dele(LinkQueue *qe)
{
	ElemType x;
	if(qe->front==qe->rear){printf("队列为空。\n");x=0;}
	else
	{
		p=qe->front->next;
		qe->front->next=p->next;
		if(p->next==NULL) qe->rear=qe->front;
		x=p->data;printf("%d\n",x);free(p);
	}
}

5.建立链表队列

cpp 复制代码
void creat(LinkQueue *qe)
{
	int i,n,x;
	h=(NodeType *)malloc(sizeof(NodeType));
	h->next=NULL;qe->front=h;qe->rear=h; 
	printf("n=");scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		printf("\n data=");scanf("%d",&x);
		insert(qe,x);
	}
}

6.完整代码

cpp 复制代码
#include "stdio.h"
#include "stdlib.h"
typedef int ElemType; 
typedef struct NodeType
{
	ElemType data;
	struct NodeType *next;
}NodeType;
typedef struct
{
	NodeType *front,*rear;
}LinkQueue;
NodeType *p,*s,*h;
void outlin(LinkQueue qq);
void creat(LinkQueue *qe);
void insert(LinkQueue *qe,ElemType x); 
void dele(LinkQueue *qe);
main()
{
	LinkQueue que;ElemType  x,y;
	int cord;
	do
	{
		printf("\n 主菜单\n");
		printf("\n 1 建立链表队列\n");
		printf("\n 2 入队一个元素 X \n");
		printf("\n 3 出队一个元素\n");
		printf("\n 4 程序结束运行\n");
		printf("\n-------------------\n");
		printf("请输入您的选择(1,2,3,4)");scanf("%d",&cord);
		switch(cord)
		{
		case 1:{creat(&que);
				outlin(que);
			   }break;
		case 2:{printf("\n x=?");scanf("%d",&x);
				insert(&que,x); outlin(que);
			   }break;
		case 3:{dele(&que);
				outlin(que);
				}break;
		case 4:exit(0);
		}
	}while(cord<=4);
}
void outlin(LinkQueue qq)
{
	p=qq.front->next;
	while(p!=NULL)
	{printf(" data=%4d\n",p->data);
	p=p->next;}
	printf("\n outend \n\n");
}
void insert(LinkQueue *qe,int x)
{
	s=(NodeType *)malloc(sizeof(NodeType));
	s->data=x;s->next=NULL;
	qe->rear->next=s;
	qe->rear=s;
}

void  dele(LinkQueue *qe)
{
	ElemType x;
	if(qe->front==qe->rear){printf("队列为空。\n");x=0;}
	else
	{
		p=qe->front->next;
		qe->front->next=p->next;
		if(p->next==NULL) qe->rear=qe->front;
		x=p->data;printf("%d\n",x);free(p);
	}
}

void creat(LinkQueue *qe)
{
	int i,n,x;
	h=(NodeType *)malloc(sizeof(NodeType));
	h->next=NULL;qe->front=h;qe->rear=h; 
	printf("n=");scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		printf("\n data=");scanf("%d",&x);
		insert(qe,x);
	}
}
相关推荐
pystraf18 分钟前
UOJ 228 基础数据结构练习题 Solution
数据结构·c++·算法·线段树
祁同伟.36 分钟前
【数据结构 · 初阶】- 堆的实现
c语言·数据结构
软行2 小时前
LeetCode 每日一题 2845. 统计趣味子数组的数目
数据结构·c++·算法·leetcode
永远在Debug的小殿下2 小时前
查找函数【C++】
数据结构·算法
我想进大厂3 小时前
图论---染色法(判断是否为二分图)
数据结构·c++·算法·深度优先·图论
Yhame.3 小时前
【使用层次序列构建二叉树(数据结构C)】
c语言·开发语言·数据结构
雾月554 小时前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
Pasregret4 小时前
访问者模式:分离数据结构与操作的设计模式
数据结构·设计模式·访问者模式
阿让啊5 小时前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北5 小时前
力扣-160.相交链表
算法·leetcode·链表