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

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);
	}
}
相关推荐
神器阿龙33 分钟前
排序算法-冒泡排序
数据结构·算法·排序算法
九章数学体系1 小时前
九章数学体系:打破“吃苦悖论”,重构学习真谛
数据结构·学习·算法·数学建模·拓扑学
一川月白7092 小时前
数据结构---概念、数据与数据之间的关系(逻辑结构、物理结构)、基本功能、数据结构内容、单向链表(该奶奶、对象、应用)
c语言·数据结构·算法·哈希算法·单向链表·数据关系
闪电麦坤952 小时前
数据结构:链表(Linked List)
数据结构·链表
东东最爱敲键盘4 小时前
数据结构: 双向链表
数据结构
云泽80813 小时前
数据结构前篇 - 深入解析数据结构之复杂度
c语言·开发语言·数据结构
逝雪Yuki13 小时前
数据结构与算法——字典(前缀)树的实现
数据结构·c++·字典树·前缀树·左程云
技术思考者14 小时前
Leetcode - 反转字符串
数据结构·算法·leetcode
熬了夜的程序员15 小时前
【华为机试】34. 在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·华为od·华为·面试·golang
草莓熊Lotso16 小时前
【数据结构初阶】--排序(一):直接插入排序,希尔排序
c语言·数据结构·经验分享·其他·排序算法