Article 84 - Data Structure - How to Implement Stack in C

This article describes an implementation of the Stack Structure.

For information about the Stack Structure, see Article 31 - Programming Data Structures.

To program a Stack, we need to understand the interface of a Stack.

Interface

  • Push Function
  • Pop Function
  • Is Empty Function

Implementation

In this implementation, we use the Node structure from the Link List Article, see Article 83 - Data Structure - How to Implement Linked List in C.

Push Function

The Push Function adds an item to the Stack, the item goes on top of the Stack. We can use a linked list to implement a Stack and append/remove from the head of the list.

For information about the Linked List Structure, see Article 83 - Data Structure - How to Implement Linked List in C.

A function to push a node onto the Stack.

int push( Node ** stack, int content ){
    Node * node;
    
    if( !stack ){
        return -1;
    }

    node = newNode( content );

    if( !node ){
        return -2;
    }

    return addNode( stack, node );
}

Pop Function

Simply remove the node at the head of the list and return it, or return error if the list is empty.

A function to pop a node from the Stack.

int pop( Node ** stack, int * content ){   
    if( !stack || !content ){
        return -1;
    }   

    if( !*stack ){
        return -2;
    }

    *content = (*stack)->content;

    return removeNode( stack, *stack );
}

Is Empty Function

Checking if the list is empty or null will determine if the Stack contains anything or not.

A function determine if the Stack is empty.

int isEmpty( Node * stack ){
    if( !stack ){
        return -1;
    }

    if( !*stack ){
        return 0;
    }

    return 1;
}

Comments (12)

Posted by anonymous - Gaming Machine at Tuesday, December 25, 2012 5:20 PM

Works like a charm.

Posted by anonymous - cheap oakley sunglasses at Saturday, December 29, 2012 1:20 AM

Quite comfy layer. Appears to be like greater than on picture. Extends true to measurement. We have bought M, cause I figured it is small((, nonetheless really like it!
[url=http://cheapsunglassol.webs.com/]cheap oakley sunglasses[/url]
<a href="http://cheapsunglassol.webs.com/" title="cheap oakley sunglasses">cheap oakley sunglasses</a>

Posted by anonymous - Pravin at Monday, December 31, 2012 6:24 PM

Oakley only does close outs to the Vault stores and to coenipams like Steep and cheap. Unless you're setup as an Oakley distributor, you won't be able to get glasses at below wholesale. If you're looking to purchase large quantities for resale- get yourself setup as a licensed dealer.If you're just looking for a few pairs for your own personal use, go to an Oakley Vault (look in the DE case), steepandcheap.com, Craigslist or ebay.HTH,D

Posted by anonymous - ?????? ?? at Friday, January 25, 2013 12:21 PM

Wanna try online shopping? Here are stylish but gorgeous miumiu ?? in perfect quality.
?????? ?? http://miumiusale5.webnode.jp/

Posted by anonymous - ?????? at Friday, January 25, 2013 4:04 PM

Get ready to find some exciting new fashion (?????? ???) for yourself.
?????? http://www.monclersaleinfo.jp/

Posted by anonymous - ??? ?? at Sunday, February 3, 2013 2:20 AM

find miumiu,chloe,prada is too many type and you may have no idea about such many type. ??? ?? http://www.chloe-mall.net/

Posted by anonymous - buy viagra online at Saturday, March 2, 2013 3:13 PM

raBGTG Hey, thanks for the article post.Much thanks again. Fantastic.

Posted by anonymous - bookmaring service at Friday, March 15, 2013 12:27 AM

sLq01Q I cannot thank you enough for the blog article.Really thank you! Keep writing.

Posted by anonymous - buy cipro at Friday, March 15, 2013 10:56 AM

A round of applause for your blog.Thanks Again. Cool.

Posted by anonymous - generic viagra online at Friday, March 15, 2013 12:34 PM

Major thankies for the post. Really Cool.

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.


Vantasy World Copyright 2011 - 2017. Vantasy World is a project developed by Vantasy Online. Privacy Policy.