Basic operation of chain stack (super detailed)

Table of contents

foreword

1. Definition of chain stack 

2. The c++ language structure description of the chain stack

3. Realization of basic operations in the chain stack 

3.1 Initialization of the chain stack

3.2 Determine whether the chain stack is empty 

3.3 Find the length of the chain stack 

3.4 Pushing the chain stack into the stack

3.4 Popping the chain stack

3.5 Find the top element of the stack 

3.6 Destroy the stack

4. The specific implementation of the chain stack 

5. Test results

6. Summary 


foreword

This article refers to Mr. Wang Zhuo's data structure video and Mr. Yan Weimin's "Data Structure"

1. Definition of chain stack 

Stack: A linear list with limited operations, limited to a linear list that only performs insertion and deletion operations at the end of the list, that is, last-in-first-out. This end is called the top of the stack, and the other end is called the bottom of the stack.

Chain stack: a stack stored in a chain structure (I actually use a single linked list without a head node)

Example: Similar to the bullet being pressed into the magazine, the bullet inserted later can be ejected from the magazine first.

2. The c++ language structure description of the chain stack

The code is as follows (example):

NOTE: I'm using a singly linked list without a head node

typedef struct LinkNode{     int data;//data field     struct LinkNode *next;//pointer field  }stackNode,*LinkStack; 


3. Realization of basic operations in the chain stack 

3.1 Initialization of the chain stack

No need for new, because I am a singly linked list without a head node

void initStack(LinkStack &s)
{
	s=NULL;//不需要头节点 
}

3.2 Determine whether the chain stack is empty 

When s==NULL, the stack is empty, then return 1; otherwise, return 0 

int stackEmpty(LinkStack s)
{
	if(s==NULL)
		return 1;
	return 0;
}

3.3 Find the length of the chain stack 

The length indicates how many nodes there are

int stackLength(LinkStack s)
{
	int sum=0;
	stackNode *temp=s;
	while(temp!=NULL)
	{
		sum++;
		temp=temp->next;
	}
	return sum;
}

3.4 Pushing the chain stack into the stack

p is the new node

The key point is that when the stack is empty, p is the first node; when the stack is not empty, let the next pointer of p point to s, and s is updated to the p node, which is equivalent to letting p be the first node node

void push(LinkStack &s,int e)
{
	stackNode *p=new stackNode;
	p->data=e;
	p->next=NULL;
	if(s==NULL)
		s=p;
	else
	{
		p->next=s;
		s=p;
	}
}

3.4 Popping the chain stack

When the stack is empty, it cannot be popped

new a p node

And when the stack is not empty, let p point to the first node of s, update s, make s point to the next node, and delete p

void pop(LinkStack &s,int &e)
{
	stackNode *p=new stackNode;
	if(s==NULL)
	{
		cout<<"栈为空,无法弹出"<<endl;
	}
	else
	{
		p=s;
		e=p->data;
		s=s->next;
		delete p;
		cout<<"成功弹出栈顶元素"<<endl;
	}
}

3.5 Find the top element of the stack 

 When the stack is not empty, return the data of the first node

int top(LinkStack s)
{
	if(s==NULL)
		return -1;
	return s->data;
}

3.6 Destroy the stack

void DestoryStack(LinkStack &S)
{
	stackNode *p;
	while(S)
	{
		p=S;
		S=S->next;
		delete p;
	}
	S=NULL;
	cout<<"成功销毁"<<endl;
}

4. The specific implementation of the chain stack 

#include <iostream>
using namespace std;
//不带头节点的 
typedef struct LinkNode{
	int data;//数据域
	struct LinkNode *next;//指针域 
}stackNode,*LinkStack;
void initStack(LinkStack &s)
{
	s=NULL;//不需要头节点 
}
int stackEmpty(LinkStack s)
{
	if(s==NULL)
		return 1;
	return 0;
}
int stackLength(LinkStack s)
{
	int sum=0;
	stackNode *temp=s;
	while(temp!=NULL)
	{
		sum++;
		temp=temp->next;
	}
	return sum;
}
void push(LinkStack &s,int e)
{
	stackNode *p=new stackNode;
	p->data=e;
	p->next=NULL;
	if(s==NULL)
		s=p;
	else
	{
		p->next=s;
		s=p;
	}
}
void pop(LinkStack &s,int &e)
{
	stackNode *p=new stackNode;
	if(s==NULL)
	{
		cout<<"栈为空,无法弹出"<<endl;
	}
	else
	{
		p=s;
		e=p->data;
		s=s->next;
		delete p;
		cout<<"成功弹出栈顶元素"<<endl;
	}
}
int top(LinkStack s)
{
	if(s==NULL)
		return -1;
	return s->data;
}

//销毁栈 
//所有节点
void DestoryStack(LinkStack &S)
{
	stackNode *p;
	while(S)
	{
		p=S;
		S=S->next;
		delete p;
	}
	S=NULL;
	cout<<"成功销毁"<<endl;
}

void menu()
{
	cout<<"**************************"<<endl;
	cout<<"1.初始化"<<endl;
	cout<<"2.判断栈是否为空"<<endl;
	cout<<"3.求栈的长度"<<endl;
	cout<<"4.销毁栈"<<endl;
	cout<<"5.入栈"<<endl;
	cout<<"6.出栈"<<endl;
	cout<<"7.求栈顶元素"<<endl;
	cout<<"8.退出"<<endl;
	cout<<"**************************"<<endl;
}
int main()
{
	int choice;
	LinkStack s;
	int e1,e2;
	while(1)
	{
		menu();
		cin>>choice;
		switch(choice)
		{
			case 1:
				initStack(s);
				cout<<"初始化成功"<<endl;
				break;
			case 2:
				if(stackEmpty(s))
					cout<<"栈为空"<<endl;
				else
					cout<<"栈不为空"<<endl; 
				break;
			case 3:
				cout<<"栈的长度为"<<stackLength(s)<<endl;
				break;
			case 4:
				DestoryStack(s);
				break;
			case 5:
				cout<<"请输入想要入栈的元素值:"<<endl;
				cin>>e1;
				push(s,e1);
				cout<<"入栈成功"<<endl; 
				break;	
			case 6:
				pop(s,e2);
				cout<<"弹出的元素为"<<e2<<endl;
				break;
			case 7:
				if(top(s)!=-1)
					cout<<"栈顶元素为"<<top(s)<<endl;
				else
					cout<<"栈为空"<<endl;
				break;
			case 8:
				cout<<"成功退出"<<endl;
				exit(0);
			default:
				cout<<"输入有误,请重新输入"<<endl;
				break;			
		}	
	}
}

5. Test results

        Figure 1

 

        Figure II 

 

         Figure three

        Figure four

 

        Figure five

 

        Figure six

 

        Figure seven

6. Summary 

        The stack is a linear list with limited operations. Although the operation is limited, it is similar to the linear list, except that the insertion and deletion of the stack are at the end of the list. The chain stack I implemented has a lot to do with the linked list without the leading node. You can also refer to the linked list to learn the chain stack.

 

Guess you like

Origin blog.csdn.net/m0_53679998/article/details/129569593