顺序栈和链栈的基本操作实现

来源:互联网 发布:网络谣言研究报告 编辑:程序博客网 时间:2024/05/22 16:40

一、实验目的1、熟练掌栈的结构特点,掌握栈的顺序存储和链式存储结构和实现。

2、 学会使用栈解决实际问题。

二、实验内容

自己确定结点的具体数据类型和问题规模:

分别建立一个顺序栈和链栈,实现栈的压栈和出栈操作。

三、实验步骤

1、依据实验内容分别说明实验程序中用到的数据类型的定义;

2、相关操作的算法表达;

3、完整程序;

4、总结、运行结果和分析。

5、总体收获和不足,疑问等。

四、实验代码

1、顺序栈

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#include <iostream>
using namespace std;

//顺序栈的实现
const int StackSize=10;

class SeqStack
{
public:
SeqStack(); //构造函数,初始化一个空栈
~SeqStack(){} //析构函数为空
void Push(int x); //压/入栈操作,将元素x入栈
int Pop(); //弹/出栈,将栈顶元素弹 出
int GetTop();//取栈顶元素(并不删除)
int Empty();
private:
int data[StackSize];//存放栈元素的数组
int top; //栈顶指针,为栈顶元素在数组中的下标
};

SeqStack::SeqStack()
{
top=-1;
}

//顺序表压栈函数Push

void SeqStack::Push(int x)
{
if(top==StackSize-1)throw "上溢";
data[++top]=x;
}

//顺序表弹栈操作函数Pop

int SeqStack::Pop()
{
if(top==-1)throw"下溢";
int x=data[top--];
return x;
}

int SeqStack::GetTop()
{
if(top!=-1)
return data[top];
}

int SeqStack::Empty()
{
if(top==-1)return 1;
{
if(top==-1)return 1;
else return 0;
}
}

void main()
{
SeqStack k1;
if(k1.Empty())
cout<<"栈为空"<<"\n"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对99和88执行入栈操作"<<"\n"<<endl;
k1.Push(99);
k1.Push(88);
cout<<"栈顶元素为:"<<"\n"<<endl;
cout<<k1.GetTop()<<"\n"<<endl;
cout<<"执行一次出栈操作"<<"\n"<<endl;
k1.Pop();
cout<<"栈顶元素为:"<<"\n"<<endl;
cout<<k1.GetTop()<<"\n"<<endl;
}
void main()
{
SeqStack k1;
if(k1.Empty())
cout<<"栈为空"<<"\n"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对99和88执行入栈操作"<<"\n"<<endl;
k1.Push(99);
k1.Push(88);
cout<<"栈顶元素为:"<<"\n"<<endl;
cout<<k1.GetTop()<<"\n"<<endl;
cout<<"执行一次出栈操作"<<"\n"<<endl;
k1.Pop();
cout<<"栈顶元素为:"<<"\n"<<endl;
cout<<k1.GetTop()<<"\n"<<endl;
}


2、链栈

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#include<iostream>
using namespace std;

struct Node
{
int data;
Node *next;
};

class LinkStack
{
public:
LinkStack();
~LinkStack();
void Push(int x);
int Pop();
int GetTop();
int Empty();
private:
Node *top;
};

//构造函数
LinkStack::LinkStack()
{
top=new Node;
top=NULL;
}

//压栈函数
void LinkStack::Push(int x)
{
Node *s=NULL;
s=new Node;
s->data=x;
s->next=top;
top=s;
}
//弹栈函数
int LinkStack::Pop()
{
if(top==NULL)throw"下溢";
else
{
int x;Node *p=NULL;
p=top;
x=top->data;
top=top->next;
delete p;
return x;
}
}
//取栈顶元素
int LinkStack::GetTop()
{
if(top!=NULL)
return top->data;
}
//判空函数
int LinkStack::Empty()
{
if(top==NULL)
return 1;
else return 0;
}
//析构函数
LinkStack::~LinkStack()
{
Node *q=NULL;
while(top!=NULL)
{
q=top;
top=top->next;
delete q;
}
}
//主函数
void main()
{
LinkStack s;
if(s.Empty())
cout<<"此栈为空!"<<"\n"<<endl;
else
cout<<"此栈不为空"<<"\n"<<endl;
cout<<"对99和88进行压栈操作""\n"<<endl;
s.Push(99);
s.Push(88);
cout<<"栈顶元素为:"<<"\n"<<endl;
cout<<s.GetTop()<<"\n"<<endl;
cout<<"执行一次弹栈操作"<<"\n"<<endl;
s.Pop();
cout<<"栈顶元素为:"<<"\n"<<endl;
cout<<s.GetTop()<<"\n"<<endl;
}
五、实验运行结果

1、顺序栈


2、链栈



六、实验总结及心得

1、通过这次实验,可以得出以下总结:

(1)初始时顺序栈必须确定一个固定的长度,所以有存储个数的限制和空间浪费的问题

(2)而链栈没有栈满的问题,只有当内存没有空间可用时才会出现栈满,但是每个元素都需要一个指针域,从而产生了结构性开销。

(3)当栈的使用过程中元素个数变化较大时,用链栈是适宜的;反之,应该使用顺序栈。

2、这次的实验中,顺序栈和链栈的算法和代码相比顺序表和单链表要简单一点。而实验过程中比较容易出现的问题是不细心导致调试不出来,还有隔使我时间久了容易忘记,所以以后还是要多多回顾一下学过的知识,温故而知新才是。







原创粉丝点击