Stack Array Implementation file C Language

Source: Internet
Author: User

/* Stack. C -- stack implementation file */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include "stack. H "</P> <p>/* interface function definition */</P> <p> int initialize_s (stack * const pstack, const int capacity) <br/> {<br/> If (capacity <= 0) <br/> return 0; <br/> * pstack = (struct stack *) malloc (sizeof (struct stack); <br/> If (null = * pstack) <br/> return 0; <br/> (* pstack) -> array = (stack_item *) malloc (sizeof (stack_item) * capacity); <br/> If (null = (* pstack)-> array) <br/>{< br/> free (* pstack); <br/> return 0; <br/>}< br/> (* pstack) -> capacity = capacity; <br/> (* pstack)-> Top = empty_top; </P> <p> return 1; <br/>}</P> <p> int isempty_s (const stack * const pstack) <br/>{< br/> return empty_top = (* pstack) -> top; <br/>}</P> <p> int isfull_s (const stack * const pstack) <br/>{< br/> return (* pstack) -> top + 2> (* pstack)-> capacity-1; <br/>}</P> <p> int push_s (const stack * const pstack, const stack_item ST1, const stack_item st2) <br/>{< br/> If (isfull_s (pstack) <br/> return 0; <br/> (* pstack) -> array [++ (* pstack)-> top] = ST1; <br/> (* pstack)-> array [++ (* pstack) -> top] = st2; </P> <p> return 1; <br/>}</P> <p> int pop_s (const stack * const pstack, stack_item * const pst1, stack_item * const pst2) <br/>{< br/> If (isempty_s (pstack) <br/> return 0; <br/> * pst2 = (* pstack)-> array [(* pstack)-> top --]; <br/> * pst1 = (* pstack) -> array [(* pstack)-> top --]; </P> <p> return 1; <br/>}</P> <p> void release_s (const stack * const pstack) <br/>{< br/> free (* pstack) -> array); <br/> free (* pstack); <br/>}

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.