考研学习—数据结构—链队
作者:
sshixx
,
2022-08-01 10:19:49
,
所有人可见
,
阅读 209
链队的基本操作
有问题欢迎大家指出
#include <iostream>
using namespace std;
//定义结构体
typedef struct QNode
{
int data;
struct QNode *next;
QNode(): next(NULL) {}
QNode(int _data): data(_data), next(NULL) {}
}QNode;
QNode *front, *rear; //直接定义成全变量使用
//初始化
void InitQueue(QNode *q)
{
front = rear = q; //定义队头和队尾指向该队列
}
//判空
bool QEmpty()
{
if(front == rear) return true;
return false;
}
//入队
void EnQueue(int e)
{
QNode *p = new QNode(e);
rear->next = p;
rear = p;
}
//出队
void DeQueue(int &e)
{
if(!QEmpty())
{
QNode *p = front->next;
e = p->data;
front = p;
delete(p);
}
}
//输出
void Output(QNode *head)
{
QNode *p = head->next;
while(p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main()
{
QNode *q = new QNode(-1); //带结点
InitQueue(q); //定义初始化队头队尾
if(QEmpty()) cout << 1 << endl;
else cout << 0 << endl;
Output(front);
EnQueue(0);EnQueue(1);EnQueue(2);EnQueue(3);EnQueue(4);EnQueue(5);
Output(front);
if(QEmpty()) cout << 1 << endl;
else cout << 0 << endl;
int a;
DeQueue(a);
DeQueue(a);
cout << a << "======" << endl;
Output(front);
return 0;
}