数据结构-队列的基本操作实现

版权声明:欢迎分享(指明出处),若有错误还请指正!!! https://blog.csdn.net/zj19941201/article/details/71218929
/*main*/
#include<stdio.h>
#include<stdlib.h>
#define TRUE		1
#define ERROR		0
#define	FALSE		0
#define	OK			1
#define OVERFLOW	-2

typedef int Status;

typedef struct QNode{
	int	data;
	struct QNode *next;
}QNode,*Queueptr;
typedef struct{
	Queueptr front;
	Queueptr rear;
	Queueptr p;
}LinkQueue;
#include"duiliehs.h"
main()
{
	LinkQueue Q;
	Init(Q);
	int cnt=1,t;
	printf("入队顺序:");
	while(cnt<11)
	{	
		EnQueue(Q,cnt);
		printf("%d ",cnt);
		cnt++;
	}
	putchar(10);
	putchar(10);
	printf("队列中元素排放顺序:");
	print(Q);
	putchar(10);
	putchar(10);
	printf("出队顺序:");
	while(Q.front!=Q.rear)
	{
		printf("%d ",DeQueue(Q,t));
	}
	putchar(10);
	putchar(10);
	DestroyQueue(Q);
	putchar(10);
	putchar(10);
	printf("**由上可知“队列是一种先进先出的线性表。”**");
		return 0;	
}
/*duiliehs.h*/
Status Init(LinkQueue &Q)
{
	Q.front=Q.rear=Q.p=(Queueptr)malloc(sizeof(QNode));
	if(!Q.front)exit(OVERFLOW);
	Q.front->next=NULL;
	return OK;
}

Status EnQueue(LinkQueue &Q,int e)
{
	
	Queueptr p=(Queueptr)malloc(sizeof(QNode));
	if(!p)exit(OVERFLOW);
	p->data=e;p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	return OK;
}

Status DeQueue(LinkQueue &Q,int &e)
{
	if(Q.front==Q.rear)return ERROR;
	Queueptr p=Q.front->next;
	e=p->data;
	Q.front->next=p->next;
	if(Q.rear==p)Q.rear=Q.front;
	free(p);
	return e;
}

Status DestroyQueue(LinkQueue &Q)
{
	while(Q.front)
	{
		Q.rear=Q.front->next;
		free(Q.front);
		Q.front=Q.rear;
	}
	printf("销毁队列成功!");
	return OK; 
}

void print(LinkQueue &Q)
{
	while(Q.p->next!=NULL)
	{
		Q.p=Q.p->next;
		printf("%d ",Q.p->data);
	}
}

猜你喜欢

转载自blog.csdn.net/zj19941201/article/details/71218929