欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

栈的顺序存储和链式存储

程序员文章站 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;
}

运行:

栈的顺序存储和链式存储