• C++中栈有顺序栈和链栈之分。在顺序栈中,定义了栈的栈底指针(存储空间首地址base)、栈顶指针top以及顺序存储空间的大小stacksize(个人感觉这个数据成员是能够不用定义的)
//顺序栈数据结构C++类声明(基类)
template <typename ElemType>
class SqStack
{
public:
void clear(); //把顺序栈置空
int getLength(); //求顺序栈中元素个数
int getstackSize(); //返回当前已分配的存储空间的大小
Status getTop(ElemType & e); //读栈顶的元素
bool isEmpty(); //推断顺序栈是否为空
SqStack<ElemType> operator =(SqStack<ElemType> rightS); //重载赋值运算符的定义
Status pop(ElemType & e); //弹出栈顶元素到e
void push(ElemType & e ); //在栈顶压入元素e
//*****************************以下为系统自己主动调用构造函数及析构函数声明******************************// SqStack(); //顺序栈构造函数
virtual ~SqStack();//顺序栈析构函数
SqStack (const SqStack<ElemType>& otherS);//顺序栈拷贝初始换构造函数 protected:
ElemType *base;
ElemType *top;
int stackSize;//顺序存储空间的大小
};
  • 而对于链栈来说,它仅仅定义栈顶指针。
template<typename ElemType>
class Linkstack
{ private:
class LinkNode
{
public:
ElemType data;
LinkNode *next;
};
typedef LinkNode * NodePointer; public:
void clear();
int getlength();
void display();
void randLinkStack();
Linkstack <ElemType> operator = (Linkstack <ElemType> rightS); protected:
NodePointer top; };

事实上这二者的差别是由顺序表和链表的存储结构决定的,在空间上,顺序表是静态分配的,而链表则是动态分配的;就存储密度来说:顺序表等于1,而链式表小于1。可是链式表能够将非常多零碎的空间利用起来;顺序表查找方便。链式表插入和删除时非常方便。

顺序表的这样的静态存储的方式,决定了必须至少得有首地址和末地址来决定一个空间。否则,不知道查找到哪了。链式表每一个节点存储了下一个节点的指针信息,故,对于链栈来说,仅仅须要一个top指针就可以查找到整个栈。

另外,顺序栈和链栈的top指针有差别,顺序栈的top指针指向栈定的空元素处,top-1才指向栈定元素,而链栈的top指针相当于链表的head指针一样,指向实实在在的元素。

另外附自己写的顺序栈和链栈的随机产生函数:

//顺序栈:
template<typename ElemType>
void MyStack<ElemType>::RandStack()
{
int *p;
ElemType n;
ElemType Elem[11];
srand(time(NULL));
n=rand()%10+1;
cout<<"产生的随机栈的深度为:"<<n<<endl;
cout<<"产生的随机栈元素为:"<<endl;
for (int i = 0; i < n; i++)
{
Elem[i]=rand()%100+1;
cout<<Elem[i]<<" ";
}
cout<<endl;
base=new ElemType[n];
assert(base!=0);
top=base;
stackSize=n;
for (int j = 0; j < n; j++)
*(base+j)=Elem[j];
top=base+n;
cout<<"随机产生的栈为:"<<endl;
for (int i = 0; i < stackSize; i++)
cout<<" "<<*(base+i);
cout<<endl;
cout<<" ♂";
for (int i = 1; i <stackSize ; i++)
{
setw(4);
cout<<" ";
}
cout<<" ♂"<<endl;
cout<<" base";
for (int i = 1; i <stackSize ; i++)
{
setw(4);
cout<<" ";
}
cout<<" top"<<endl; } template<typename ElemType>
void MyStack<ElemType>::display()
{
int n=top-base;
cout<<"当前栈为:"<<endl;
for (int i = 0; i < n; i++)
cout<<" "<<*(base+i);
cout<<endl;
cout<<" ♂";
for (int i = 1; i <n ; i++)
{
setw(4);
cout<<" ";
}
cout<<" ♂"<<endl;
cout<<" base";
for (int i = 1; i <n ; i++)
{
setw(4);
cout<<" ";
}
cout<<" top"<<endl;
} //链栈
template<typename ElemType>
void Linkstack<ElemType>::display()
{
NodePointer r;
int num=0;
r=top;
while (r)
{
cout<<r->data<<" ";
r=r->next;
num++;
}
cout<<endl;
for(int i=0;i<num-1;i++)
cout<<setw(4)<<" ";
cout<<"↑" ;
cout<<endl;
for(int i=0;i<num-1;i++)
cout<<setw(4)<<" ";
cout<<"top"<<endl; } template <typename ElemType>
void Linkstack<ElemType>::randLinkStack()
{
ElemType elem[11];
srand(unsigned(time(NULL)));
int n;
n=rand()%10+1;
cout<<"the number of the stack is:"<<n<<endl;
cout<<"the elements here are:";
for (int i = 0; i < n; i++)
{
elem[i]=rand()%100+1;
cout<<elem[i]<<" ";
}
cout<<endl;
NodePointer p,s;
p=NULL;
for (int i = 0; i < n; i++)
{
s=new(LinkNode);
assert(s!=NULL);
s->data=elem[i];
s->next=p;
p=s;
}
top=p;
cout<<"the stack produced is:"<<endl;
NodePointer r;
int num=0;
r=top;
while (r)
{
cout<<r->data<<" ";
r=r->next;
num++;
}
cout<<endl;
for(int i=0;i<num-1;i++)
cout<<setw(4)<<" ";
cout<<"↑" ;
cout<<endl;
for(int i=0;i<num-1;i++)
cout<<setw(4)<<" ";
cout<<"top"<<endl;
}

C++栈学习——顺序栈和链栈的差别的更多相关文章

  1. java与数据结构(6)---java实现链栈

    栈之链式存储结构链栈 链栈 栈的链式存储结构成为链栈.链栈是没有头结点,头结点就是栈顶指针top. 代码结构 package list; public interface Stackable;公共接口 ...

  2. C++实现链栈的基本操作

    之前对顺序栈写了基本操作,认为有必要也动手练练栈的链表实现. 对于链栈,一般不会出现栈满的情况. 链栈头文件定义例如以下: #ifndef CSTOCK_H_ #define CSTOCK_H_ ty ...

  3. 链栈的基本操作(C语言)

    栈的链式储存结构称为链栈.链栈的节点类型与链式线性表的节点类型 定义相同,不同的是它是仅在表头进行操作的单链表.链栈通常用不带头节 点的单链表来实现,栈顶指针就是链表的头指针 ,如图所示: 代码如下: ...

  4. 【C#】【数据结构】006-栈:链栈

    C#数据结构:链栈 1.自定义链栈结构: 链栈节点类 using System.Collections; using System.Collections.Generic; using UnityEn ...

  5. C语言数据结构链栈(创建、入栈、出栈、取栈顶元素、遍历链栈中的元素)

    /**创建链栈*创建一个top指针代表head指针*采用链式存储结构*采用头插法创建链表*操作 创建 出栈 入栈 取栈顶元素*创建数据域的结构体*创建数据域的名称指针*使用随机函数对数据域的编号进行赋 ...

  6. 栈stack(2):栈的链表实现

    定义 从上一篇我们知道,栈(stack)是一个只允许一端进行删除插入操作的线性表.同时,我们联想到线性表的链式结构,其特点是用一组任意的存储单元存储线性表的数据元素,因此我们选择使用链表去实现栈,规定 ...

  7. 顺序栈,链栈,队列java实现

    顺序栈 /** * 顺序栈 * */ public class SqStack { //栈的大小 private int maxSize; //栈顶指针 private int top; privat ...

  8. 【Java】 大话数据结构(6) 栈的顺序与链式存储

    本文根据<大话数据结构>一书,实现了Java版的栈的顺序存储结构.两栈共享空间.栈的链式存储机构. 栈:限定仅在表尾进行插入和删除操作的线性表. 栈的插入(进栈)和删除(出栈)操作如下图所 ...

  9. 数据结构实验3:C++实现顺序栈类与链栈类

      实验3 3.1 实验目的 熟练掌握栈的顺序存储结构和链式存储结构. 熟练掌握栈的有关算法设计,并在顺序栈和链栈上实现. 根据具体给定的需求,合理设计并实现相关结构和算法.3.2实验要求3.2.1 ...

随机推荐

  1. Linux FastDFS 分布式文件系统安装

    Linux FastDFS 分布式文件系统安装 2013 年 3 月 11 日 – 09:21 | 930 views | 收藏  (No Ratings Yet) FastDFS是一款类Google ...

  2. Hadoop学习笔记(一)——编译安装和配置

    近期工作调动.打算补一下大数据处理的知识.可能会陆续涉及hadoop.mongodb.ddbs等. 首先Apache提供二进制的Hadoop版本号是32位的.在启动时总是有警告,所以想自己编译一遍.部 ...

  3. Swift语言精要 - 浅谈结构体(Struct)

    CGRect, CGSize, CGPoint这些是 . String, Int, Array, Dictionary这些我们经常用的也是结构体(Struct). 那么结构体(Struct)到底是什么 ...

  4. ubuntu建立快捷方式

    拷贝一下文件到新的文件里 [Desktop Entry]Categories=Development;Comment[zh_CN]=Comment=Exec=/usr/local/idea-IU-17 ...

  5. __builtin_constant_p

    int __builtin_constant_p (exp); You can use the built-in function __builtin_constant_p to determine ...

  6. ajax 与springmvc交互返回数据

    1.controller将数据封装成json格式返回页面 @RequestMapping("/dataList") public void datalist(CsoftCunsto ...

  7. 算法笔记_223:打印回型嵌套(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 *********** * * * ******* * * * * * * * *** * * * * * * * * * * *** * * * ...

  8. C# new和初始化

    虽然知道使用new可以创建对象,但一直不是很理解初始化和new等知识的具体. 通过8个问题和需求,了解相关知识. 了解问题和需求 1.new 的三个步骤 2.初始化是什么意思. 3.变量声明后和变量赋 ...

  9. Linux主流架构运维工作简单剖析

    转载:http://wgkgood.blog.51cto.com/1192594/1586259 随着IT运维的不断发展,尤其的Linux的飞速发展,越来越多的企业开始使用Linux操作系统平台,例如 ...

  10. Spring Boot动态修改日志级别

    1. pom中引入 org.springframework.boot       spring-boot-starter-actuator 2. 发送POST请求: 地址: http://[服务地址] ...