做网站给女朋友,网页优化包括,无锡建设厅的官方网站,网站备案一次吗《数据结构、算法与应用C语言描述》使用C语言实现数组队列
定义
队列的定义
队列#xff08;queue#xff09;是一个线性表#xff0c;其插入和删除操作分别在表的不同端进行。插入元素的那一端称为队尾#xff08;back或rear#xff09;#xff0c;删除元素的那一端称…《数据结构、算法与应用C语言描述》使用C语言实现数组队列
定义
队列的定义
队列queue是一个线性表其插入和删除操作分别在表的不同端进行。插入元素的那一端称为队尾back或rear删除元素的那一端称为队首front。
队列的抽象数据类型 数组队列实现代码
_17queue.h
抽象类栈。
/*
Project name : allAlgorithmsTest
Last modified Date: 2022年8月13日17点38分
Last Version: V1.0
Descriptions: 队列的抽象类
*/
#pragma once
#ifndef _QUEUE_H_
#define _QUEUE_H_
templateclass T
class queue
{
public:virtual ~queue() {}virtual bool empty() const 0;//返回true,当且仅当队列为空virtual int size() const 0;//返回队列中元素个数virtual T front() 0;//返回头元素的引用virtual T back() 0;//返回尾元素的引用virtual void pop() 0;//删除首元素virtual void push(const T theElement) 0;//把元素theELment加入队尾
};
#endif_18arrayQueue.h
/*
Project name : allAlgorithmsTest
Last modified Date: 2022年8月13日17点38分
Last Version: V1.0
Descriptions: 数组存储的队列的头文件
*/
#pragma once
#ifndef _ARRAYQUEUE_H_
#define _ARRAYQUEUE_H_
#includesstream
#includeiostream
#include _1myExceptions.h
#include _17queue.h
#include cmath
/*测试函数*/
void arrayQueueTest();using namespace std;
templateclass T
class arrayQueue : public queueT
{
public:/*成员函数*/arrayQueue(int initialCapacity 10);~arrayQueue() { delete[] queue; }bool empty() const { return theFront theBack; }int size() const //返回队列的元素个数{return (queueLength - theFront theBack) % queueLength;}void clear() { theFront theBack 0; }/*清空队列中的元素*/int capacity() const { return queueLength-1; }//返回第一个元素T front(){if (theFront theBack)throw queueEmpty();return queue[(theFront 1) % queueLength];}//返回最后一个元素T back(){if (theFront theBack)throw queueEmpty();return queue[theBack];}//删除队首元素void pop(){if (theFront theBack)throw queueEmpty();theFront (theFront 1) % queueLength;queue[theFront].~T();}//向队尾插入元素theElementvoid push(const T theElement);/*调整队列容量大小*/void resizeQueue(int newLength);void meld(arrayQueueT a, arrayQueueT b);//合并队列a,b到当前队列void split(arrayQueueT a, arrayQueueT b);//将当前队列分成两个队列a,b/*重载操作符*//*重载[]操作符*/T operator[](int i){ return queue[(theFront i 1) % queueLength]; }/*友元函数*/friend istream operator T(istream in, arrayQueueT m);//输出但是不pop()元素friend ostream operator T(ostream out, arrayQueueT m);
private:int theFront; // 第一个元素的前一个位置int theBack; // 最后一个元素的位置int queueLength; // 队列的容量实质上比队列容量(不包含queueFront指向的那一个位置)大1T* queue; // 指向队列首地址的指针
};
/*友元函数*/
/*操作符*/
templateclass T
istream operator(istream in, arrayQueueT m)
{int numberOfElement 0;cout Please enter the number of element:;while (!(in numberOfElement)){in.clear();//清空标志位while (in.get() ! \n)//删除无效的输入continue;cout Please enter the number of element:;}T cinElement;for (int i 0; i numberOfElement; i){cout Please enter the element i 1 :;while (!(in cinElement)){in.clear();//清空标志位while (in.get() ! \n)//删除无效的输入continue;cout Please enter the element i 1 :;}m.push(cinElement);}return in;
}
/*操作符*/
templateclass T
ostream operator(ostream out, arrayQueueT m)
{int size m.size();for (int i 0; i size; i)out m.queue[(m.theFront i 1) % m.queueLength] ;out endl;return out;
}
/*成员函数*/
/*构造函数*/
templateclass T
arrayQueueT::arrayQueue(int initialCapacity)
{if (initialCapacity 1){ostringstream s();s Initial capacity initialCapacity Must be 0;throw illegalParameterValue(s.str());}queue new T[initialCapacity1];queueLength initialCapacity1;theFront theBack 0;
}/*向队尾插入元素theElement*/
templateclass T
void arrayQueueT::push(const T theElement)
{//首先检查队列是否已满如已满则将队列容量加倍if ((theBack 1) % queueLength theFront)resizeQueue(2 * (queueLength-1)); theBack (theBack 1) % queueLength;queue[theBack] theElement;
}
/*调整队列容量大小*/
templateclass T
void arrayQueueT::resizeQueue(int newLength)
{T* temp new T[newLength 1];int size min((*this).size(), newLength);for (int i 0; i size; i)temp[i] queue[(theFront i 1) % queueLength]; queueLength newLength1;theFront newLength;theBack size - 1;delete[] queue;queue temp;
}/*
创建一个新的队列该表包含了a和b中的所有元素其中a和b的元素轮流出现表中的首
元素为a中的第一个元素。在轮流排列元素时如果某个队列的元素用完了则把另一个队列的其
余元素依次添加在新队列的后部。代码的复杂性应与两个输入队列的长度呈线性比例关系。
归并后的线性队列是调用对象*this
*/
template class T
void arrayQueueT::meld(arrayQueueT a, arrayQueueT b)
{(*this).clear();int i 0;while (i a.size() i b.size()){push(a[i]);push(b[i]);i;}while (i a.size()){push(a[i]);i;}while (i b.size()){push(b[i]);i;}
}/*生成两个线性队列a和ba包含*this中索引为奇数的元素b包含其余的元素*/
templateclass T
void arrayQueueT::split(arrayQueueT a, arrayQueueT b)
{a.clear();b.clear();int size (*this).size();for (int i 0; i size; i){if (i % 2 0)a.push(queue[(theFront i 1) % queueLength]);elseb.push(queue[(theFront i 1) % queueLength]);}
}
#endif_18arrayQueue.cpp
/*
Project name : allAlgorithmsTest
Last modified Date: 2022年8月13日17点38分
Last Version: V1.0
Descriptions: 测试_18arrayQueue.h头文件中的所有函数
*/
#include iostream
#include time.h
#include _18arrayQueue.h
using namespace std;/*测试函数*/
void arrayQueueTest()
{cout endl *********************************arrayQueueTest()函数开始************************************* endl;arrayQueueint a;//测试输入和输出cout endl 测试友元函数******************************************* endl;cout 输入输出************************ endl;cin a;cout arrayQueue a is: a;cout endl 测试成员函数******************************************* endl;cout empty()************************* endl;cout a.empty() a.empty() endl;cout size()************************** endl;cout a.size() a.size() endl;cout capacity()********************** endl;cout a.capacity() a.capacity() endl;cout push()************************** endl;cout arrayQueue a is: a;a.push(99);a.push(22);cout arrayQueue a is: a;cout front()************************* endl;cout a.front() a.front() endl;cout back()************************** endl;cout a.back() a.back() endl;cout pop()*************************** endl;cout before pop arrayQueue a is: a;a.pop();a.pop();cout after pop arrayQueue a is: a;cout resizeQueue()******************* endl;cout before resizeQueue a.capacity() a.capacity()endl;a.resizeQueue(4);cout after resizeQueue a.capacity() a.capacity() endl;cout arrayQueue a is: a;cout a.front() a.front() endl;cout a.back() a.back() endl;a.push(88);cout after resizeQueue a.capacity() a.capacity() endl;cout meld()************************** endl;arrayQueueint b;cin b;cout arrayQueue a is: a;cout arrayQueue b is: b;arrayQueueint c;c.meld(a, b);cout arrayQueue c is: c;cout split()************************* endl;arrayQueueint d;arrayQueueint e;c.split(d, e);cout arrayQueue c is: c;cout arrayQueue d is: d; cout arrayQueue e is: e;cout endl 测试成员函数性能*************************************** endl;cout push()************************** endl;arrayQueueint f;double clocksPerMillis double(CLOCKS_PER_SEC) / 1000;clock_t startTime clock();for (int i 0; i 100000000; i)f.push(i);double pushTime (clock() - startTime) / clocksPerMillis;cout 10000 push took pushTime ms endl;cout *********************************arrayQueueTest()函数结束************************************* endl;}main.cpp
/*
Project name : allAlgorithmsTest
Last modified Date: 2022年8月13日17点38分
Last Version: V1.0
Descriptions: main()函数控制运行所有的测试函数
*/
#include iostream
#include _18arrayQueue.hint main()
{arrayQueueTest();return 0;
}_1myExceptions.h
/*
Project name : allAlgorithmsTest
Last modified Date: 2022年8月13日17点38分
Last Version: V1.0
Descriptions: 综合各种异常
*/
#pragma once
#ifndef _MYEXCEPTIONS_H_
#define _MYEXCEPTIONS_H_
#include string
#includeiostreamusing namespace std;// illegal parameter value
class illegalParameterValue
{public:illegalParameterValue(string theMessage Illegal parameter value){message theMessage;}void outputMessage() {cout message endl;}private:string message;
};// illegal input data
class illegalInputData
{public:illegalInputData(string theMessage Illegal data input){message theMessage;}void outputMessage() {cout message endl;}private:string message;
};// illegal index
class illegalIndex
{public:illegalIndex(string theMessage Illegal index){message theMessage;}void outputMessage() {cout message endl;}private:string message;
};// matrix index out of bounds
class matrixIndexOutOfBounds
{public:matrixIndexOutOfBounds(string theMessage Matrix index out of bounds){message theMessage;}void outputMessage() {cout message endl;}private:string message;
};// matrix size mismatch
class matrixSizeMismatch
{public:matrixSizeMismatch(string theMessage The size of the two matrics doesnt match){message theMessage;}void outputMessage() {cout message endl;}private:string message;
};// stack is empty
class stackEmpty
{public:stackEmpty(string theMessage Invalid operation on empty stack){message theMessage;}void outputMessage() {cout message endl;}private:string message;
};// queue is empty
class queueEmpty
{public:queueEmpty(string theMessage Invalid operation on empty queue){message theMessage;}void outputMessage() {cout message endl;}private:string message;
};// hash table is full
class hashTableFull
{public:hashTableFull(string theMessage The hash table is full){message theMessage;}void outputMessage() {cout message endl;}private:string message;
};// edge weight undefined
class undefinedEdgeWeight
{public:undefinedEdgeWeight(string theMessage No edge weights defined){message theMessage;}void outputMessage() {cout message endl;}private:string message;
};// method undefined
class undefinedMethod
{public:undefinedMethod(string theMessage This method is undefined){message theMessage;}void outputMessage() {cout message endl;}private:string message;
};
#endif
文章转载自: http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.kpxky.cn.gov.cn.kpxky.cn http://www.morning.xysxj.com.gov.cn.xysxj.com http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.gqfbl.cn.gov.cn.gqfbl.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.wtdyq.cn.gov.cn.wtdyq.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn http://www.morning.gqhgl.cn.gov.cn.gqhgl.cn http://www.morning.xjnjb.cn.gov.cn.xjnjb.cn http://www.morning.drfrm.cn.gov.cn.drfrm.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.hslgq.cn.gov.cn.hslgq.cn http://www.morning.rjqtq.cn.gov.cn.rjqtq.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.ykrg.cn.gov.cn.ykrg.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.dfndz.cn.gov.cn.dfndz.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.hcwlq.cn.gov.cn.hcwlq.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn http://www.morning.kdnbf.cn.gov.cn.kdnbf.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.bgpb.cn.gov.cn.bgpb.cn http://www.morning.nmfml.cn.gov.cn.nmfml.cn http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.pplxd.cn.gov.cn.pplxd.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.fyxr.cn.gov.cn.fyxr.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.prjns.cn.gov.cn.prjns.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn http://www.morning.gcftl.cn.gov.cn.gcftl.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn http://www.morning.kzpxc.cn.gov.cn.kzpxc.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.ssrjt.cn.gov.cn.ssrjt.cn http://www.morning.wkcl.cn.gov.cn.wkcl.cn http://www.morning.cgdyx.cn.gov.cn.cgdyx.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.lhrwy.cn.gov.cn.lhrwy.cn http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.nlkm.cn.gov.cn.nlkm.cn http://www.morning.iuibhkd.cn.gov.cn.iuibhkd.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.xhqr.cn.gov.cn.xhqr.cn http://www.morning.ypfw.cn.gov.cn.ypfw.cn http://www.morning.zffps.cn.gov.cn.zffps.cn