Chained physical structure implementation of stack

link_stack.h
#ifndef __LINK_STACK_H_
#define __LINK_STACK_H_

typedef struct node{
	int num;
	struct node* p_next;
}node;
typedef struct  
{
	node head,tail;//head node, tail node
}stack;

//Initialize the stack
void stack_init(stack *);

//clean up the storage area of ​​the station
void stack_deinit (stack *);

// Determine if the stack is full
int stack_full(const stack *);

//Check if the stack is empty
int stack_empty(const stack *);

//Get the number of valid digits in the stack
int stack_size(stack *);

// put a number on the stack
void stack_push(stack *,int);

// get the next number from the stack
int stack_pop(stack *);

// get the next number from the stack
int stack_top(stack *);
#endif
link_stack.cpp
 
  
p_node = p_node->p_next) { node* p_first = p_node; node* p_mid = p_first->p_next; node* p_last = p_mid->p_next; if(p_mid != &(p_stack->tail))//valid node is not When the tail node is { size++; } } return size; } //Put a number on the stack void stack_push(stack *p_stack,int num) { node *p_node = NULL,*p_tmp = NULL; p_tmp = (node*)malloc( sizeof(node)); if (!p_tmp) { return; } p_tmp->num = num; for (p_node = &(p_stack->head);p_node != &(p_stack->tail);p_node = p_node-> p_next) { node* p_first = p_node; node* p_mid = p_first->p_next; node* p_last = p_mid->p_next; if (p_mid == &(p_stack->tail))//The valid node is equal to the tail node, in the tail Insert at the node. { p_first->p_next = p_tmp; p_tmp->p_next = p_mid; break; } } } //Get the next number from the stack int stack_pop(stack *p_stack) { node* p_node = NULL; int num = 0; for ( p_node = &(p_stack->head);p_node != &(p_stack->tail);p_node = p_node->
main.cpp
#include "stdio.h"
#include "link_stack.h"
int main()
{
	stack stk = {0};
	stack_init(&stk);
	stack_push(&stk,1);
	stack_push(&stk,3);
	stack_push(&stk,5);
	stack_push(&stk,7);
	stack_push(&stk,25);
	printf("数字个数是:%d\n",stack_size(&stk));
	printf("判断的结果是:%d\n",stack_full(&stk));
	printf("top结果是:%d\n",stack_top(&stk));

	printf("pop结果是:%d\n",stack_pop(&stk));
	printf("pop结果是:%d\n",stack_pop(&stk));
	printf("pop结果是:%d\n",stack_pop(&stk));
	printf("pop结果是:%d\n",stack_pop(&stk));
	printf("pop结果是:%d\n",stack_pop(&stk));

	printf("判断空的结果是:%d\n",stack_empty(&stk));
	stack_deinit(&stk);
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325625894&siteId=291194637