队列的链式存储结构(作业)

#include <stdio.h>
#include <stdlib.h>
#include<iostream> 
#define MAXSIZE 100
using namespace std;
#define ERROR 0
#define OK 1

#define OVERFLOW -2
typedef int QElemType;
typedef int Status;
//#define STACK_INIT_SIZE 100//存储空间初始分配量 

//00 定义 
typedef struct QNode
{
	QElemType data;
	struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
	QueuePtr front; //队头指针 
	QueuePtr rear;  //队尾指针 
}LinkQueue;
//01 初始化
Status InitQueue(LinkQueue &Q)
{//构造一个空队列
    Q.front=Q.rear=new QNode;//生成新结点作为头结点,队头和队尾指针指向此结点
    Q.front->next=NULL; //头结点的指针域置空
    return OK;
 } 
 //02 创建非空链队列 
 Status CreateQueue1(LinkQueue &Q,int n)
{
	//InitQueue(Q);
	int x,i=0;
	QNode *p;
   // p=new QNode;//为入队元素分配结点空间,用指针p指向 
    printf("输入正整数:"); 
	for(i=0;i<n;i++)
	{
	    cin>>x;
	     p=new QNode;//为入队元素分配结点空间,用指针p指向
         p->data=x;//将新结点数据域置为x
        p->next=NULL; Q.rear->next=p;//将新结点插入到队尾 
    	Q.rear=p;//修改队尾指针 
	
	}
	return OK; 
}

 Status CreateQueue2(LinkQueue &Q,int n)
{
	int x,i=0;
    QNode *head; 
	QNode *p; 
	head = new QNode;
	head->next = NULL;
 
	Q.front = head; Q.rear = head;
	cout << "请输入元素" << endl;
	while (i<n)
	{   cin >> x;
		p = new QNode;
			p->data = x;
			p->next = NULL;	
			Q.rear->next = p;
		    Q.rear = p;
		i++;
	}
//	return Q;
}

//03 将一个数x插到队尾 
Status EnQueue(LinkQueue &Q,QElemType x)
{//插入元素x为Q的新的队尾元素 
    QNode *p;
	p=new QNode;//为入队元素分配结点空间,用指针p指向 
	p->data=x;//将新结点数据域置为e 
	p->next=NULL; Q.rear->next=p;//将新结点插入到队尾 
	Q.rear=p;//修改队尾指针 
	return OK;
  }  
//04 删除队头元素 
Status DeQueue(LinkQueue &Q,QElemType &x)
{//删除Q的队头元素,用x返回其值
    QNode *p;
	if(Q.front==Q.rear) return ERROR;//若队列空,则返回ERROR
	p=Q.front->next;//p指向队头元素
	x=p->data;//e保存队头元素的值 
	Q.front->next=p->next;//修改头指针 
	if(Q.rear==p)  Q.rear=Q.front;//最后一个元素被删,队尾指针指向头结点 
	delete p;//释放原队头元素的空间 
	return OK;
//	printf("队头元素%d",Q.front->data);
	cout << Q.front->data << endl;
}
//05 判断链队是否为空,不空输出队头元素的值 
Status JudgeQueue(LinkQueue &Q)
{
	QElemType x;
	if(Q.front==Q.rear)
	{
		printf("队空"); 
	}
	else 
	{
		x=Q.front->next->data;
		printf("队头元素的值为:%d\n",x);
	}
 } 

//06 打印链队
Status DisplayQueue (LinkQueue &Q)    //打印链队
{
	if (Q.front==Q.rear) 
		cout << "链队为空" << endl;
	else
	{
		QNode *p;
		p = Q.front->next;//P指向队头元素 
		while (p!=NULL)
		{
		cout << p->data << endl;
		//printf("%d",p->data);
			p = p->next;
		}
		
	}
	return OK;
	
}

 int main()
 {
 	int n;
 	QElemType x;
 	LinkQueue Q;
 	//01 初始化 
 	InitQueue(Q);
 	//02 创建非空链队 
 	printf("输入元素个数:") ;
 	scanf("%d",&n);
 	CreateQueue1(Q,n);
    DisplayQueue (Q);  
 	//03 插入元素到队尾 
 	printf("插入的新的元素为:");
	scanf("%d",&x); 
 	EnQueue(Q,x);
 	DisplayQueue (Q);  
 	//04 删除队头元素 
 	DeQueue(Q,x);
 	printf("被删除元素的值:%d\n",x); 
 	//05 判断队空否
	 JudgeQueue(Q); 
 	//06 打印队中所有元素
	 printf("打印队中所有元素:") ;
	 DisplayQueue (Q);     
	
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值