C++數據結構鏈隊出隊入隊

鏈隊相關操作

  1. 初始化隊列
  2. 入隊
  3. 出隊
#include "stdafx.h"
#include <iostream>
using namespace std;

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int ELEMTYPE;
typedef int Status;

typedef struct LNode  //結點結構 單鏈表
{
	ELEMTYPE data;
	struct LNode *next;
}LNode,*Queueptr;

typedef struct     //隊列的結構
{
	Queueptr front; //隊頭指針
	Queueptr rear;  //隊尾指針
}LinkQueue;

Status initLinkQueue(LinkQueue &Q)
{
	Q.front = Q.rear = new LNode;  //帶頭結點 初始化都指向頭結點
	if(!Q.front) return OVERFLOW;
	Q.front->next = NULL;
}

bool isEmpty(LinkQueue &Q) //判隊空
{
	if (Q.front->next == NULL)
		return true;
	else
		return false;
}
void EnQueue(LinkQueue &Q,ELEMTYPE e) //入隊
{
	LNode *p = (LNode *)malloc(sizeof(LNode));
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
}

void ExQueue(LinkQueue &Q,ELEMTYPE &e) //出隊
{
	if(isEmpty(Q))
		return;
	LNode *p = Q.front->next;
	e = p->data;
	Q.front->next = Q.front->next->next;
	delete (p);
	if (isEmpty(Q)) //若空表 尾指針置初位
		Q.rear = Q.front;
}

void Visit(LinkQueue &Q)
{
	LNode *p = Q.front->next;
	while(p)
	{
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章