队列

链队列

 #include <iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
using namespace std;

#define ERROR            0
#define TRUE             1
#define FALSE  			 0
#define OK 				 1
#define STACK_INIT_SIZE 20
#define STACKINCREMENT  10
typedef char QElemType;

typedef struct QNode{
	QElemType data;
	struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue &Q)				//构造一个空队列 
{
	Q.rear = Q.front = new QNode;
	if(NULL == Q.rear)
	{
		exit(0);
		return 0;
	}
	Q.rear->next = NULL;
	return 1;	
} 
int DestrotQueue(LinkQueue &Q)			//销毁队列 
{
	if(Q.front == NULL)
	{
		printf("队列已经销毁\n");
		return 1; 
	}
	 
	while(Q.front)
	{
		Q.rear = Q.front->next;
		free(Q.front);
		Q.front = Q.rear;
	}
	printf("队列已销毁\n");
	return OK;
}
int ClearQueue(LinkQueue &Q)			//清空队列 
{
	if(Q.rear == Q.front)
	{
		printf("队列已空\n"); 
		return 1;
	}
	QueuePtr q, p;
	q = Q.front->next;
	Q.rear = Q.front;
	while(q)
	{
		p = q;
		q = q->next;
		free(p);
	}
	printf("队列已清空\n");	
} 

int QueueEmpty(LinkQueue Q)					//判断队列是否为空 
{
	if(Q.rear == Q.front)
	return 1;
	else 
	return 0;
} 
	
int QueueLength(LinkQueue Q)				//队列的长度 
{
	int i = 0;
	QueuePtr p;
	p = Q.front;
	while(p != Q.rear)
	{
		i++;
		p = p->next;
	}
	return i;
} 
	
int GetHead(LinkQueue Q, QElemType &e)	//得到队头元素 
{
	if(Q.rear == Q.front)
	{
		return 0;
	}
	e = Q.front->next->data;
	return 1;
}
int DeQueue(LinkQueue &Q, QElemType &e)	//删除队头元素 
{
	if(Q.rear == Q.front)
	{
		return 0;
	}
	QueuePtr q;
	q = Q.front->next;
	Q.front->next = q->next;
	e = q->data; 
	if(Q.rear == q)
	Q.rear = Q.front;
	free(q);
	q = NULL;
	return 1;
} 

int EnQueue(LinkQueue &Q, QElemType e)	//插入元素 
{
	QueuePtr p;
	p = new QNode;
	if(NULL == p)
	{
		exit(0);
		return 0;
	}
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
	return 1;
}
int QueueTraverse(LinkQueue Q)
{
	if(Q.rear == Q.front)
	{
		printf("队列为空,队内无元素\n"); 
	}
	else
	{
		printf("队列中的元素为:") ; 
		while(Q.front->next)
		{
			printf("%c ",Q.front->next->data);
			Q.front = Q.front->next;	
		}
		printf("\n");
	} 
	
} 
void help(){//使用说明
	cout<<"1~~~~~~~构造一个空队列"<<endl;
	cout<<"2~~~~~~~清空队列"<<endl;
	cout<<"3~~~~~~~销毁队列"<<endl;
	cout<<"4~~~~~~~判断队列长度"<<endl;
	cout<<"5~~~~~~~判断队列是否为空"<<endl;
	cout<<"6~~~~~~~入队"<<endl;
	cout<<"7~~~~~~~得到队首元素"<<endl;
	cout<<"8~~~~~~~出队"<<endl;
	cout<<"9~~~~~~~队列的遍历"<<endl; 
	cout<<"        输入小于等于0的数退出"<<endl;
}
int main()
{
	LinkQueue Q;
	help();
	int operate_code,len;
	char c; 

	while(1)
	{
		printf("请输入操作:  \n");
		cin >> operate_code;
		if(operate_code == 1)
		{
			if( InitQueue(Q) )
			{
				printf("初始化队列成功\n");
			}
			else
			printf("申请内存失败,创建队列失败\n");
		} 
		if(operate_code == 2)
		{
			ClearQueue(Q);
		} 
		if(operate_code == 3)
		{
			 DestrotQueue(Q); 
		} 
		if(operate_code == 4)
		{
			len = QueueLength(Q);
			printf("队列的长度为:%d\n",len);
		} 
		if(operate_code == 5)
		{
			if(QueueEmpty(Q))
				printf("队列为空\n"); 
			else
			printf("队列不为空\n");
			
		} 
		if(operate_code == 6)
		{
			printf("请输入你想插入队的字符:");
			cin >> c;
			if(EnQueue(Q, c))
			printf("插入成功\n");
			else
			printf("插入失败,内存分配失败\n"); 
		} 
		if(operate_code == 7)
		{
			if( GetHead(Q,c) ) 
			printf("队首元素为:%c\n",c);
			else
			printf("队列为空\n"); 
		} 
		if(operate_code == 8)
		{
			if( DeQueue(Q, c) )
			printf("出队成功,队首元素为:%c\n",c);
			else
			printf("这个队列为空\n"); 
		} 
		if(operate_code == 9)
		{
			 QueueTraverse(Q);
		} 
		if(operate_code <= 0)
		{
			break;
		}
	}
	
	
	return 0;
}


顺序队列

 #include <iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
using namespace std;

#define ERROR            0
#define TRUE             1
#define FALSE  			 0
#define OK 				 1
#define STACK_INIT_SIZE 20
#define STACKINCREMENT  10
typedef char QElemType;

typedef struct QNode{
	QElemType data;
	struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue &Q)				//构造一个空队列 
{
	Q.rear = Q.front = new QNode;
	if(NULL == Q.rear)
	{
		exit(0);
		return 0;
	}
	Q.rear->next = NULL;
	return 1;	
} 
int DestrotQueue(LinkQueue &Q)			//销毁队列 
{
	if(Q.front == NULL)
	{
		printf("队列已经销毁\n");
		return 1; 
	}
	 
	while(Q.front)
	{
		Q.rear = Q.front->next;
		free(Q.front);
		Q.front = Q.rear;
	}
	printf("队列已销毁\n");
	return OK;
}
int ClearQueue(LinkQueue &Q)			//清空队列 
{
	if(Q.rear == Q.front)
	{
		printf("队列已空\n"); 
		return 1;
	}
	QueuePtr q, p;
	q = Q.front->next;
	Q.rear = Q.front;
	while(q)
	{
		p = q;
		q = q->next;
		free(p);
	}
	printf("队列已清空\n");	
} 

int QueueEmpty(LinkQueue Q)					//判断队列是否为空 
{
	if(Q.rear == Q.front)
	return 1;
	else 
	return 0;
} 
	
int QueueLength(LinkQueue Q)				//队列的长度 
{
	int i = 0;
	QueuePtr p;
	p = Q.front;
	while(p != Q.rear)
	{
		i++;
		p = p->next;
	}
	return i;
} 
	
int GetHead(LinkQueue Q, QElemType &e)	//得到队头元素 
{
	if(Q.rear == Q.front)
	{
		return 0;
	}
	e = Q.front->next->data;
	return 1;
}
int DeQueue(LinkQueue &Q, QElemType &e)	//删除队头元素 
{
	if(Q.rear == Q.front)
	{
		return 0;
	}
	QueuePtr q;
	q = Q.front->next;
	Q.front->next = q->next;
	e = q->data; 
	if(Q.rear == q)
	Q.rear = Q.front;
	free(q);
	q = NULL;
	return 1;
} 

int EnQueue(LinkQueue &Q, QElemType e)	//插入元素 
{
	QueuePtr p;
	p = new QNode;
	if(NULL == p)
	{
		exit(0);
		return 0;
	}
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
	return 1;
}
int QueueTraverse(LinkQueue Q)
{
	if(Q.rear == Q.front)
	{
		printf("队列为空,队内无元素\n"); 
	}
	else
	{
		printf("队列中的元素为:") ; 
		while(Q.front->next)
		{
			printf("%c ",Q.front->next->data);
			Q.front = Q.front->next;	
		}
		printf("\n");
	} 
	
} 
void help(){//使用说明
	cout<<"1~~~~~~~构造一个空队列"<<endl;
	cout<<"2~~~~~~~清空队列"<<endl;
	cout<<"3~~~~~~~销毁队列"<<endl;
	cout<<"4~~~~~~~判断队列长度"<<endl;
	cout<<"5~~~~~~~判断队列是否为空"<<endl;
	cout<<"6~~~~~~~入队"<<endl;
	cout<<"7~~~~~~~得到队首元素"<<endl;
	cout<<"8~~~~~~~出队"<<endl;
	cout<<"9~~~~~~~队列的遍历"<<endl; 
	cout<<"        输入小于等于0的数退出"<<endl;
}
int main()
{
	LinkQueue Q;
	help();
	int operate_code,len;
	char c; 

	while(1)
	{
		printf("请输入操作:  \n");
		cin >> operate_code;
		if(operate_code == 1)
		{
			if( InitQueue(Q) )
			{
				printf("初始化队列成功\n");
			}
			else
			printf("申请内存失败,创建队列失败\n");
		} 
		if(operate_code == 2)
		{
			ClearQueue(Q);
		} 
		if(operate_code == 3)
		{
			 DestrotQueue(Q); 
		} 
		if(operate_code == 4)
		{
			len = QueueLength(Q);
			printf("队列的长度为:%d\n",len);
		} 
		if(operate_code == 5)
		{
			if(QueueEmpty(Q))
				printf("队列为空\n"); 
			else
			printf("队列不为空\n");
			
		} 
		if(operate_code == 6)
		{
			printf("请输入你想插入队的字符:");
			cin >> c;
			if(EnQueue(Q, c))
			printf("插入成功\n");
			else
			printf("插入失败,内存分配失败\n"); 
		} 
		if(operate_code == 7)
		{
			if( GetHead(Q,c) ) 
			printf("队首元素为:%c\n",c);
			else
			printf("队列为空\n"); 
		} 
		if(operate_code == 8)
		{
			if( DeQueue(Q, c) )
			printf("出队成功,队首元素为:%c\n",c);
			else
			printf("这个队列为空\n"); 
		} 
		if(operate_code == 9)
		{
			 QueueTraverse(Q);
		} 
		if(operate_code <= 0)
		{
			break;
		}
	}
	
	
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值