队列的顺序和链式存储
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;
}
相关文章推荐
- 栈和队列的顺序存储和链式存储
- 队列的顺序存储和链式存储
- 队列的顺序存储与链式存储
- 数据结构-队列-顺序链式存储
- 队列——顺序存储与链式存储
- 队列的顺序存储和链式表示方法-数据结构学习笔记2.3
- 队列的链式存储与顺序存储
- 数据结构—队列的顺序和链式存储
- 队列的顺序存储实现和链式存储实现
- 队列的顺序存储和链式存储实现
- Java实现三种(顺序,链式,循环)队列
- 第二章 栈和队列(1)——顺序存储
- 队列(顺序存储)代码
- 队列的顺序存储结构和链式存储结构
- 004 队列的顺序存储
- 数据结构学习之循环队列(顺序存储)
- 数据结构学习之队列(链式存储)
- 数据结构-栈的顺序存储和链式存储
- C++学习总结——顺序存储,链式存储,索引存储,哈希存储的优缺点,以及vector数组,list链表,十字链表,索引结构,hash表的实现
- 线性结构的顺序存储和链式存储的实现代码(二)