您的位置:首页 > 其它

队列的顺序和链式存储

2018-03-11 18:01 441 查看
非循环顺序存储:
#include "stdafx.h"
#include "malloc.h"
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef int DataType;
typedef struct
{
DataType elem[MAXSIZE];
int front;  //队头
int rear;	//队尾

}Queue;

void initQueue(Queue & Q)
{
Q.rear = 0;
Q.front = 0;
}

bool isEmpty(Queue &Q)
{
if (Q.rear == Q.front)
{
return true;
}
else
return false;

}

bool isFull(Queue &Q)
{
if (Q.rear == MAXSIZE - 1)
return true;
else
return false;
}

void inQueue(Queue &Q, DataType elem)
{
if (!isFull)
{
Q.elem[Q.front] = elem;
Q.front++;
}
else
cout << "Full Queue" << endl;
}

DataType OutQueue(Queue &Q)
{
if (!isEmpty)
{
return Q.elem[Q.rear];
Q.front++;

}
else
cout << "Empty Queue" << endl;

}


顺环队列如何解决front==near无法判断空还是满的情况?
1.添加标签tag
2.空出一个结点不填

#include "stdafx.h"
#include "malloc.h"
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef int DataType;
typedef struct SqQueue
{
DataType queue[MAXSIZE];
int front; //队头
int rear; //队尾

}SqQueue;

void initSqQueue(SqQueue & Q)
{
Q.rear = 0;
Q.front = 0;
}

void inSqQueue(SqQueue *Q, int x)
{
if ((Q->rear + 1) % MAXSIZE == Q->front)
{
cout<<"队列已经满了,不可以入队"<<endl;
exit(1);
}

Q->queue[Q->rear] = x;
Q->rear = (Q->rear + 1) % MAXSIZE; //例如当MAXSIZE=6的时候,rear为5,要使得rear下一个指针为0

}

int outSqQueue(SqQueue *Q)
{
if (Q->front == Q->rear)
{
cout << "队列已经空了,无元素可以出队" << endl;
return 0;
}
int x = Q->queue[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return x;
}

#include "stdafx.h"
#include "malloc.h"
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef char DataType;
typedef struct Node //链表
{
DataType elem;
Node * next;

}ListNode;
typedef ListNode * QNode;
typedef struct //队列
{
ListNode *front; //front做删除
ListNode *rear; //rear做插入

}QueueNode;
typedef QueueNode *Queue;
Queue PtrQ;

//无头结点的队列链式存储方式
//PtrQ->front即为存储第一个元素的结点
//若为有头结点,则PtrQ->front为头结点,PtrQ->front->next为存储第一个元素的结点
DataType DeleteQ(Queue)
{
struct Node *FrontCell;
DataType FrontElem;

if (PtrQ->front == NULL)
{
cout << "Empty Queue" << endl;
return NULL;
}
FrontCell = PtrQ->front;
if (PtrQ->front == PtrQ->rear)
{
PtrQ->front = PtrQ->rear = NULL;
}
else
PtrQ->front = PtrQ->front->next;
FrontElem = FrontCell->elem;
free(FrontCell);
return FrontElem;
}

void Init_Queue(Queue &Q)
{
Q = (Queue)malloc(sizeof(QueueNode));
Q->rear = (QNode)malloc(sizeof(QNode));
Q->front= (QNode)malloc(sizeof(QNode));
Q->rear->next = Q->front;
Q->front->next = NULL;
}

//有头结点
bool isEmpty(Queue Q)
{
if (Q->front->next == NULL)
return true;
else
return false;

}

//入队
void InQueue(Queue &Q,DataType value)
{
QNode NewNode = (QNode)malloc(sizeof(QNode));
NewNode->elem = value;
NewNode->next = NULL;
Q->rear->next->next = NewNode;
Q->rear->next = NewNode;

}

DataType getTop(Queue Q)
{

if (!isEmpty(Q))
{
return Q->front->next->elem;
}

else
return NULL;
}

DataType OutQueue(Queue &Q)
{
if (!isEmpty(Q))
{
DataType ch = getTop(Q);
QNode q1 = Q->front->next;//头结点
Q->front->next = q1->next;//改变头结点指针域
if (Q->front->next == Q->rear->next)
Q->rear->next = Q->front->next;
return ch;
}
else
cout << "队列已空" << endl;

}

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