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

数据结构课后题源码

2016-05-24 21:10 405 查看
题目描述:若将一个双端队列顺序表表示在一维数组V[m]中,两个端点设为end1和end2,并组织成一个循环队列。如图3.29所示,试写出双端队列所用指针end1和end2的初始化条件及队空和队满条件,并基于此结构的相应的插入(EnQueue)和删除(DeQueue)算法。

下面是博主自己码的算法,思路很简单,程序也进行简单的注释了,相信大家不难理解。

#ifndef DOUBLEQUEUE_H_
#define DOUBLEQUEUE_H_
#include <iostream>

using namespace std;

template<class T>
class DoubleQueue{
private:
int end1;        //指向队列的首端节点
int end2;        //指向队列的尾端节点
T * elements;
int maxSize;
int tag;        //一个标志位,每当执行进队列操作时令tag==1;执行退队列操作时令tag==0;
public:
DoubleQueue(int sz = 10);
~DoubleQueue();
void EnQueueTail(const T & x);    //从尾端入队列
void EnQueueHead(const T & x);    //从首端入队列
bool DeQueueTail(T & x);          //从尾端出队列
bool DeQueueHead(T & x);          //从首端出队列
void initiaQueue();
void makeEmpty(){ end1 = end2 = 0; }   //inline函数,用来置空队列,主要是为了后面的测试程序
};

template<class T>
DoubleQueue<T>::DoubleQueue(int sz)   //类的构造函数,用来初始化累的私有成员
{
maxSize = sz;
tag = 0;
end1 = end2 = 0;
elements = new T[maxSize];       //分配一个T类型的一维数组空间
}

template<class T>
DoubleQueue<T>::~DoubleQueue()      //析构函数,用来删除类对象,释放内存
{
delete[] elements;
}

template<class T>
void DoubleQueue<T>::EnQueueTail(const T & x)
{
if (end2 % maxSize == end1 && tag==1)    //进队列时判断队列是否已满,是同过end1和end2的值和tag的值来判断,若end1==end2且tag==1即上次操作为进队列操作,则说明队列已满
{
cout << "队列已满,不能再插入数据!" << endl;
}
else
{
elements[end2] = x;
end2 = (end2 + 1) % maxSize;      //因为实现的是循环双端队列,所以需要通过这个语句来提供逻辑上的循环队列
tag == 1;                         //操作为进队列,置tag为1
cout << "尾端数据插入队列成功!" << endl;
}
}

template<class T>
void DoubleQueue<T>::EnQueueHead(const T & x)
{
if (end2 % maxSize == end1 && tag==1)  //作用与上述相同
{
cout << "队列已满,不能再插入数据!" << endl;
}
else
{
end1 = (end1 - 1 + maxSize) % maxSize;
elements[end1] = x;
tag == 1;                         //操作为进队列,tag置1
cout << "首端数据插入队列成功!" << endl;
}
}

template<class T>
bool DoubleQueue<T>::DeQueueTail(T & x)
{
if (end1 == end2 && tag==0)   //出队列时判断是否为空队列,原理上述讲过
{
cout << "队列为空,无法进行删除操作!" << endl;
return false;
}
else
{
end2 = (end2 - 1 + maxSize) % maxSize;
x = elements[end2];
cout << "尾端数据出队列成功!" << endl;
tag == 0;                               //操作为出队列,置tag为0
return true;
}
}

template<class T>
bool DoubleQueue<T>::DeQueueHead(T & x)
{
if (end1 == end2 && tag==0)   //与上述相同
{
cout << "队列为空,不能进行删除操作!" << endl;
return false;
}
else
{
x = elements[end1];
end1 = (end1 + 1) % maxSize;
cout << "首端数据出队列成功!" << endl;
tag == 0;                       //操作为出队列,置tag为0
return true;
}
}

template<class T>
void DoubleQueue<T>::initiaQueue()   //用来初始化队列,这主要是来测试上述算法的,大家可以根据自己需要来选择是否留下此函数
{
T value;
for (int i = 0; i < maxSize; i++)
{
cout << "请输入第" << i + 1 << "个节点的值: ";
cin >> value;
elements[end2] = value;
end2 = (end2 + 1) % maxSize;
}
cout << "含有" << maxSize << "个节点的队列初始化成功了!" << endl;
}
#endif
// ex3-26.cpp : Defines the entry point for the console application.
//  简单的算法测试程序,打击可以自己设计测试程序

#include "stdafx.h"
#include "doubleQueue.h"

int _tmain(int argc, _TCHAR* argv[])
{
DoubleQueue<int> a(5);
a.initiaQueue();
int temp = 2;
a.EnQueueHead(temp);
a.EnQueueTail(temp);
int temp2;
a.makeEmpty();
a.DeQueueHead(temp2);
a.DeQueueTail(temp2);
a.EnQueueTail(temp);

system("pause");
return 0;
}


这是博主自己写的,肯定存在很多不足,忘大家多多指出,博主也一直在学习,想进步,还有大家不喜勿喷!谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息