Talk C Chestnut Bar (17th time: C-language example-Stack II)

Source: Internet
Author: User


Crossing, everyone, from today onwards, we talk about the large-scale couplet science fiction: C Chestnut, that is, C language examples. Gossip Hugh,

Words return to the positive turn. Let's talk C chestnuts together!


Crossing, last time we were talking about stacks and features and basic operations, and finally implemented the stack through sequential storage, which we continue to

Say stack, but this time we are talking about the chain storage of the stack.


The chain-store of stacks is implemented in code through a doubly linked list. The stack operation is carried out in the direction of the header to the end of the table, and the stack operation coincides with its

(Just use it as an example of a doubly linked list). The node of the stack can be seen as a node in the list, and the operation of the stack can be seen.

Insert or delete a node operation in a linked list. Only insert or delete to follow the stack "advanced out" characteristics. Type of stack

Adds a size member that allows you to easily derive the length of the stack. Compared to the sequential storage of the stack, there is one more destruction stack

Function. Because the space in the stack is dynamically allocated, each time the stack operation allocates a chunk of memory space, in contrast to each stack

The operation will release the memory space. However, in the actual program into the stack and out of the stack is not a pair of occurrences, that is, if the stack is finished using

, a memory leak is caused by not releasing the dynamic space through a stack operation. So I added the ability to destroy stacks to facilitate

Whether the dynamically allocated space in the final check stack of the program is released.


Stack of chain storage and stack of sequential storage, the biggest advantage is not to know the length of the stack, as long as the memory space is large enough to

Store enough elements in the stack. However, it also has the disadvantage that the stack and stack operations are complex and inefficient. In short, the real

If you know the length of the stack in advance, you can use the order of the stack to store it, and if you don't know the length of the stack beforehand, you can use

Stack of chained storage, so more flexible.


Crossing, the text does not write code, the detailed code put in my resources, you can click the link to download the use.


You crossing, we're going to talk about the stack here. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (17th time: C-language example-Stack II)

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.