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

1.输出队列

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.入队一个元素

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.出队一个元素

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.建立链表队列

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.完整代码

#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);
	}
}

你可能感兴趣的:(数据结构,队列,数据结构,链表,队列)