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

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);
	}
}
相关推荐
Rachela_z3 小时前
代码随想录算法训练营第十四天| 二叉树2
数据结构·算法
细嗅蔷薇@3 小时前
迪杰斯特拉(Dijkstra)算法
数据结构·算法
S-X-S3 小时前
算法总结-数组/字符串
java·数据结构·算法
努力学习java的哈吉米大王4 小时前
数据结构-队列
数据结构
因兹菜4 小时前
[LeetCode]day9 203.移除链表元素
算法·leetcode·链表
萌の鱼4 小时前
leetcode 2080. 区间内查询数字的频率
数据结构·c++·算法·leetcode
萝卜青今天也要开心6 小时前
读书笔记-《Redis设计与实现》(一)数据结构与对象(下)
java·数据结构·redis·学习
逊嘘9 小时前
【Java数据结构】了解排序相关算法
数据结构·算法·排序算法
_周游9 小时前
【数据结构】_链表经典算法OJ(力扣/牛客第二弹)
数据结构·算法·链表
LUCIAZZZ10 小时前
Hot100之双指针
java·数据结构·算法