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

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);
	}
}
相关推荐
^^为欢几何^^2 小时前
lodash中_.difference如何过滤数组
javascript·数据结构·算法
ahauedu3 小时前
案例分析-Stream List 中取出值最大的前 5 个和最小的 5 个值
数据结构·list
X同学的开始5 小时前
数据结构之二叉树遍历
数据结构
AIAdvocate8 小时前
Pandas_数据结构详解
数据结构·python·pandas
jiao000018 小时前
数据结构——队列
c语言·数据结构·算法
kaneki_lh8 小时前
数据结构 - 栈
数据结构
铁匠匠匠8 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计
C-SDN花园GGbond8 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
CV工程师小林10 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
Navigator_Z10 小时前
数据结构C //线性表(链表)ADT结构及相关函数
c语言·数据结构·算法·链表