栈的链式存储结构
来源:互联网 发布:360手机 知乎 编辑:程序博客网 时间:2024/05/01 17:49
栈的链式表示,即链栈。由于栈的操作是线性表操作的特例,因此链栈可以看成运算受限的单链表。其特点如下:
①链栈无栈满问题,空间可扩充;
②插入和删除仅在栈顶处执行;
③链式栈的栈顶在链头;
④适合于多栈操作。
stackList.h
#include<iostream>
class ListInt
{
public:
ListInt() { data = 0;};
int data;
ListInt *link;
};
class StackList
{
public:
StackList() { top=0; };
~StackList();
bool IsEmpty() const {return top==0;} //栈空返回1,否则返回0
void GetTop(ListInt& x); //获得栈顶元素
bool Push(const ListInt& x); //元素x入栈
bool Pop(ListInt& x); //元素出栈,并保存在x中
bool Pop(); //元素出栈
void ClearStack(); //清除栈中所有元素
void Output(); //输出栈元素
private:
ListInt *top; //指向栈顶的结点
};
stackList.cpp
#include <iostream>
#include "stackList.h"
using namespace std;
StackList::~StackList()
{
ListInt *next;
while(top)
{
next=top->link;
delete top;
top = next;
}
}
void StackList::GetTop(ListInt& x)
{
if(!top)
{
cout<<"The stack is empty!"<<endl;
return;
}
x = *top->link;
}
bool StackList::Push(const ListInt &x)
{
ListInt *listTemp = new ListInt;
*listTemp = x;
listTemp->link = top;
top = listTemp;
return true;
}
bool StackList::Pop(ListInt &x)
{
if(IsEmpty())
{
cout<<"栈为空!"<<endl;
return false;
}
x = *top;
top = x.link;
return true;
}
bool StackList::Pop()
{
if(IsEmpty())
{
cout<<"栈为空!"<<endl;
return false;
}
ListInt *x;
x = top;
top = x->link;
delete x;
return true;
}
void StackList::Output()
{
ListInt *next;
next = top;
if(IsEmpty())
{
cout<<"栈为空!"<<endl;
return;
}
while(next)
{
cout<<next->data<<" ";
next = next->link;
}
cout<<endl;
}
void StackList::ClearStack()
{
ListInt *next;
while(top)
{
next=top->link;
delete top;
top = next;
}
top = 0;
}
.codearea{ color:black; background-color:white; line-height:18px; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys","BitStream Vera Sans Mono", courier,monospace,serif}.codearea pre{ color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap pre{white-space:pre-wrap; white-space:-moz-pre-wrap; white-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word; word-break:normal}.codearea pre.alt{ background-color:#f7f7ff !important}.codearea .lnum{color:#4f81bd;line-height:18px}
测试文件:stack.app
#include <iostream>
#include "stackList.h"
using namespace std;
int main()
{
StackList *stackList = new StackList();
ListInt x ;
x.data = 5;
stackList->Push(x);
x.data = 6;
stackList->Push(x);
x.data = 8;
stackList->Push(x);
x.data = 62;
stackList->Push(x);
x.data = 12;
cout<<"当前栈中元素为:"<<endl;
stackList->Output();
stackList->Pop(x);
cout<<"出栈元素:"<<x.data<<endl;
stackList->GetTop(x);
cout<<"栈顶元素:"<<x.data<<endl;
cout<<"当前栈中元素为:"<<endl;
stackList->Output();
return 0;
}
运行结果:
- 栈的链式存储结构
- 栈的链式存储结构
- 栈的链式存储结构
- 栈的链式存储结构
- 栈的链式存储结构
- 栈的链式存储结构
- 栈的链式存储结构
- 栈的链式存储结构
- 栈的链式存储结构
- 栈 -- 链式存储结构
- Day17、链式存储结构动态分配内存、栈的顺序存储结构和链式存储结构
- 栈的链式存储结构的实现
- 数据结构(栈):栈的链式存储结构
- 栈的链式存储结构与操作
- 栈的链式存储结构与操作
- 基于链式存储结构的栈
- 数据结构四栈的链式存储结构
- 数据结构:栈的链式存储结构
- bash中关于控制字符的显示和转换
- 彻底搞定C指针
- 常用sql相关
- 信号量函数 semget() semop() semctl() 说明
- 常用工具类
- 栈的链式存储结构
- 给packetix的vpn加了几个路由
- Delegate.BeginInvoke例子
- C#中“using”小提示
- 2010.4.9 委托和事件学习
- WinCE下存储设备的遍历以及磁盘分区的格式化、扫描和碎片整理类
- 全局数据库名?数据库名?实例名?
- unable to start debugging on the web server.
- ubuntu 下c和c++环境配置以及man文档的安装