栈
#ifndef ARRAY_STACK_H
#define ARRAY_STACK_H
#include <iostream>
using namespace std;
template<class T>
class ArrayStack
{
public:
ArrayStack(unsigned int length);
~ArrayStack();
void push(T t);
T peek();
T pop();
int size();
int is_Empty();
private:
T* arr;
unsigned int count;
};
template<class T>
ArrayStack<T>::ArrayStack(unsigned int length):count(0)
{
arr = new T[count];
if (!arr){ cout << "arr malloc error" << endl; }
}
template<class T>
ArrayStack<T>::~ArrayStack()
{
if (arr)
{
delete[] arr;
arr = nullptr;
}
}
template<class T>
void ArrayStack<T>::push(T t)
{
arr[count] = t;
count++;
}
template<class T>
T ArrayStack<T>::peek()
{
return arr[count - 1];
}
template<class T>
T ArrayStack<T>::pop()
{
int ret = arr[count - 1];
count--;
return ret;
}
template<class T>
int ArrayStack<T>::size()
{
return count;
}
template<class T>
int ArrayStack<T>::is_Empty()
{
return size() == 0;
}
#endif
队列
#ifndef ARRAY_QUEUE_H
#define ARRAY_QUEUE_H
#include <iostream>
using namespace std;
template<class T>
class ArrayQueue
{
public:
ArrayQueue(unsigned int length);
~ArrayQueue();
void add(T t);
T front();
T pop();
int size();
int isEmpty();
private:
T* arr;
int count;
};
template<class T>
ArrayQueue<T>::ArrayQueue(unsigned int length) :count(0)
{
arr = new T[length];
if (!arr)
{
cout << "arr malloc error!" << endl;
}
}
template<class T>
ArrayQueue<T>::~ArrayQueue()
{
if (arr)
{
delete[] arr;
arr = nullptr;
}
}
template<class T>
void ArrayQueue<T>::add(T t)
{
arr[count] = t;
count++;
}
template<class T>
T ArrayQueue<T>::front()
{
return arr[0];
}
template<class T>
T ArrayQueue<T>::pop()
{
count--;
return arr[count];
}
template<class T>
int ArrayQueue<T>::size()
{
return count;
}
template<class T>
int ArrayQueue<T>::isEmpty()
{
return 0 == count;
}
#endif
双向链表
#ifndef DOUBLE_LINK_H
#define DOUBLE_LINK_H
#include <iostream>
using namespace std;
template<class T>
struct DNode
{
public:
DNode(){}
DNode(T t, DNode* prev, DNode* next)
{
this->value = t;
this->prev = prev;
this->next = next;
}
public:
T value;
DNode* prev;
DNode* next;
};
template<class T>
class DoubleLink
{
public:
DoubleLink();
~DoubleLink();
int size();
int is_empty();
T get(int index);
T get_first();
T get_last();
int insert(int index, T t);
int insert_first(T t);
int insert_last(T t);
int del(int index);
int delete_first();
int delete_last();
private:
int count;
DNode<T>* phead;
DNode<T>* get_node(int index);
};
template<class T>
DoubleLink<T>::DoubleLink() :count(0)
{
phead = new DNode<T>();
phead->prev = phead->next = phead;
}
template<class T>
DoubleLink<T>::~DoubleLink()
{
DNode<T>* ptmp;
DNode<T>* pnode = phead->next;
while (pnode != phead)
{
ptmp = pnode;
pnode = pnode->next;
delete ptmp;
}
delete phead;
phead = nullptr;
}
template<class T>
int DoubleLink<T>::size(){ return count; }
template<class T>
int DoubleLink<T>::is_empty(){ return count == 0; }
template<class T>
DNode<T>* DoubleLink<T>::get_node(int index)
{
if (index < 0 || index > count)
{
cout << "get node failed! the index in out of bound!" << endl;
return nullptr;
}
if (index <= count / 2)
{
int i = 0;
DNode<T>* pindex = phead->next;
while (i < index)
{
pindex = pindex->next;
i++;
}
return pindex;
}
else
{
int i = count;
DNode<T>* pindex = phead->prev;
while (i > index)
{
pindex = pindex->prev;
i--;
}
return pindex;
}
}
template<class T>
T DoubleLink<T>::get(int index)
{
return get_node(index)->value;
}
template<class T>
T DoubleLink<T>::get_first()
{
return get_node(0)->value;
}
template<class T>
T DoubleLink<T>::get_last()
{
return get_node(count-1)->value;
}
template<class T>
int DoubleLink<T>::insert(int index, T t)
{
if (index < 0 || index > count)
{
cout << "get node failed! the index in out of bound!" << endl;
return -1;
}
DNode<T>* pindex = get_node(index);
DNode<T>* pnode = new DNode<T>(t, pindex->prev, pindex);
pindex->prev->next = pnode;
pindex->prev = pnode;
count++;
return 0;
}
template<class T>
int DoubleLink<T>::insert_first(T t)
{
insert(0, t);
return 0;
}
template<class T>
int DoubleLink<T>::insert_last(T t)
{
insert(count - 1, t);
return 0;
}
template<class T>
int DoubleLink<T>::del(int index)
{
DNode<T>* pindex = get_node(index);
pindex->next->prev = pindex->prev;
pindex->prev->next = pindex->next;
delete pindex;
count--;
return 0;
}
template<class T>
int DoubleLink<T>::delete_first(){ return del(0); }
template<class T>
int DoubleLink<T>::delete_last(){ return del(count - 1); }
#endif