当前位置:网站首页>Chain storage of stack

Chain storage of stack

2022-07-07 05:59:00 Wukong doesn't buy vegetables anymore

We talked about sequential storage before , make a concrete analysis , Look at the sequential storage of stacks , Now let's talk about the operation with linked list :

When storing sequentially , I use a cursor top_of_index To control top Corner marker , So how to control the top of the stack in a single linked list ,

Just keep plugging in , Head inserting is inserting at the top of the stack .

What about the stack , The stack is nothing more than to operate from scratch , Because the head is the top of the stack , We will destroy this node directly ( The premise is that this node allocates memory space ) Or the next node that points to the first node is ok 了 .

in addition , About the insertion and deletion of single linked list , Please see the design of single linked list , In addition, for some common operations and sequential storage, please see my other article .

In a word? , The general design idea is just four words : Start from scratch .

Don't talk much , Code up :

linkstack.h

  1 #ifndef _LINKLIST_H_
  2 #define _LINKLIST_H_
  3 
  4 // The element type is still controlled in this 
  5 typedef int element_type;
  6 
  7 typedef struct _t_link_stack link_stack;
  8 
  9 struct _t_link_stack{
 10         link_stack *next;// Pointer to the next node 
 11         element_type value;
 12 };
 13 
 14 link_stack *create_stack();
 15 int is_empty(link_stack *t_stack);
 16 void destroy_stack(link_stack *t_stack);
 17 element_type top_stack(link_stack *t_stack);
 18 void pop_stack(link_stack *t_stack);
 19 void push_stack(link_stack *t_stack,element_type value);

linkstack.c

#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"

// Create a stack 
link_stack *create_stack()
{
	link_stack *t_stack = (link_stack*)malloc(sizeof(link_stack));
	if(t_stack != NULL) {
		t_stack->next = NULL;
		t_stack->value = 0;// This can represent the number of elements in the stack 
	}
	return t_stack;
}
// Determine whether it is null 
int is_empty(link_stack *t_stack)
{
	int res = -1;
	if(t_stack != NULL) {
		res = (t_stack->next == NULL);
	}
	return res;
}
// Destroy the stack 
void destroy_stack(link_stack *t_stack) 
{
	// Pay attention to a problem , Stack destroyed , Can you traverse it , You can't , The stack can only be top Top of stack operations , therefore 
	if(t_stack != NULL) {
		free(t_stack);
	}
}
// Get the element at the top of the stack 
element_type top_stack(link_stack *t_stack)
{
	// The top of the stack is the first element pointed to by the header node 
	if(t_stack == NULL) {
		printf(" error \n");
	} else {
		return t_stack->next->value;
	}
}
void pop_stack(link_stack *t_stack)
{
	// Here's the stack , Let's make the head node point to the next node of the first node 
	if(t_stack != NULL && !is_empty(t_stack)) {
		link_stack *p_first = t_stack->next;
		t_stack->next = p_first->next;
		free(p_first);
		t_stack->value--;
	}
}
// insert data , When inserting data, remember to allocate corresponding memory space to the data 
void push_stack(link_stack *t_stack,element_type value)
{
	link_stack *p_node = (link_stack *)malloc(sizeof(link_stack));
	if(t_stack != NULL && p_node != NULL) {
		p_node->value = value;
		p_node->next = t_stack->next;
		t_stack->next = p_node;
		t_stack->value++;
	}
}

  Then look at the test file :

main.c

#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"

int main()
{
	link_stack *t_stack = create_stack();
	printf(" The number of elements in the stack %d\n",t_stack->value);
	element_type value1 = 1;
	element_type value2 = 2;
	element_type value3 = 3;
	// Into the stack 
	push_stack(t_stack,value1);
	push_stack(t_stack,value2);
	push_stack(t_stack,value3);

	printf(" The number of elements in the stack after insertion %d\n",t_stack->value);

	// Loop out the stack 
	while(!is_empty(t_stack)) {
		element_type value = top_stack(t_stack);
		printf("%d\n",value);
		pop_stack(t_stack);
	}
	
	printf(" Length after stack %d\n",t_stack->value);

	// Destroy the stack 
	destroy_stack(t_stack);

	return  0;
}

Running results :

 

原网站

版权声明
本文为[Wukong doesn't buy vegetables anymore]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130723083137.html