Using single linked list to realize stack _c skill

Source: Internet
Author: User

Stack is a data structure that is limited to the operation of the tail, and in contrast to the queue, the stack is characterized by "cash out" and is therefore called LIFO.

Like queues, stacks also have linked lists and arrays of two implementations, each with the same pros and cons as the one mentioned in the introduction queue. The following describes the way to implement stacks using a linked list (chained stacks). The following is a schematic of the chain stack:

Because the stack is characterized by "advanced out", so we just need a pointer to the top of the stack, because either the insert or delete is for the top node, that is, we need to implement a "head node" will change the list, and each change can get the first node the latest address.

With this in mind, the basic stack and the corresponding operation can be realized simply by slightly improving on the basis of a single linked list.

The following is the use of a single linked list to implement the stack source:

#include <string.h> #include <malloc.h> #include <iostream> using namespace std;
    typedef struct stack{int data;
struct stack* nextnode;

}stack_t;

static stack_t* s; void Stack_init (stack_t** top) {/*/} bool Stack_push (stack_t** top, int data) {stack_t* node = (stack_t*)
    malloc (sizeof (stack_t));   
    if (!node) {return false;
    } node->data = data;
    Node->nextnode = *top;
    *top = node;
return true;

BOOL IsEmpty (stack_t* top) {return (top = NULL);}
    BOOL Stack_pop (stack_t** top) {if (IsEmpty (*top)) {return false;
    } stack_t* tmp = *top;
    *top = (*top)->nextnode;  
    if (TMP) {free (TMP); 
return true;
    } void Printstackdata (stack_t* top) {stack_t* tmp = top;
        while (TMP) {cout << tmp->data << "";
    TMP = tmp->nextnode;
} cout << Endl;
    } void Stack_deinit (stack_t** top) {stack_t* tmp = *top; WhileTMP) {cout << "free node, data is:" << tmp->data << Endl;
        Free (TMP);
        *top = (*top)->nextnode;
    TMP =*top;
    } bool Topvalue (stack_t* top, int& val) {if (IsEmpty (top)) {return false;
    } val = top->data;
return true;
    int main () {stack_t* s = NULL;
    Stack_push (&s,1);
    Stack_push (&s,2);
    int Val; 
    if (Topvalue (S, Val)) cout << "Now top value of the stack is:" << val << Endl;
    Stack_push (&s,3);

    Printstackdata (s);
    Stack_pop (&s);

    Printstackdata (s);
    Stack_push (&s,100);
    Stack_push (&s,201);
    if (Topvalue (S, Val)) cout << "Now top value of the stack is:" << val << Endl;
    Stack_push (&s,303);

    Printstackdata (s);
    Stack_deinit (&s);

    Printstackdata (s);

 cout << "Stack Empty" << isempty (s) << Endl;

} Go from: http://blog.csdn.net/keheinash/article/details/51163932

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.