首页 > 其他 > 详细

线性队列

时间:2015-01-07 12:34:47      阅读:139      评论:0      收藏:0      [点我收藏+]
#include "stdafx.h"

#include <iostream>
using namespace std;

typedef int DataType;
#define SIZE 100

typedef struct {
	DataType data[SIZE];
	int head,tail;
}SqQueue;

//初始化
SqQueue init(SqQueue *Q)//顺序型数据结构,初始化之类的方法一定要有返回值
{
	Q = (SqQueue*)malloc(sizeof(SqQueue));
	if(Q==NULL) 
	{
		printf("%s","init failed");
		exit(0);
	}
	else
	{
		Q->data[0]=0;
		Q->head = 0;
		Q->tail = 0;
	}
	return *Q;
}
//判断是否为空
int isQempty(SqQueue *Q)
{
	if(Q->head == Q->tail) return 1;
	else return 0;
}
//判断是否满
int isQfull(SqQueue *Q)
{
	if(Q->tail == SIZE) return 1;
	else return 0;
}
//进
void inQueue(SqQueue *Q,DataType e)
{
	if(isQfull(Q) ==1) 
	{
		cout<<"full"<<endl;
			exit(0);
	}else
	{
		cout<<"q->tail"<<Q->tail<<endl;
		Q->data[Q->tail++] = e;//
		cout<<"not full"<<endl;
	}
	
}
//出
void outQueue(SqQueue *Q)
{
	if(isQempty(Q) == 1)
	{
		cout<<"out queue empty"<<endl;
		exit(0);
	}
	else
	{
		cout<<"out:"<<Q->data[Q->head++]<<endl;
	}
}



/*void main()
{
	SqQueue S;
	S=init(&S);
	cout<<"isempty"<<isQempty(&S)<<endl;
	inQueue(&S,1);
	cout<<"isempty"<<isQempty(&S)<<endl;
	inQueue(&S,2);
	cout<<"isempty"<<isQempty(&S)<<endl;
	outQueue(&S);
	
}*/

  

线性队列

原文:http://www.cnblogs.com/waiwai4701/p/4207937.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!