1、队列特性
只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出特性。
入队列:进行插入操作的一端称为
队尾
出队列:进行删除操作的一端称为
队头
* 写一个hpp文件,将所有要实现的函数全部声明。
//队列的声明
#pragma once
typedef int QDataType;
typedef struct QNode{
struct QNode* next;
QDataType val;
}QNode;
typedef struct Queue{
struct QNode* head;
struct QNode* rear;
QDataType _size;//记录有效元素的个数
}Queue;
//队列初始化
void QueueInit(Queue* q);
//队列入
void QueuePush(Queue* q, QDataType val);
//队列出
void QueuePop(Queue* q);
//有效元素的个数
int QueueSize(Queue* q);
//判断队列是否为空
int QueueEmpty(Queue* q);
//获取对头元素
QDataType QueueFront(Queue* q);
//获取队尾元素
QDataType QueueBack(Queue* q);
//测试代码
void QueueTest();
* 对上面函数的实现。
//队列的实现
#include "Queue.h"
#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>
QNode* BuyNode(int val)
{
QNode* newNode = (QNode*)malloc(sizeof(QNode));
if (NULL == newNode)
{
assert(0);
return NULL;
}
newNode->next = NULL;
newNode->val = val;
return newNode;
}
//队列初始化
void QueueInit(Queue* q)
{
assert(q);
q->head = BuyNode(0);
q->rear = q->head;
q->_size = 0;
}
//队列入
void QueuePush(Queue* q, QDataType val)
{
assert(q);
QNode* newNode = BuyNode(val);
q->rear->next = newNode;
q->rear = newNode;
q->_size++;
}
//判断队列是否为空
int QueueEmpty(Queue* q)
{
assert(q);
//if (q->head >= q->rear)
//{
// return true;
//}
//return false
return 0 == q->_size;
}
//队列出
void QueuePop(Queue* q)
{
QNode* pdelNode = NULL;
if (QueueEmpty(q))
return;
pdelNode = q->head->next;
q->head->next = pdelNode->next;
free(pdelNode);
q->_size--;
//如果此时队列为空
if (q->_size == 0)
q->rear = q->head;
}
//有效元素的个数
int QueueSize(Queue* q)
{
assert(q);
return q->_size;
}
//获取队头元素
QDataType QueueFront(Queue* q)
{
assert(!QueueEmpty(q));
return q->head->next->val;
}
//获取队尾元素
QDataType QueueBack(Queue* q)
{
assert(q);
return q->rear->val;
}
//测试代码
void QueueTest()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
QueuePush(&q, 5);
QueuePush(&q, 6);
printf("Size:%d\n", QueueSize(&q));
printf("Front:%d\n", QueueFront(&q));
printf("Back:%d\n", QueueBack(&q));
QueuePop(&q);
QueuePop(&q);
QueuePop(&q);
printf("Size:%d\n", QueueSize(&q));
printf("Front:%d\n", QueueFront(&q));
printf("Back:%d\n", QueueBack(&q));
}
int main()
{
QueueTest();
system("pause");
return 0;
}
// 顺序表实现
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <assert.h>
#include <stdio.h>
template<class T>
class Queue
{
public:
Queue(int size = 10)
: front(0)
, rear(0)
, capacity(size)
{
array = new T[size];
assert(array != NULL);
}
Queue(const Queue<T>& q)
: front(q.front)
, rear(q.rear)
, capacity(q.capacity)
{
array = new T[q.capacity];
for (int i = 0; i < capacity; ++i){
array[i] = q.array[i];
}
}
Queue<T>& operator=(const Queue<T>& q)
{
if (this != &q)
{
delete[] array;
front = q.front;
rear = q.rear;
capacity = q.capacity;
array = new T[q.capacity];
for (int i = 0; i < capacity; ++i){
array[i] = q.array[i];
}
}
return *this;
}
~Queue()
{
delete[] array;
array = nullptr;
front = rear = capacity = 0;
}
int Size()
{
return (rear - front + capacity) % capacity;
}
void Check()
{
if (!isFull()){
return;
}
int size = Size();
T* temp = new T[size * 2 + 1];
for (int i = 0; i < capacity; ++i){
temp[i] = array[i];
}
delete[]array;
array = temp;
capacity = size * 2 + 1;
}
void Push(T data)
{
Check();
array[rear] = data;
rear = (rear + 1) % capacity;
}
void Pop()
{
if (!isEmpty()){
front = (front + 1) % capacity;
}
else {
cout << "队列为空" << endl;
}
}
bool isFull()
{
return (rear + 1) % capacity == front;
}
bool isEmpty()
{
return front == rear;
}
T Front()
{
return array[front];
}
T Back()
{
return array[(rear - 1 + capacity ) % capacity];
}
void Print()
{
for (int i = 0; i < Size(); ++i){
cout << array[(i + front) % capacity] << " ";
}
cout << endl;
}
private:
int front;
int rear;
int capacity;
T* array;
};
#endif
// 链表实现
#ifndef __QUEUE_H__
#define __QUEUE_H__
template<class T>
struct QueueNode
{
struct QueueNode<T>* next;
T val;
QueueNode(T data = T())
: next(nullptr)
, val(data)
{}
};
template<class T>
class Queue
{
typedef QueueNode<T> Node;
public:
Queue()
{
head = new Node;
tail = head;
_size = 0;
}
~Queue()
{
Clear();
}
void QueuePush(T data)
{
Node* t = new Node(data);
tail->next = t;
tail = t;
++_size;
}
void QueuePop()
{
if (!isEmpty()){
Node* del = head;
head = head->next;
delete del;
del = nullptr;
}
if (_size == 0){
head = tail;
}
}
int Size()
{
return _size;
}
bool isEmpty()
{
return _size == 0;
}
void Clear()
{
Node* cur = head;
while (cur != nullptr)
{
Node* temp = cur;
cur = cur->next;
delete temp;
}
head = tail = nullptr;
_size = 0;
}
T front()
{
return head->next->val;
}
T back()
{
return tail->val;
}
void Print()
{
Node* cur = head->next;
while (cur != tail)
{
cout << cur->val << "->";
cur = cur->next;
}
cout << cur->val << endl;
}
private:
Node* head;
Node* tail;
int _size;
};
#endif