栈的顺序存储和链式存储
程序员文章站
2022-03-12 17:33:10
...
顺序存储:
#include <iostream>
#define MAXSIZE 20//栈容量
using namespace std;
typedef int Datatype;
typedef class Node Stack;
class Node
{
public:
Node();
~Node() {};
void CreateStack();//创建
int IsFull();//判断是否满栈
int IsEmpty();//判断是否空栈
void Push();//入栈
void Pop();//出栈
void TravalStack();//遍历
private:
Datatype data[MAXSIZE];//存储栈元素
int top;//记录栈顶元素下标
};
Node::Node()
{
Datatype data = 0;
top = 0;
}
//创建
void Node::CreateStack()
{
top = -1;//初始化栈为空栈顶下标为-1
return;
}
//判断是否满栈
int Node::IsFull()
{
if (top == MAXSIZE - 1)
return 1;
else
return 0;
}
//判断是否为空
int Node::IsEmpty()
{
if (top < 0)
return 1;
else
return 0;
}
//入栈
void Node::Push()
{
cout << "输入入栈元素:";
if (IsFull())
cout << "堆栈满" << endl;
else
{
top++;
cin >> data[top];
}
return;
}
//出栈
void Node::Pop()
{
if (IsEmpty())
cout << "堆栈已空" << endl;
else
{
cout << "出栈栈顶元素为:" << data[top]<<endl;
top--;
}
}
//遍历
void Node::TravalStack()
{
if (IsEmpty())
cout << "堆栈空" << endl;
cout << "当前堆栈:";
for (int i = 0; i <= top; i++)
{
cout << data[i] << " ";
}
cout << endl;
}
int main()
{
Stack s;
int i;
cout << "1.创建; 2.判断是否满栈; 3.判断栈是否为空; 4.入栈; 5.出栈; 6.遍历;0.退出" << endl;
do
{
cout << "输入操作:";
cin >> i;
switch (i)
{
case 0:break;
case 1:s.CreateStack(); break;
case 2:
if (s.IsFull())
cout << "堆栈满" << endl;
else
cout << "堆栈不满" << endl;
break;
case 3:
if (s.IsEmpty())
cout << "堆栈空" << endl;
else
cout << "堆栈不空" << endl;
break;
case 4:s.Push(); break;
case 5:s.Pop(); break;
case 6:s.TravalStack(); break;
default:
cout << "输入错误,请重新";
}
} while (i != 0);
return 0;
}
运行:
链式存储:
#include <iostream>
using namespace std;
typedef int Datatype;
typedef class Node Stack;
class Node
{
public:
Node();
~Node() {};
void CreateStack();//创建
int IsEmpty();//判断是否空
void Push();//入栈
void Pop();//出栈
void TravalStack();//遍历
private:
Datatype data;
Stack* next;//头结点
};
//构造函数初始化
Node::Node()
{
Datatype data = 0;
next = NULL;
}
//创建
void Node::CreateStack()
{
next = NULL;
return;
}
//判断是否为空
int Node::IsEmpty()
{
if (next == NULL)
return 1;//为空返回1
else
return 0;
}
//入栈
void Node::Push()
{
Stack* pnew = new Stack;//创建新结点进行存储
cout << "请输入入栈值:";
cin >>pnew->data;
pnew->next=next;//插入结点指向头结点
next = pnew;//头结点移到新结点
return;
}
//出栈
void Node::Pop()
{
if (IsEmpty())
cout << "堆栈已空;"<<endl;
else
{
cout << "出栈的栈顶值为:" << next->data<<endl;
Stack* ptemp = next;//记录当前结点
next = next->next;//结点向下移
delete ptemp;//删除出栈元素
}
return;
}
//遍历
void Node::TravalStack()
{
if (IsEmpty())
cout << "堆栈为空;" << endl;
else
{
cout << "当前堆栈为:"<<endl;
Stack* ptemp = next;//记录当前栈顶
while (ptemp!=NULL)
{
cout << ptemp->data<<endl;
ptemp = ptemp->next;
}
cout << endl;
}
}
int main()
{
Stack s;
int i;
cout << "1.创建; 2.判断栈是否为空; 3.入栈; 4.出栈; 5.遍历;"<<endl;
do
{
cout << "输入操作:";
cin >> i;
switch (i)
{
case 0:break;
case 1:s.CreateStack(); break;
case 2:
if (s.IsEmpty())
cout << "堆栈为空" << endl;
else
cout << "堆栈不空" << endl;
break;
case 3:s.Push(); break;
case 4:s.Pop(); break;
case 5:s.TravalStack(); break;
default:
cout << "输入错误,请重新";
}
} while (i != 0);
return 0;
}
运行:
上一篇: Java数据结构基础--顺序栈与链式栈
下一篇: 二叉查找树
推荐阅读
-
mysql存储方式MyISAM 和 InnoDB的区别
-
图的存储方式(邻接矩阵,邻接表,链式前向星)
-
线性表的链式存储结构:定义、单链表存储结构、给链表头结点分配空间、初始化链表数据、输出链表、在某个位置上插入数据、头插法、尾插法、删除某个位置上的数据、删除某个数据、删除整个链表计算链表的长度
-
七牛云存储 - cli和普通模式的php如何通信?
-
MySQL存储过程和函数的操作(十二)
-
利用mysql的inet_aton()和inet_ntoa()函数存储IP地址的方法分享
-
浅谈MySQL存储过程中declare和set定义变量的区别
-
SQL Server自动备份存储过程和视图的方法
-
sqlserver 批量删除存储过程和批量修改存储过程的语句
-
mysql 海量数据的存储和访问解决方案