【數據結構】順序隊列

隊列,一種受限制的線性結構。它有一個隊首和一個隊尾,且只能在隊尾接收新的元素,在隊首輸出一個新的元素。就是排隊購物一樣的,新來的只能呆在後面,等隊伍的第一個人走了之後,第二個人就成了新的隊首。
隊列這種結構也只能進行隊首的刪除,隊尾的添加,判斷是否爲空這三種操作。
下面是具體實現。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
using namespace std;

typedef  int ElemType;
typedef struct LNode{
	ElemType data[MaxSize];
	int front,rear;
}SqQueue;

void InitQueue(SqQueue &Q);				//構造一個隊列 
bool QueEmpty(SqQueue Q);				//判斷隊列是否爲空 
void EnQueue(SqQueue &Q,ElemType x);	//入隊,在隊尾插入一個元素 
bool DeQueue(SqQueue &Q,ElemType &x);	//出隊,在隊首刪除一個元素 


int main()
{
	int n,num;
	SqQueue Q;
	InitQueue(Q);
	cin >> n;
	while(n--){
		cin >> num;
		EnQueue(Q,num);
	}
	for(int i=0;i<5;i++)
		cout << Q.data[i] << " "; 
	return 0;
}
void InitQueue(SqQueue &Q){			//構造一個隊列 
	Q.front=Q.rear=0;
	return ; 
}

bool QueEmpty(SqQueue Q){			//判斷隊列是否爲空 
	if(Q.front==Q.rear)
		return true;
	else
		return false;
}

void EnQueue(SqQueue &Q,ElemType x){	//入隊,隊尾添加一個元素 
	Q.data[Q.rear]=x;
	Q.rear++;
	return ;
}

bool DeQueue(SqQueue &Q,ElemType &x){	//出隊,刪除隊首元素 
	if(Q.front==Q.rear)
		return false;
	x=Q.data[Q.front];
	Q.front++;
	return true;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章