链栈,C++,数据结构王红梅版
2014-12-23 22:51
225 查看
#ifndef _LINKSTACK_H_
#define _LINKSTACK_H_
#include "LinkStack.h"
#include<iostream>
using namespace std;
template<class DataType>
struct Node //链栈节点
{
DataType data; //节点数据域
Node<DataType> *next; //节点指针域
};
template<class DataType>
class LinkStack
{
public:
LinkStack(); //构造函数
~LinkStack(); //析构函数
void Push(DataType x); //压栈
void Pop(); //弹栈
DataType GetTop(); //得到栈顶元素
bool isEmpty(); //判断栈是否为空
private:
Node<DataType> *top; //栈顶指针
};
template<class DataType>
LinkStack<DataType>::LinkStack()
{
top=NULL; //栈初始化为空
}
template<class DataType>
LinkStack<DataType>::~LinkStack()
{
Node<DataType> *p; //栈顶指针
p=top;
while(top!=NULL)
{
top=p->next; //栈顶指针后移,相当于top--
delete p; //将栈顶节点删除
p=top;
}
}
template<class DataType>
void LinkStack<DataType>::Push(DataType x)
{
Node<DataType> *p; //栈顶节点
p=new Node<DataType>;
p->data=x;
p->next=top;
top=p;
}
template<class DataType>
void LinkStack<DataType>::Pop()
{
Node<DataType> *p;
p=top;
top=top->next;
DataType x;
x=p->data;
delete p;
cout<<x<<" ";
}
template<class DataType>
DataType LinkStack<DataType>::GetTop()
{
DataType x;
x=top->data;
return x;
}
template<class DataType>
bool LinkStack<DataType>::isEmpty()
{
return top==NULL;
}
#endif // _LINKSTACK_H_
#include "LinkStack.h"
int main()
{
LinkStack<int> link;
int i=0;
for(;i!=3;i++)
{
link.Push(i);
}
for(;i!=3;++i)
{
link.Pop();
}
return 0;
}
#define _LINKSTACK_H_
#include "LinkStack.h"
#include<iostream>
using namespace std;
template<class DataType>
struct Node //链栈节点
{
DataType data; //节点数据域
Node<DataType> *next; //节点指针域
};
template<class DataType>
class LinkStack
{
public:
LinkStack(); //构造函数
~LinkStack(); //析构函数
void Push(DataType x); //压栈
void Pop(); //弹栈
DataType GetTop(); //得到栈顶元素
bool isEmpty(); //判断栈是否为空
private:
Node<DataType> *top; //栈顶指针
};
template<class DataType>
LinkStack<DataType>::LinkStack()
{
top=NULL; //栈初始化为空
}
template<class DataType>
LinkStack<DataType>::~LinkStack()
{
Node<DataType> *p; //栈顶指针
p=top;
while(top!=NULL)
{
top=p->next; //栈顶指针后移,相当于top--
delete p; //将栈顶节点删除
p=top;
}
}
template<class DataType>
void LinkStack<DataType>::Push(DataType x)
{
Node<DataType> *p; //栈顶节点
p=new Node<DataType>;
p->data=x;
p->next=top;
top=p;
}
template<class DataType>
void LinkStack<DataType>::Pop()
{
Node<DataType> *p;
p=top;
top=top->next;
DataType x;
x=p->data;
delete p;
cout<<x<<" ";
}
template<class DataType>
DataType LinkStack<DataType>::GetTop()
{
DataType x;
x=top->data;
return x;
}
template<class DataType>
bool LinkStack<DataType>::isEmpty()
{
return top==NULL;
}
#endif // _LINKSTACK_H_
#include "LinkStack.h"
int main()
{
LinkStack<int> link;
int i=0;
for(;i!=3;i++)
{
link.Push(i);
}
for(;i!=3;++i)
{
link.Pop();
}
return 0;
}
相关文章推荐
- 数据结构的链栈
- 数据结构 - C语言版 - 链栈 所有基本操作
- 《数据结构》链栈
- C语言数据结构-链栈
- 简单数据结构----链栈的C++实现
- 【数据结构】链栈
- 数据结构——线性结构(3)——链栈的实现
- 第六周项目二数据结构之自建算法库——链栈
- 【数据结构】链栈
- 数据结构-链栈的基本操作
- 数据结构(十一)顺序栈及链栈
- 第六周--数据结构【线性表(二)链表】项目之链栈
- 数据结构-栈:顺序栈与链栈的C++模板类实现
- 数据结构 链栈
- 数据结构专题——栈与队列之链栈及其Java实现
- 【数据结构】之链栈的java实现
- 数据结构 C语言 双向链栈 迷宫问题
- C语言实现数据结构中的链栈
- 链栈的数据结构以及链栈的实现
- 数据结构 -- 链栈java实现