一.结构的定义
1.包括数据域和指针域
typedef struct QNode
{
ElemType data;
struct QNode* next;
}QNode,*Queue;
2.指针要包括队头指针和队尾指针
typedef struct
{
Queue front;
Queue rear;
}LinkQueue;
二.初始化
1.定义一个指针指向新创建的头结点。
2.将对头队尾指针指向头结点
3.将头结点指针域初始化为空
代码:
status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=new QNode;
Q.front->next=NULL;
return OK;
}
三.入队
1.创建一个新节点并用指针p指向该节点。
2.将数据e辅助给p的data。
3.将结点指针部分初始化为空。(因为先进先出所以采用尾插法)。
4.将前一个结点的指针指向该节点(由于队尾指针指向最后一个节点,所以用Q.real->next来表示)。
5.将队尾指针指向该节点。
代码:
status push(LinkQueue &Q,ElemType e)
{
QNode *p;
p=new QNode;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
四.出队
1.判断对是否为空。
2.定义一个指针指向首元节点(Q.front->next)。
3.将数据赋给e,便于输出。
4.将队头指针向后移动首元结点的后一个节点。
5.需注意的是要判断出队时是否为最后一个节点,若是需将队尾指针指向队首指针(防止队尾指针丢失)
代码:
status pop(LinkQueue &Q,ElemType &e)
{
QNode *p;
if(Q.rear==Q.front) return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
delete p;
return OK;
}
五。取队首元素
1.判断是否队空。
2.取元素。
代码:
status Get(LinkQueue &Q,ElemType &e)
{
if(Q.rear==Q.front) return ERROR;
e=(Q.front->next)->data;
return OK;
}
六.展示
1.判断是否队空。
2.定义一个指针指向首元结点,通过while循环输出p的元素,p不断向下移动。
代码:
status show(LinkQueue &Q,ElemType e)
{
if(Q.rear==Q.front) return ERROR;
QNode *p;
p=Q.front->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
return OK;
}
总代码:
#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
typedef int status;
typedef int ElemType;
typedef struct QNode
{
ElemType data;
struct QNode* next;
}QNode,*Queue;
typedef struct
{
Queue front;
Queue rear;
}LinkQueue;
status InitQueue(LinkQueue &Q);
status push(LinkQueue &Q,ElemType e);
status pop(LinkQueue &Q,ElemType &e);//出队时定义的e一定有&(要修改)
status Get(LinkQueue &Q,ElemType &e);
status show(LinkQueue &Q,ElemType e);
int main()
{
LinkQueue Q;
int choice;
while(1)
{
cout<<"1.初始化 2.入队 3.出队 4.取队头元素 5.显示 "<<endl;
cout<<"选择操作序号:";
cin>>choice;
switch(choice)
{
case 1:
if(InitQueue(Q)==OK)
cout<<"初始化成功"<<endl;
else cout<<"初始化失败"<<endl;
break;
case 2:
ElemType e;
cout<<"插入数据为:";
cin>>e;
if(push(Q,e)==OK)
cout<<"入队成功"<<endl;
else cout<<"入队失败"<<endl;
break;
case 3:
if(pop(Q,e)==ERROR) cout<<"队空"<<endl;
else
cout<<e<<endl;
break;
case 4:
if(Get(Q,e)==ERROR)
cout<<"队空"<<endl;
else cout<<"对头元素为:"<<e<<endl;
break;
case 5:
if(show(Q,e)==ERROR) cout<<"队空"<<endl;
break;
default : cout<<"操作序号错误,请重新输入"<<endl;
}
system("pause");
system("cls");
}
}
status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=new QNode;
Q.front->next=NULL;
return OK;
}
status push(LinkQueue &Q,ElemType e)
{
QNode *p;
p=new QNode;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
status pop(LinkQueue &Q,ElemType &e)
{
QNode *p;
if(Q.rear==Q.front) return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
delete p;
return OK;
}
status Get(LinkQueue &Q,ElemType &e)
{
if(Q.rear==Q.front) return ERROR;
e=(Q.front->next)->data;
return OK;
}
status show(LinkQueue &Q,ElemType e)
{
if(Q.rear==Q.front) return ERROR;
QNode *p;
p=Q.front->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
return OK;
}