/************************
author's email:[email protected]
date:2018.2.7
链队
************************/
#include <iostream>
using namespace std;
#define maxSize 6
typedef struct QNode
{
int data;//数据域
struct QNode *next;//指针域
}QNode;//队结点类型定义
typedef struct
{
QNode *front;//队头指针
QNode *rear;//队尾指针
}LiQueue;//链队类型定义
void initQueue(LiQueue *&lqu)//初始化队列
{
lqu = (LiQueue*)malloc(sizeof(LiQueue));
lqu->front = lqu->rear = NULL;
}
int isQueueEmpty(LiQueue *lqu)//判断对空
{
if (lqu->rear == NULL || lqu->front == NULL)
return 1;
else
return 0;
}
void enQueue(LiQueue *lqu, int x)
{
QNode *p;
p = (QNode*)malloc(sizeof(QNode));
p->data = x;
p->next = NULL;
if (lqu->front == NULL || lqu->rear == NULL)//若队列为空,则新结点是队首结点也是队尾结点
lqu->front = lqu->rear = p;
else
{
lqu->rear->next = p;//将p入队
lqu->rear = p;//队尾指针指向队尾结点
}
}
int deQueue(LiQueue *lqu, int &x)
{
QNode *p;
if (lqu->rear == NULL || lqu->front == NULL)//队空不能出队
return 0;
else
p = lqu->front;
if (lqu->front == lqu->rear)//队列中只有一个结点是出队操作
lqu->front = lqu->rear = NULL;
else
lqu->front = lqu->front->next;
x = p->data;
free(p);
return 1;
}
int main()
{
LiQueue *lqu;
initQueue(lqu);
int exp[maxSize] = { 1,54,46,78,12,63 };
int i = 0;
int x;
while (i < maxSize)
{
enQueue(lqu, exp[i]);
++i;
deQueue(lqu, x);
cout << x << " ";
}
cout << endl;
system("pause");
return 0;
}
数据结构——链式队列
猜你喜欢
转载自blog.csdn.net/wardseptember/article/details/79282448
今日推荐
周排行