链式栈

#include <iostream>
using namespace std;


typedef struct listnode
{
    int data;
    struct listnode *next;
}LIST_NODE;


typedef struct list
{
    LIST_NODE* top;
}LIST;


LIST* create_list()
{
    LIST* list = new (LIST);
    list->top = NULL;
    return list;
}

LIST_NODE* create_node(int data, LIST_NODE* next)
{
    LIST_NODE* node = new (LIST_NODE);
    node->data =data;
    node->next = next;
    return node;
}


void list_push(LIST* list, int data)
{
    list->top = create_node(data,list->top);
}


LIST_NODE* destroy_node(LIST_NODE* node)
{
    LIST_NODE* next = node->next;
    delete(node);
    return next;
}


int list_pop(LIST* list)
{
    int data=list->top->data;
    list->top = destroy_node(list->top);
    return data;
}
void destroy_list(LIST* list)
{
    while(list->top)
    {
        list->top = destroy_node(list->top);
    }
   delete list;
}


bool empty_list(LIST* list)
{
    if(NULL==list->top)
    {
        return true;
    }
    else
    {
        return false;
    }
}


bool full_list(LIST* list)
{
    return false;
}


int main()
{
    LIST* list = create_list();
    for(int i=10;i<=100;i+=10)
    {
        list_push(list,i);
    }
    while(! empty_list(list))
    {
        cout<<list_pop(list)<<endl;
    }


    destroy_list(list);
    return 0;
}

posted @ 2017-03-02 09:25  sowhat1412  阅读(62)  评论(0编辑  收藏  举报