数据结构-队列
2017-09-15 18:59
260 查看
#include <iostream>
#include <string>
using namespace std;
template<class T>
class queue
{
public:
virtual ~queue() {}
virtual bool empty() const = 0;
//判空
virtual int size() const = 0;
//长度
virtual T& front() = 0;
//表头元素
virtual T& back() = 0;
//表位元素
virtual void pop() = 0;
//出队
virtual void push(const T& theElement) = 0;
//入队
};
template<class T>
class arrayQueue : public queue<T>
{
public:
arrayQueue(int initialCapacity = 10);
~arrayQueue() { delete[] queue; }
bool empty() const { return theFront == theBack; }
int size() const
{
return (theBack - theFront + arrayLength) % arrayLength;
}
T& front()
{
if (theFront == theBack)
{
cout << "队列为空" << endl;
exit(0);
}
return queue[(theFront + 1) % arrayLength];
}
T& back()
{
if (theFront == theBack)
{
cout << "队列为空" << endl;
exit(0);
}
return queue[theBack];
}
void pop()
{
if (theFront == theBack)
{
cout << "队列为空" << endl;
}
theFront = (theFront + 1) % arrayLength;
queue[theFront].~T();
}
void push(const T& theElement);
private:
int theFront;
int theBack;
int arrayLength;
T *queue;
};
template<class T>
arrayQueue<T>::arrayQueue(int initialCapacity)
{
if (initialCapacity < 1)
{
exit(0);
}
arrayLength = initialCapacity;
queue = new T[arrayLength];
theFront = 0;
theBack = 0;
}
//难点
template<class T>
void arrayQueue<T>::push(const T& theElement)
{
if ((theBack + 1) % arrayLength == theFront)
{
T* newQueue = new T[2 * arrayLength];
int start = (theFront + 1) % arrayLength;
if (start < 2)
copy(queue + start, queue + start + arrayLength - 1, newQueue);
else
{
copy(queue + start, queue + arrayLength, newQueue);
copy(queue, queue + theBack + 1, newQueue + arrayLength - start);
}
theFront = 2 * arrayLength - 1;
theBack = arrayLength - 2;
arrayLength *= 2;
queue = newQueue;
}
theBack = (theBack + 1) % arrayLength;
queue[theBack] = theElement;
}
int main(void)
{
arrayQueue<int> q(4);
q.push(1);
cout << "队尾" << q.back() << endl;
q.push(2);
cout << "队尾 " << q.back() << endl;
q.push(3);
cout << "队尾 " << q.back() << endl;
q.push(4);
cout << "队尾" << q.back() << endl;
cout << "应该是1234,从Front--->Rear" << endl;
if (q.empty())
cout << "队列为空" << endl;
else
cout << "队列不为空" << endl;
cout << "队列长度为" << q.size() << endl;
while (!q.empty())
{
cout << "队头元素" << endl;
cout << "队头 " << q.front() << endl;
q.pop();
}
q.pop();
cout << "最后出队失败 " << endl;
arrayQueue<int> r(4);
r.push(1);
r.push(2);
r.push(3);
r.pop();
r.pop();
r.push(4);
r.push(5);
r.push(6);
r.push(7);
cout << "应该是4567,从Front--->Rear" << endl;
if (r.empty())
cout << "队为空" << endl;
else
cout << "队不为空" << endl;
cout << "队大小" << r.size() << endl;
while (!r.empty())
{
cout << "队头元素" << endl;
cout << "队头 " << r.front() << endl;
r.pop();
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
template <class T>
struct chainNode
{
T element;
chainNode<T> *next;
chainNode(){};
//结点初始化
chainNode(const T& element)
{
this->element = element;
}
//实现队列的FIFO ,尾部插入
chainNode(const T& element, chainNode<T>* next)
{
this->element = element;
this->next = next;
}
};
//ADT过程
template <class T>
class queue
{
public:
virtual ~queue(){};
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& front() = 0;
virtual T& back() = 0;
virtual void pop() = 0;
virtual void push(const T& theElement) = 0;
//在派生类中必须实现
//一个项目分为多个模块
};
template <class T>
class linkedQueue :public queue<T>
{
public:
//构造函数+析构函数
linkedQueue(int initialCapacity = 10)
{
//初始化基本数据成员
queueFront = NULL;
queueSize = 0;
}
~linkedQueue();
bool empty() const
{
return queueSize == 0;
}
int size() const
{
return queueSize;
}
T& front()
{
if (queueSize == 0)
{
cout << "队为空" << endl;
exit(0);
}
return queueFront->element;
}
T& back()
{
if (queueSize == 0)
{
cout << "队为空" << endl;
exit(0);
}
return queueBack->element;
}
void pop();
void push(const T& theElement);
private:
chainNode<T>* queueFront; //指向队头的指针
chainNode<T>* queueBack; //指向队尾的队尾
int queueSize; //队容量
};
template <class T>
linkedQueue<T>::~linkedQueue()
{
while (queueFront != NULL)
{
//删除头结点 ,保护他下一个结点
chainNode<T>* nextNode = queueFront->next;
delete queueFront;
queueFront = nextNode;
}
}
template <class T>
void linkedQueue<T>::push(const T& theElement)
{
//对于基本的数据类型你可以直接new ,自定义类型要用构造函数
chainNode<T>* newNode = new chainNode<T>(theElement, NULL);
//队尾增加结点
//队为空
if (queueSize == 0)
queueFront = newNode;
else
queueBack->next = newNode; //队不为空,队尾插入
queueBack = newNode;
queueSize++;
}
template <class T>
void linkedQueue<T>::pop()
{
if (queueFront == NULL)
{
cout << "队为空" << endl;
exit(0);
}
chainNode<T>* nextNode = queueFront->next;
delete queueFront;
queueFront = nextNode;
queueSize--;
}
int main()
{
linkedQueue<string> myQueue;
myQueue.push("I");
myQueue.push(" am");
myQueue.push(" a");
myQueue.push(" student");
cout << "Front---->rear:I am a student" << endl;
if (myQueue.empty())
cout << "队为空" << endl;
else
{
while (!myQueue.empty())
{
cout << myQueue.front();
myQueue.pop();
}
//队空
cout << "队空后:" << endl;
myQueue.pop();
}
return 0;
}
//priotity.h:
#include <iostream> #define MaxQueueSize 100 //队列的大小 //数据类型 数据本身 优先权 typedef int ElementType; //定义数据类型地 别名 //数据 结构体 typedef struct { int priority; //优先权---工作量 ElementType element;//队列中的元素 }DataType; //队列的结构体 typedef struct { int size; //结构体的嵌套 DataType Queue[MaxQueueSize]; }PriQueue; //读取文件作业---调度读取 //队列操作 //初始化---初始化基本成员 //size Queue void InitQueue(PriQueue *Q) { Q->size = 0; } //判断队列是否为空 NULL //和它的初始化比较 int QueueEmpty(PriQueue *Q) { // -> 指向运算符 C:结构体指针 C++: 类和结构体 // :: 作用域限定符 if (Q->size <= 0) return 0; //标志 else return 1; //不为空 // return 0 函数结束 } //入队 文件操作 int Push(PriQueue *Q, DataType data) { //入队前判断是否满 if (Q->size >= MaxQueueSize) { std::cout << "杯子已满,无法再倒水" << std::endl; return 0; } else { //Q->size=0; Q->Queue[Q->size] = data; //入队了队列长度就要增长 Q->size++; return 1; } } //出队 int Pop(PriQueue *Q, DataType *A) { DataType min; //最小的开始,排序 int minIndex = 0; //短作业优先法 //喝水,先判断杯子有没有水 if (Q->size <= 0) { std::cout << "为空,无法出队" << std::endl; return 0; } else { //假设第一个作业是最短的 min = Q->Queue[0]; //1 for (int i = 1; i < Q->size; i++) { //多了一个数据项 按照权值排序 //0 if (Q->Queue[i].priority < min.priority) { min = Q->Queue[i]; minIndex = i; //出队依据就是最小作业序号 } } *A = Q->Queue[minIndex]; //难点 //删除完数组后,后面的元素往前移动 for (int i = minIndex + 1; i < Q->size; i++) { Q->Queue[i - 1] = Q->Queue[i]; minIndex = i; } Q->size--; return 1; } } //获取队头元素 int GetQueue(PriQueue *Q, DataType *A) { DataType min; //最小的开始,排序 int minIndex = 0; //短作业优先法 //喝水,先判断杯子有没有水 if (Q->size <= 0) { std::cout << "为空,无法出队" << std::endl; return 0; } else { //假设第一个作业是最短的 min = Q->Queue[0]; //1 for (int i = 1; i < Q->size; i++) { //多了一个数据项 按照权值排序 //0 if (Q->Queue[i].priority < min.priority) { min = Q->Queue[i]; minIndex = i; //出队依据就是最小作业序号 } } *A = Q->Queue[minIndex]; return 1; } }
// PriotityQueue.cpp
#define _CRT_SECURE_NO_WARNINGS #include "priotity.h" #include <stdio.h> int main() { //文件操作 PriQueue Q; FILE *fp; DataType task; if ((fp = fopen("task.txt", "r")) == NULL) { std::cout << "文件夹打开失败" << std::endl; exit(0); } InitQueue(&Q); while (!feof(fp)) { fscanf(fp, "%d %d", &task.element, &task.priority); Push(&Q, task); //入队 } int i = 1; //工作的序号 std::cout << "\t序号\t任务编号\t优先级" << std::endl; while (QueueEmpty(&Q)) { Pop(&Q, &task); std::cout << "\t" << i << "\t" << task.element << "\t" << task.priority << std::endl; i++; } system("pause"); return 0; }task.txt文件中内容:
1 90
2 70
3 40
4 80
5 20
6 30
7 50
相关文章推荐
- 一步一步复习数据结构和算法基础-链式队列
- 数据结构复习 - 队列Queue
- 精讲全面解读数据结构第三章栈和队列c++版
- 数据结构之队列的定义与简单实现
- 数据结构学习——队列代码
- 简单数据结构之队列模拟
- 内核数据结构之队列-kfifo
- 数据结构--二叉树--双亲、孩子和兄弟节点的查询(链式结构--队列)
- 内核数据结构之队列----kfifo
- 数据结构与算法总结3_常用的数据结构(背包,栈和队列)
- 【郝斌数据结构自学笔记】57-59_递归8 _ 汉诺塔_1线性结构总复习 2线性结构和非线性结构关系 3栈队列链表数组之间的关系【重点】
- 数据结构:通用队列
- JAVA数据结构---循环队列
- 数据结构-栈和队列
- 数据结构:循环队列
- 队列——数据结构
- 数据结构---队列
- C++数据结构--队列的应用--基数排序
- [数据结构]队列的实现-C++
- JavaScript数据结构学习之数组、栈与队列