数据结构 栈的链表实现法
来源:互联网 发布:印象笔记导出为知笔记 编辑:程序博客网 时间:2024/06/06 12:26
#include <iostream>
using namespace std;
template<class T>
class stackNode
{
public:
T data;
stackNode* next;
};
template<class T>
class mystack
{
private:
unsigned int stacklength;
stackNode<T> *node;
stackNode<T> *headnode;
public:
mystack();
unsigned int length();
void push(T x);
void isEmpty();
void pop();
void top();
void Traverse();
void clear();
};
template<class T>
mystack<T>::mystack()
{
node=NULL;
headnode=NULL;
stacklength=0;
};
template<class T>
void mystack<T>::push(T x)
{
node=new stackNode<T>();
node->data=x;
node->next=headnode;
headnode=node;
++stacklength;
};
template<class T>
void mystack<T>::isEmpty()
{
if( stacklength==0)
cout<<"Please input data!"<<endl;
else
cout<<"棧中一共有:"<<stacklength<<"個元素"<<endl;
};
template<class T>
void mystack<T>::pop()
{
node=headnode;
headnode=headnode->next;
delete(node);
--stacklength;
};
template<class T>
void mystack<T>::top()
{
if(headnode!=NULL)
cout<<"********************"<<endl;
cout<<"棧頂元素為: "<<headnode->data<<endl;
cout<<"********************"<<endl;
};
template<class T>
void mystack<T>::clear()
{
while(headnode!=NULL)
{
node=headnode;
headnode=headnode->next;
delete(node);
}
node=NULL;
headnode=NULL;
stacklength=0;
};
template<class T>
void mystack<T>::Traverse()
{
cout<<"-------------------------------------------"<<endl;
cout<<"棧表中的元素:";
stackNode<T>*p=headnode;
while(p!=NULL)
{
cout<<p->data<<' ';
p=p->next;
}
cout<<"\n-------------------------------------------"<<endl;
};
int main()
{
int x;
mystack<int> istack;
istack.isEmpty();
for(x=0;x<10;x++)
istack.push(x);
istack.top();
istack.Traverse();
istack.isEmpty();
istack.pop();
istack.top();
istack.Traverse();
istack.isEmpty();
istack.clear();
istack.isEmpty();
return 0;
}
using namespace std;
template<class T>
class stackNode
{
public:
T data;
stackNode* next;
};
template<class T>
class mystack
{
private:
unsigned int stacklength;
stackNode<T> *node;
stackNode<T> *headnode;
public:
mystack();
unsigned int length();
void push(T x);
void isEmpty();
void pop();
void top();
void Traverse();
void clear();
};
template<class T>
mystack<T>::mystack()
{
node=NULL;
headnode=NULL;
stacklength=0;
};
template<class T>
void mystack<T>::push(T x)
{
node=new stackNode<T>();
node->data=x;
node->next=headnode;
headnode=node;
++stacklength;
};
template<class T>
void mystack<T>::isEmpty()
{
if( stacklength==0)
cout<<"Please input data!"<<endl;
else
cout<<"棧中一共有:"<<stacklength<<"個元素"<<endl;
};
template<class T>
void mystack<T>::pop()
{
node=headnode;
headnode=headnode->next;
delete(node);
--stacklength;
};
template<class T>
void mystack<T>::top()
{
if(headnode!=NULL)
cout<<"********************"<<endl;
cout<<"棧頂元素為: "<<headnode->data<<endl;
cout<<"********************"<<endl;
};
template<class T>
void mystack<T>::clear()
{
while(headnode!=NULL)
{
node=headnode;
headnode=headnode->next;
delete(node);
}
node=NULL;
headnode=NULL;
stacklength=0;
};
template<class T>
void mystack<T>::Traverse()
{
cout<<"-------------------------------------------"<<endl;
cout<<"棧表中的元素:";
stackNode<T>*p=headnode;
while(p!=NULL)
{
cout<<p->data<<' ';
p=p->next;
}
cout<<"\n-------------------------------------------"<<endl;
};
int main()
{
int x;
mystack<int> istack;
istack.isEmpty();
for(x=0;x<10;x++)
istack.push(x);
istack.top();
istack.Traverse();
istack.isEmpty();
istack.pop();
istack.top();
istack.Traverse();
istack.isEmpty();
istack.clear();
istack.isEmpty();
return 0;
}
- 数据结构 栈的链表实现法
- 【数据结构】栈的链表实现
- 数据结构-----栈的链表实现
- 数据结构---栈的链表实现
- 【数据结构】栈-链表的实现
- 【数据结构】栈的链表实现
- 数据结构之栈的链表实现
- 【数据结构】栈的链表实现
- 链表数据结构的实现
- 数据结构-链表的实现
- 数据结构 栈 (链表实现)
- 数据结构 --栈的实现
- 数据结构:栈的实现
- 数据结构-----栈的实现
- 数据结构-----栈的实现
- 【数据结构】栈的实现
- 数据结构-栈的实现
- 经典数据结构之链表实现的栈
- 循环
- Eclipse开发环境搭建
- SAX中止解析XML方法
- 直接选择排序
- Android中可增删联系人列表的简单实现
- 数据结构 栈的链表实现法
- 几款代码比较工具
- 三角形类(3)
- hdu2861
- 【翻译】通过CMD3.1来实现自定义基于海王星主题的Ext JS 4.1应用程序
- Android HAL 开发 (1)
- COMMTIMEOUTS详解(转)
- 中西医诊疗与软件测试
- 【C++复习五】inline和宏