//stack.h #ifndef __STACK_H_ #define __STACK_H_ #define SIZE 5 typedef struct{ int arr[SIZE]; int size;//Number of significant digits }stack; //Initialize the memory area of the stack void stack_init(stack *); //clean up the stack memory 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(const stack *); // put a number on the stack void stack_push(stack *, int); // get next number from stack int stack_pop(stack *); // get the next number from the stack int stack_top(const stack *); #endif //__STACK_H_ //stack.cpp /* Sequential Physical Structure Stack Demonstration */ #include "stack.h" //Initialize the memory area of the stack void stack_init(stack *p_stack) { p_stack->size = 0; } //clean up the stack memory void stack_deinit(stack *p_stack) { p_stack->size = 0; } // Determine if the stack is full int stack_full(const stack *p_stack) { return p_stack->size == SIZE; } //Check if the stack is empty int stack_empty(const stack *p_stack) { return !(p_stack->size); } //Get the number of valid digits in the stack int stack_size(const stack *p_stack) { return p_stack->size; } // put a number on the stack void stack_push(stack *p_stack, int num) { p_stack->arr[p_stack->size] = num; p_stack->size++; } // get next number from stack int stack_pop(stack *p_stack) { p_stack->size--; return p_stack->arr[p_stack->size]; } // get the next number from the stack int stack_top(const stack *p_stack) { return p_stack->arr[p_stack->size - 1]; } //main.cpp /* stack test */ #include "stdio.h" #include "stack.h" intmain() { stack pcs = {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("Number of numbers is:%d\n",stack_size(&stk)); printf("The result of judging full is:%d\n",stack_full(&stk)); stack_top(&stk); printf("The top result is: %d\n",stack_top(&stk)); printf("The top result is: %d\n",stack_top(&stk)); printf("pop result is:%d\n",stack_pop(&stk)); printf("pop result is:%d\n",stack_pop(&stk)); printf("pop result is:%d\n",stack_pop(&stk)); printf("pop result is:%d\n",stack_pop(&stk)); printf("pop result is:%d\n",stack_pop(&stk)); printf("The result of judging empty is %d\n",stack_empty(&stk)); stack_deinit (& stk); return 0; }
The sequential storage structure of the stack is implemented.
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325284367&siteId=291194637
Recommended
Ranking