1 / 6
文档名称:

数据结构示例(二)——栈和队列.doc

格式:doc   页数:6页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

数据结构示例(二)——栈和队列.doc

上传人:zgs35866 2015/6/6 文件大小:0 KB

下载得到文件列表

数据结构示例(二)——栈和队列.doc

相关文档

文档介绍

文档介绍:1、栈

template<class T>
class SeqStack
{ private:
int top;
int MaxSize;
T *data;
public:
SeqStack(int size=100);
~SeqStack(){delete[] data;}
bool IsEmpty() const { return top==-1; }
bool IsFull() const {return top==MaxSize-1; }
void Push(const T& x);
void Pop(T& x);
T GetTop() const;
};
template<class T>
SeqStack<T>::SeqStack(int size)
{ MaxSize=size;
data=new T[MaxSize];
top=-1;
}
template<class T>
void SeqStack<T>::Push(const T& x)
{ if(IsFull())
{ cerr<<"堆栈已满!"<<endl;
exit(1);
}
data[++top]=x;
}
template<class T>
void SeqStack<T>::Pop(T& x)
{ if(top==-1)
{ cerr<<"堆栈空!"<<endl;
exit(1);
}
x=data[top--];
}
template<class T>
T SeqStack<T>::GetTop()const
{ return data[top];
}


2、链栈

template<class T>
struct Node
{ T data;
Node<T> *next;
};
template<class T>
class LinkStack
{
private:
Node<T> *top;
public:
LinkStack() { top=NULL; }
~LinkStack();
bool IsEmpty() const { return top==NULL; }
void Push(const T& x);
void Pop(T& x);
T GetTop() const;
};
template<class T>
LinkStack<T>::~LinkStack()
{
Node<T> *p;
while(top)
{ p=top;
top=top->next;
delete p;
}
}
template<class T>
void LinkStack<T>::Push(const T& x)
{
Node<T> *s=new Node<T>;
s->data=x;
s->next=top;
top=s;
}
template<class T>