数据结构草草学过,不过没有认真运用过。 虽然知道一些最为基本的抽象类型及一些常用操作,不过叫我把这些基本的算法写出来我也是写不出来的。因为常说数据结构+算法是一个程序员最基本的素质,所以这次认真加以复习。在复习的同时我尽量将自己学习的其他的一些基本知识比如C++中的面向对象思想也引入进来,同时也会将在复习中碰到其他的一些问题提出来,能解决的便解决,不能解决的可以试着解决。
To be a programmer .
队列是另外一种限定性的线性表,它只允许在表的一端插入元素,而在另一端删除元素。允许插入的一端叫做队尾,允许删除的一端称为对头。
队列只是一种抽象的数据类型,在计算机内有两种实现方式:顺序队列和链队列。通常用数组来表示顺序队列,本文档直接考虑循环队列的基本运算。
文章的组织结构分为三部分:开头是简单的介绍,之后是数据结构基本运算的代码,最后是在复习中遇到的一些问题。
我晕倒! 原来还自以为是的认为自己在代码实现的时候运用了类模版和自定义异常层次还有点新意,起码与《数据结构 c语言描述》多了一些面向对象的东西。没想到的是,今天突然翻了下《数据结构 c++语言描述》,里面早就是这样的例子,太土了!
以下代码实现了一些线性表最基本的运算,有关代码的两点总结:
1) 学会使用默认形式参数;
2) 学会在返回的时候并不仅仅是返回常用的数据类型, 试试this。
实现代码分三个部分:queueexception.h定义针对队列的异常类,queue.h定义了模版类,main.cpp进行简单的测试。
#include <iostream>
class QueueExcep
{
public:
QueueExcep()
{
}
~QueueExcep()
{
}
virtual void print()
{
std::cout <<" An Exception has occured !/n";
}
};
class QueueOverflow : public QueueExcep
{
public:
QueueOverflow(){}
~QueueOverflow(){}
void print()
{
std::cout <<" Overflow ! No Enough Memory. /n";
}
};
class QueueOutofBound : public QueueExcep
{
public:
QueueOutofBound(){}
~QueueOutofBound(){}
void print()
{
std::cout <<" Out Of Bound ! /n";
}
};
#include "queueexception.h"
/*
* class decleration
*/
template <class T>
class Queue
{
public:
Queue(int maxQueueSize = 6)
{
maxSize = maxQueueSize + 1;
queue = new T[maxSize];
front = 0;
rear = 0;
}
~Queue()
{
delete []queue;
}
bool isEmpty();
bool isFull();
T first();
T last();
Queue<T>& insert(T item);
Queue<T>& delet();
private:
int maxSize;
int front;
int rear;
T *queue;
};
/*
* previous state: the queue has been created.
* input: no.
* purpose: to check if the queue is empty.
* output: bool value
* next state: no
*/
template <class T>
bool Queue<T>::isEmpty()
{
if (front == rear)
return true;
return false;
}
/*
* previous state: the queue has been created.
* input: no.
* purpose: to check if the queue is full.
* output: bool value
* next state: no
*/
template <class T>
bool Queue<T>::isFull()
{
if (front == (rear+1) % maxSize)
return true;
return false;
}
/*
* previous state: the queue is not empty.
* input: no
* purpose: return the first element of the queue.
* output: the first elment of the queue.
* previous state: no alteration.
*/
template <class T>
T Queue<T>::first()
{
if (isEmpty())
throw QueueOutofBound();
return queue[front+1];
}
/*
* previous state: the queue is not empty.
* input: no
* purpose: return the last element of the queue.
* output: the last elment of the queue.
* previous state: no alteration.
*/
template<class T>
T Queue<T>::last()
{
if (isEmpty())
throw QueueOutofBound();
return queue[rear];
}
/*
* previous state: the queue has been created.
* input: one element .
* purpose: to insert one element to the queue.
* output: a pointer to the queue
* next state: the element has been inserted successfully or not.
*/
template <class T>
Queue<T>& Queue<T>::insert(T item)
{
if (isFull())
throw QueueOverflow();
rear = (rear + 1) % maxSize;
queue[rear] = item;
return *this;
}
/*
* previous state: the queue has been created.
* input: no.
* purpose: to delete one element from the queue.
* output: a pointer to the queue
* next state: the element has been deleted successfully or not.
*/
template <class T>
Queue<T>& Queue<T>::delet()
{
if (isEmpty())
throw QueueOutofBound();
front = (front + 1) % maxSize;
return *this;
}
#include "queue.h"
int main()
{
try
{
Queue<int> Q(6);
for (int i=0; i<6; i++)
Q.insert(i);
std::cout <<"the first element of the queue is:/n";
std::cout <<Q.first() <<std::endl;
std::cout <<"the last element of the queue is:/n";
std::cout <<Q.last() <<std::endl;
for (int i=0; i<22; i++)
Q.delet();
}
catch (QueueOverflow &ex)
{
ex.print();
}
catch (QueueOutofBound &ex)
{
ex.print();
}
catch (QueueExcep &ex)
{
ex.print();
}
catch (...)
{}
return 0;
}
在学习vim的同时,发现必须学会使用make工具。编译的文件虽然不多,但是也觉得每次把全部文件名都敲进去或者滚动命令找到之前的编译命令都是挺繁琐的,因而也来小试牛刀,学着写个简单的Makefile文件来帮帮忙。
.PHONY : clean
clean :
$(RM) –r –f *.o