Stack chain Implementation 1

Source: Internet
Author: User

This section provides a testCodeAnd description.

 
Int main (void) {# define max_sizemax_node # define val_range1_int I, Val; stack * pstack = NULL; printf ("Enter % s: \ n", _ FUNC __); pstack = create (); If (pstack = NULL) {perror ("create stack failed! \ N "); Return-1;} printf (" \ npush data: \ n "); for (I = 0; I <max_size; I ++) {if (I! = 0 & I % line_num = 0) printf ("\ n"); printf ("% d \ t", I); push (pstack, & I );} # If 0 printf ("\ n \ ndump stack % d Data: \ n", pstack-> size); dump_stack (pstack ); # endif printf ("\ n \ NPOP data: \ n"); for (I = 0; I <max_size; I ++) {if (I! = 0 & I % line_num = 0) printf ("\ n"); If (! Pop (pstack, & Val) printf ("% d \ t", Val); elsebreak;} printf ("\ n \ ndump stack data: \ n "); dump_stack (pstack); // destory the stack; destory_stack (pstack); printf ("\ nmain end! \ N "); Return 0 ;}

Stack implementation is relatively simple. Generally, it can be implemented using arrays or linked lists. The advantage of array implementation is that the address of each element is continuous, but the disadvantage is that the number of nodes in the stack must be known first; the stack implemented by chain can dynamically increase, you can flexibly define the data field type (void * Data). In actual use, you only need to specify the required type for the "Void *" type.

Full code: Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.