Data structure----chain stack

Table of contents

Preface

chain stack

Operation method 

1. Storage structure

2.Initialization

 3. Create nodes

 4. Determine whether the stack is full

 5. Determine whether the stack is empty

 6.Push to the stack

 7. Pop out

8. Get the top element of the stack

 9. Traverse the stack

 10. Clear the stack

Complete code


Preface

        We have learned the related methods of array stack before, (link: linear table-----stack (initialization, establishment, push, pop, traversal, clearing and other operations of the stack)_Helletade's blog-CSDN Blog ) So today we will start to learn a new structural stack---chained stack. As the name suggests, the related method operations of the stack are implemented through the structure of the linked list, including creation, judging whether it is empty or full, popping the stack, pushing into the stack, traversing and clearing, etc. Let’s take a look at the operation below!

chain stack

The diagram is as follows:

Operation method 

#include<stdio.h>
#include<stdlib.h>
#define Maxsize 20 //设置最大节点数量

create_node(ElemType data);//创建节点

stack_init(Stack* top);//初始化

isFull(Stack* top);//判断是否满栈

isEmpty(Stack* top);//判断是否空栈

push(Stack* top, ElemType data);//入栈

pop(Stack* top);//入栈

get_stacktop(Stack* top);//获取栈顶元素

show_stack(Stack* top);//遍历栈

clear_stack(Stack* top);//清空栈

1. Storage structure

What is implemented today is the chain storage of the stack, which is commonly known as "chain stack". Since a singly linked list has been implemented before, the principle of the linked storage of the stack is the same, except that the operation of the linked stack is limited - insertion and deletion can only be performed on the top of the stack! Without further ado, let’s take a look at the storage structure of the chain stack.

//数据类型
typedef struct datatype {
	int age;
	char name[10];
	int num;
}ElemType;
//节点
typedef struct node {
	ElemType data;
	struct node* next;
}Node;
//栈顶指示
typedef struct stack {
	int count;	//计数
	Node* point;
}Stack;

2.Initialization

Initialize the position pointed by the head pointer to NULL and the node count to 0.

//初始化
void stack_init(Stack* top) {
	top->count = 0;
	top->point = NULL;
}

 3. Create nodes

To create a node, create the node through a linked list, and then assign the data value to the node.

//创建节点
Node* create_node(ElemType data) {
	Node* new_node = (Node*)malloc(sizeof(Node));
	if (new_node) {
		new_node->data = data;
		new_node->next = NULL;
		return new_node;
	}
	else
	{
		printf("ERROR\n");
	}
}

 4. Determine whether the stack is full

To determine whether the stack is full, you only need to check whether the count reaches the maximum number of nodes at this time.

//判断是否满
int isFull(Stack* top) {
	if (top->count > Maxsize) {
		printf("The stack is full\n");
		return 1;
	}
	return 0;
}

 5. Determine whether the stack is empty

At this time, you only need to check whether the count is 0.

//判断是否为空
int isEmpty(Stack* top) {
	if (top->count == 0) {
		printf("The stack is empty\n");
		return 1;
	}
	return 0;
}

 6.Push to the stack

The operation of pushing onto the stack is similar to the chaining operation of a linked list, that is to say, just connect the created nodes. The difference is that every time a node is put in, the top pointer on the stack will move up to the top of the stack. When moving, the count should also be +1. The code is as follows:

//入栈
void push(Stack* top, ElemType data) {
	Node* new_node = create_node(data);
	if (new_node&&!isFull(top)) {
		top->count++;
		if (top->count == 1) {//如果入栈是第一个节点的话
			top->point = new_node;
		}
		else
		{
            //以下两个步骤不能调过来!
			new_node->next = top->point;
			top->point = new_node;
		}
	}
	else
		return;
}

 7. Pop out

When popping the stack, first obtain the position pop_node pointed by the top pointer of the stack at this time, then move the top pointer of the stack downward by one, decrement the count by one, and then return the element pop_node:

//出栈
Node* pop(Stack* top) {
	Node* pop_node=NULL;
	if (!isEmpty(top)) {
		pop_node = top->point;
		top->point = pop_node->next;
   		pop_node->next = NULL;
		top->count--;
	}
	return pop_node;
}

8. Get the top element of the stack

To get the top element of the stack, you don’t need to pop it, you just need to return the top element of the stack: 

//获取栈顶元素
Node* get_stacktop(Stack* top) {
	return top->point;
}

 9. Traverse the stack

Traverse the stack, starting from the top of the stack and traversing the output data downwards:

//遍历栈
void show_stack(Stack* top) {
	Node* cur = top->point;
	while (cur) {
		printf("%d %s %d\n", cur->data.age, cur->data.name, cur->data.num);
		cur = cur->next;
	}
	printf("Print over!\n");
}

 10. Clear the stack

To clear the stack, you need to release the space of each node in turn, and then move the top of the stack downward until it moves to the original position.

//清空栈
void clear_stack(Stack* top) {
	Node* cur;
	while (top->point) {
		cur = top->point;
		top->point = cur->next;
		free(cur);
	}
	printf("Clear successfully!\n");
}

Complete code

#include<stdio.h>
#include<stdlib.h>
#define Maxsize 20 //设置最大节点数量

//链表栈

//数据类型
typedef struct datatype {
	int age;
	char name[10];
	int num;
}ElemType;
//节点
typedef struct node {
	ElemType data;
	struct node* next;
}Node;
//栈顶指示
typedef struct stack {
	int count;	//计数
	Node* point;
}Stack;


//创建节点
Node* create_node(ElemType data) {
	Node* new_node = (Node*)malloc(sizeof(Node));
	if (new_node) {
		new_node->data = data;
		new_node->next = NULL;
		return new_node;
	}
	else
	{
		printf("ERROR\n");
	}
}

//初始化
void stack_init(Stack* top) {
	top->count = 0;
	top->point = NULL;
}


//判断是否满
int isFull(Stack* top) {
	if (top->count > Maxsize) {
		printf("The stack is full\n");
		return 1;
	}
	return 0;
}
//判断是否为空
int isEmpty(Stack* top) {
	if (top->count == 0) {
		printf("The stack is empty\n");
		return 1;
	}
	return 0;
}


//入栈
void push(Stack* top, ElemType data) {
	Node* new_node = create_node(data);
	if (new_node&&!isFull(top)) {
		top->count++;
		if (top->count == 1) {//如果入栈是第一个节点的话
			top->point = new_node;
		}
		else
		{
			new_node->next = top->point;
			top->point = new_node;
		}
	}
	else
		return;
}


//出栈
Node* pop(Stack* top) {
	Node* pop_node=NULL;
	if (!isEmpty(top)) {
		pop_node = top->point;
		top->point = pop_node->next;
		pop_node->next = NULL;
		top->count--;
	}
	return pop_node;
}

//获取栈顶元素
Node* get_stacktop(Stack* top) {
	return top->point;
}

//遍历栈
void show_stack(Stack* top) {
	Node* cur = top->point;
	while (cur) {
		printf("%d %s %d\n", cur->data.age, cur->data.name, cur->data.num);
		cur = cur->next;
	}
	printf("Print over!\n");
}

//清空栈
void clear_stack(Stack* top) {
	Node* cur;
	while (top->point) {
		cur = top->point;
		top->point = cur->next;
		free(cur);
	}
	printf("Clear successfully!\n");
}


int main() {

	Stack top;
	stack_init(&top);//初始化
	ElemType data[4] = { {15,"Jack",01},{16,"Leimu",02},{17,"Lamu",03},{18,"Ajx",04} };
	for (int i = 0; i < 4; i++) {
		push(&top, data[i]);//入栈操作
	}
	show_stack(&top);//遍历栈
	Node* out_data=pop(&top);//出栈操作
	printf("%d %s %d\n", out_data->data.age, out_data->data.name, out_data->data.num);
	clear_stack(&top);//清空栈
}

 The above is the content of this issue. If you like it, please give it a follow! See you next time!

Guess you like

Origin blog.csdn.net/m0_73633088/article/details/132919218