您的位置:首页 > 理论基础 > 数据结构算法

数据结构之链队列

2015-10-10 13:03 453 查看
#include <iostream>

#include <stdio.h>

#include <cstdlib>

#include <string>

using namespace std;

#define OK 1

#define ERROR 0

#define OVERFLOW -2

typedef struct QNode //队列结点定义

{
int date;
struct QNode *next;

}QNode, *QueuePtr;

typedef struct  //队列结构定义

{
QueuePtr front;
QueuePtr rear;

}LinkQueue;

int InitQueue(LinkQueue &Q)//初始化

{
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q.front) exit(OVERFLOW);
Q.front->next = NULL;
return OK;

}

void CreateQueue(LinkQueue &Q, int N)//创建队列

{
int i;
QNode *p;
p = (QNode*)malloc(sizeof(QNode));
p = Q.front->next;
for (i = 1; i <= N; i++)
{
p = (QNode*)malloc(sizeof(QNode));
cin >> p->date;
p->next = (QNode*)malloc(sizeof(QNode));
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
p = p->next;
}

}

int EnQueue(LinkQueue &Q, int E)//插入元素

{
QNode *p;
p = (QNode*)malloc(sizeof(QNode));
if (!p) exit(OVERFLOW);
p->date = E;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;

}

int DeQueue(LinkQueue &Q, int E)//删除元素

{
QNode *p;
p = (QNode*)malloc(sizeof(QNode));
if (Q.rear == Q.front) return ERROR;
p = Q.front->next;
E = p->date;
Q.front->next = p->next;
if (Q.rear == p)  Q.rear = Q.front;
free(p);
return OK;

}

void DestoryQueue(LinkQueue &Q)//销毁队列

{
while (Q.front){
Q.rear = Q.front->next;
free(Q.front);
Q.front = Q.rear;
}

}

void display(LinkQueue &Q)//显示

{
QNode *p;
p = Q.front->next;
while (p != NULL)
{
cout << p->date << " ";
p = p->next;
}

}

int LinkQueue_menu()//功能菜单

{

int n;

do
{

cout << "——————链队列的相关操作——————" << endl;
cout << "请输入要操作的选项:" << endl;
cout << "*******************************************" << endl;
cout << "*****(1)队列初始化******(2)创建队列******" << endl;
cout << "*****(3)入队*************(4)出队***********" << endl;
cout << "*******************************************" << endl;
cout << "*****(5)销毁队列*********(6)显示***********" << endl;
cout << "*******************************************" << endl;
cout << "*******************(7)退出*****************" << endl;
cout << "*******************************************" << endl;
cin >> n;
} while (n<1 || n>7);

return n;

}

int main()

{
LinkQueue Q;
for (;;)
{
switch (LinkQueue_menu())
{
case 1:
system("cls");
InitQueue(Q);
system("pause");
system("cls");
break;
case 2:
system("cls");
int M;
InitQueue(Q);
cout << "请输入要创建队列的元素个数:" << endl;
cin >> M;
cout << "请输入队列元素:" << endl;
CreateQueue(Q, M);
system("pause");
system("cls");
break;
case 3:
system("cls");
int e;
cout << "请输入要插入到队列的元素:" << endl;
cin >> e;
EnQueue(Q, e);
system("pause");
system("cls");
break;
case 4:
system("cls");
int a;
cout << "请输入要删除的队首元素:" << endl;
cin >> a;
DeQueue(Q, a);
system("pause");
system("cls");
break;
case 5:
system("cls");
DestoryQueue(Q);
system("pause");
system("cls");
break;
case 6:
system("cls");
cout << "队列中的元素如下:" << endl;
display(Q);
system("pause");
system("cls");
case 7:
system("cls");
system("pause");
}

}
return OK;
system("pause");
system("cls");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: