# 算法导论-5.基本数据结构

## 栈

/*Stack*/
template <typename T> class xStack{
public:
xStack(int size);
bool push(T input);
T pop();
private:
T* arrayData;
int top;
int size;
};
template <typename T> xStack<T>::xStack(int size){
arrayData = new T[size];
top = -1;
this->size = size;
}
template <typename T> bool xStack<T>::push(T input){
if(top >= size-1){
return false;
}
else{
top++;
arrayData[top] = input;
}
return true;
}
template <typename T> T xStack<T>::pop(){
if(top <= -1){
return NULL;
}
else{
int tmp = top;
top--;
return arrayData[tmp];
}
}

## 队列

/*Queue*/
template <typename T> class xQueue{
public:
xQueue(int size);
bool enQueue(T input);
T deQueue();
private:
T* arrayData;
int size;
int tail;
int step(int pos);
};
template <typename T> xQueue<T>::xQueue(int size){
arrayData = new T[size+1];
this->size = size;
tail = 0;
}
template <typename T> bool xQueue<T>::enQueue(T input){
if ((head == tail+1) || (head == 0 && tail == size)){
return false;
}
else{
arrayData[tail] = input;
tail = step(tail);
return true;
}
}
template <typename T> T xQueue<T>::deQueue()
{
return NULL;
}
else{
return arrayData[tmp];
}
}
template <typename T> int xQueue<T>::step(int pos)
{
if (pos < 0 || pos > size)    {
return -1;
}
else{
if (pos == size){
return 0;
}
else{
return pos+1;
}
}
}

## 链表

/*LinkList*/
template <typename T> class xNode{
public:
xNode();
xNode(T data);
T data;
xNode<T>* next;
};
template <typename T> xNode<T>::xNode(){
this->next = NULL;
}
template <typename T> xNode<T>::xNode(T data){
this->data = data;
this->next = NULL;
}
public:
xNode<T>* search(T value);
void insert(T input);
bool deleteNode(xNode<T>* node);
};
}
template <typename T> xNode<T>* xLinkList<T>::search(T value){
while (tmp != NULL)
{
if (tmp->data == value)
{
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
template <typename T> void xLinkList<T>::insert(T input){
xNode<T>* tmp = new xNode<T>(input);
}
template <typename T> bool xLinkList<T>::deleteNode(xNode<T>* node){
xNode<T>* prevNode = NULL;
while(tmp != NULL)
{
if (tmp->next == node)
{
prevNode = tmp;
break;
}
tmp = tmp->next;
}
if (prevNode == NULL)
{
return false;
}
prevNode->next = node->next;
return true;
}

posted @ 2013-01-05 09:13  一叶斋主人  阅读(1533)  评论(0编辑  收藏  举报