C++ Data Structure X _08_C++ Realizes Sequential Storage and Chained Storage of Stack

This article refers to C++ to realize the sequential storage and chain storage of the stack . First understand the structural framework, and then use c to implement the content according to the video, and it can also have a higher improvement on c.

The stack is a special data structure. The data in the stack is first out, and the data in the stack can only be popped from the head. The data that can be directly accessed is only the head data of the stack. If you want to access the following data, you need to pop the previous data out of the stack one by one. The following is an example of word revocation:
insert image description here
when we use word to write paper, we first need to create a blank document (that is, an empty stack), and then perform a series of operations on this blank document. Each operation is a new version, that is, the operation of pushing data to the stack. But after the paper was written (version4), we found that there was a problem with the written paper and needed to be modified. If we wanted to return to version1, we used the undo operation. But because word can only directly access the latest version, we can't go back to version1 directly. We need to go back to version3, then back to version2, and finally back to version1. This access method is to access top by popping the stack.

The basic theory of the stack is introduced here, and the implementation code of the two storage structures of the stack is attached below.

1. Sequential storage of the stack

#include <iostream>
using namespace std;
const int max_size=1024;
//创建栈
class seqstack
{
    
    
public:
	void* data[max_size];  //可以指向任何类型数据的指针
	int size;
};
//初始化栈
seqstack* init_seqstack()
{
    
    
	seqstack* stack =new seqstack;
	for (int i = 0; i < max_size; i++)
	{
    
    
		stack->data[i]=NULL;
	}
	stack->size=0;
	return stack;
}
//入栈操作
void push_seqstack(seqstack* stack,void* data)
{
    
    
	if (stack->size==max_size)
	{
    
    
		cout<<"数据已满"<<endl;
		return;
	}
	stack->data[stack->size]=data;
	stack->size++;
}
//返回栈顶元素
void* top_seq_stack(seqstack* stack)
{
    
    
	return stack->data[stack->size-1];
}
//出栈
void pop_seqstack(seqstack* stack)
{
    
    
	stack->data[stack->size-1]=NULL;
	stack->size--;
}
int main()
{
    
    
	//创建栈
	seqstack* stack=init_seqstack();
	//创建数据
	int num[9]={
    
    1,2,3,4,5,6,7,8,9};
	//数据入栈
	for (int i = 0; i < 9; i++)
	{
    
    
		cout<<"第"<<i+1<<"个入栈数据为:"<<num[i]<<endl;
		push_seqstack(stack,&num[i]);
	}
	cout<<endl;
	//逐个出栈并访问
	for (int i = 0; i < 9; i++)
	{
    
    
		cout<<"第"<<i+1<<"个出栈数据为:"<<*(int *)(top_seq_stack(stack))<<endl;  //将出栈的地址强转成int*再解指针
		pop_seqstack(stack);
	}
	system("pause");
	return 0;
}

operation result:
insert image description here

2. Stack chain storage

#include <iostream>
#include <string>
using namespace std;
//节点
class linknode
{
    
    
public:
	linknode* next;
};
//自定义数据
class my_data
{
    
    
public:
	linknode* node;
	char data;
};
//链式栈
class linkstack
{
    
    
public:
	linknode head;
	int size;
};
//初始化栈
linkstack* init_linkstack()
{
    
    
	linkstack* stack=new linkstack;
	stack->head.next=NULL;
	stack->size=0;
	return stack;
}
//入栈
void push_linkstack(linkstack* stack,linknode* data)
{
    
    
	data->next=stack->head.next;
	stack->head.next=data;
	stack->size++;
}
//出栈
void pop_linkstack(linkstack* stack)
{
    
    
	stack->head.next=stack->head.next->next;
	stack->size--;
}
//返回栈顶元素
linknode* top_linkstack(linkstack* stack)
{
    
    
	return stack->head.next;
}
 
int main()
{
    
    
	//创建空栈
	linkstack* stack=init_linkstack();
	//创建数据
	string str="ABCDEFGHI";
	my_data data[9];
	for (int i = 0; i < str.size(); i++)
	{
    
    
		data[i].data=str[i];
		data[i].node=NULL;
	}
	//入栈
	for (int i = 0; i < str.size(); i++)
	{
    
    
		cout<<"第"<<i+1<<"个元素入栈:"<<str[i]<<endl;
		push_linkstack(stack,(linknode*)&data[i]);
	}
	cout<<endl;
	//出栈
	for (int i = 0; i < 9; i++)
	{
    
    
		cout<<"第"<<i+1<<"个元素出栈:"<<(((my_data*)top_linkstack(stack))->data)<<endl;
		pop_linkstack(stack);
	}
	system("pause");
	return 0;
}

The result of the operation is:
insert image description here

Guess you like

Origin blog.csdn.net/Dasis/article/details/131649587