1. 程式人生 > >佇列:出隊、入隊、銷燬、清空等

佇列:出隊、入隊、銷燬、清空等

具體的佇列演算法實現程式碼。

將N個隨機數入隊。

實現出隊、再次入隊、列印佇列等方法。


記憶體洩露是個大問題。

記得free(p);p=NULL

p指標本身是在棧上儲存,不過p所指向的空間在堆上,所以需要程式設計師自行釋放,防止記憶體洩露。

free(p);之後p指標仍然存在,如果堆上原來指向的記憶體沒有被重寫,p還是可以的輸出的,以防p所指向的記憶體重寫,p相當於野指標,需要p=NULL;

原始碼如下:



#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
typedef int QElemType;
typedef int Status;
typedef struct QNode{
	QElemType data;
	struct QNode *next;
};//定義節點
typedef struct{
	 struct QNode * rear;//尾指標
	 struct QNode * front;//頭指標
	 int Length;
}LinkQueue;//定義佇列
Status InitQueue(LinkQueue &Q);//初始化佇列
Status EnQueue(LinkQueue &Q,QElemType e);//入隊在隊尾插入元素e
Status ShowQueue(LinkQueue Q);//列印當前佇列元素
Status GetTop(LinkQueue Q,QElemType &e);//獲取當前隊頭元素,並用e返回
Status DeQueue(LinkQueue &Q);//出隊
Status IsEmptyQueue(LinkQueue Q);//判斷是否為空,隊空返回1。
Status ClearQueue(LinkQueue &Q);//清空佇列
Status DestroyQueue(LinkQueue &Q);//銷燬佇列

Status InitQueue(LinkQueue &Q){//傳地址,修改Q	
	Q.rear=Q.front=(QNode *)malloc(sizeof(QNode));//指向頭結點
	if(!Q.rear)
		cout<<"Error Overflow!"<<endl;
	Q.front->next=NULL;
	Q.Length=0;
	return 0;
}
Status EnQueue(LinkQueue &Q,QElemType e){
	//在隊尾插入新的節點,需要開闢空間
	QNode *p=(QNode *)malloc(sizeof(QNode));
	if(!p) cout<<"分配記憶體失敗"<<endl;
	p->data=e;
	p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	if(Q.front->next==NULL)
		Q.front->next=p;
	Q.Length++;
	return 0;
}
Status ShowQueue(LinkQueue Q){
	QNode *p;
	p=Q.front->next;//p指向第一個元素
	if(Q.front==Q.rear)
	{
		cout<<endl<<"當前佇列為空!沒有元素可以顯示"<<endl;
		return 0;
	}
		
	cout<<endl<<"當前佇列共計 "<<Q.Length<<" 個元素"<<endl;
	cout<<"------------------------------當前佇列元素為:-------------------"<<endl;
	while(p!=Q.rear)
	{
	cout<<p->data<<" ";
	p=p->next;//不能用p++
	}
	cout<<p->data<<endl;
	return 0;
}
Status GetTop(LinkQueue Q,QElemType &e){
	e=Q.front->next->data;
	return OK;
}
Status DeQueue(LinkQueue &Q){
	if(Q.front==Q.rear)
		cout<<endl<<"佇列為空,沒有元素可以刪除!"<<endl;
	else 
	{
		Q.front->next=Q.front->next->next;
		Q.Length--;
	}	
	return OK;
}
Status IsEmptyQueue(LinkQueue Q){
	if(Q.front==Q.rear)
		return 1;
	else
		return 0;
}
Status DestroyQueue(LinkQueue &Q){
	 if(Q.front!= NULL) 
	 { 
		 Q.rear=Q.front->next;
		 free(Q.front);
		 Q.front=Q.rear; //釋放一塊記憶體要做兩點:1.釋放指向它的指標。2.將該指標指向空
	 }		
    return OK;
}
Status ClearQueue(LinkQueue &Q)  
{ // 將Q清為空佇列   
  struct QNode *p,*q;  
  Q.rear=Q.front;  
  p=Q.front->next;  
  Q.front->next=NULL;//只留下頭結點   
  while(p)  
  {  
    q=p;  
    p=p->next;  
    free(q);  
  }  
  return OK;
} 

int main(int argc,char *argv[]){
	LinkQueue Q;//結構體Q	
	InitQueue(Q);
	QElemType e;
	e=rand()%100;
	int n;//隨機插入佇列元素的數目。
	cout<<"請輸入佇列隨機元素數目:"<<endl;
	cin>>n;
	while(n--)
	{
		e=rand()%100;
		EnQueue(Q,e);//進隊n個元素	
	}
	
	GetTop(Q,e);
	cout<<"當前隊頭元素為:"<<e;
	ShowQueue(Q);
	
	DeQueue(Q);
	cout<<endl<<"-----------------隊頭元素出隊後:------------------------"<<endl;
	GetTop(Q,e);
	cout<<"當前隊頭元素為:  "<<e;
	ShowQueue(Q);
	if(!IsEmptyQueue(Q))
		cout<<"佇列非空"<<endl;

	e=rand()%50;
	cout<<endl<<"入隊的元素為"<<e<<endl;
	EnQueue(Q,e);//再次入隊	
	GetTop(Q,e);
	cout<<"當前隊頭元素為:"<<e;
	ShowQueue(Q);
	
	//清空
	cout<<endl<<"-----------清空佇列--------";
	DestroyQueue(Q);
	ShowQueue(Q);

	return 0;
}

實現結果截圖: