In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use C language to implement chain stack. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The basic concept of stack
The stack is a table structure that can only add or delete elements at one end. The basic operations of the stack at this position, called top stack, are push and pop, the former is equivalent to insert, while the latter is to delete the last inserted element. It is considered an error that the last inserted element can be accessed by the outside world as the top of the stack before being popped up, or pushed into the full stack.
The common stacks are sequential stack and chain stack.
Sequential stack
Chain stack
-C code implementation of chain stack # include # include / * structure of nodes * / typedef struct node {struct node* pnode; int data;} structure of node_t;/* stack * / typedef struct stack {number of data in int size;// stack with pointer at the top of struct node* top;// stack} stack_t;/* initialization stack * / void stack_init (stack_t* stk) {stk- > top = NULL; stk- > size = 0 } / * Stack operation * / void stack_push (stack_t* stk, int data) {node_t * node = malloc (sizeof (node_t)); node- > data = data; node- > pnode = stk- > top; stk- > top = node; stk- > size++;} / * bullet stack: pop the stack data into buf*/void stack_pop (stack_t* stk, int buf [], int size) {for (int I = 0; I)
< size; ++i) { if(stk->Size = = 0) {printf ("the data in the stack has bounced clean! Break;} node_t* temp = stk- > top; buf [I] = stk- > top- > data; stk- > top = stk- > top- > pnode; stk- > size--; free (temp) }} / * Delete the entire stack * / void stack_deinit (stack_t* stk) {while (stk- > size | | stk- > top) {node_t* temp = stk- > top; stk- > top = stk- > top- > pnode; stk- > size--; free (temp) }} / * print all data in the stack from top to bottom * / void print_stack (stack_t* stk) {if (stk- > size = = 0) {printf ("there is no data in the stack! For (node_t* node = stk- > top; node; node = node- > pnode) {printf ("% d", node- > data);} printf ("\ n");} / * Test Code * / # define N 30int main (void) {stack_t stack; int buf [N]; stack_init (& stack); printf ("start pressing the stack! \ n "); for (int I = 0; I
< N; ++i) { stack_push(&stack, i); } print_stack(&stack);//打印栈中数据 //stack_deinit(&stack); printf("开始弹栈!\n"); stack_pop(&stack, buf, N);//弹栈 print_stack(&stack); printf("取出的数据为:"); for(int i = 0; i < sizeof(buf) / sizeof(buf[0]); ++i) { printf("%d ", buf[i]); } printf("\n"); return 0;}代码运行效果Thank you for reading! This is the end of the article on "how to use C language to realize chain stack". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.