用链表实现的栈

 

     链表是相当重要的数据结构,对于链表的操作,只有多多练习才会有感觉。以下是我用链表实现的栈:

 

 

/*

 *File: stacklist.h

 *

 */

 

 

#ifndef _STACKLIST_H_
#define _STACKLIST_H_

typedef struct _StackNode{
    int         data;
    struct  _StackNode     *prev;
}StackNode, *PStackNode;            //the type of the stack data


typedef struct _StackList{
    PStackNode      top;        //the top of stack
}StackList, *PStackList;

typedef void(*FunVisit)(int);

PStackList  create_stack_list();
int     destory_stack_list(PStackList*);
int     push_stack_list(PStackList, int);
int     pop_stack_list(PStackList,int*);
void        visit_stack_list(PStackList, FunVisit);

#endif
 

 

 

 

/*
 *File    : stacklist.c
 *Author  : hui
 *created : 2010-7-25
 *Modified:
 */

/******************************************
 * There are the main funtion of the stack!
 *
 ******************************************/

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

#include "stacklist.h"

/**********************************************
 * Create a stack node and destory the node
 * static:           Just use in this file.
 **********************************************/

static PStackNode create_stack_node(int value)
{
    PStackNode pnode = NULL;
   
    pnode = (PStackNode)malloc(sizeof(StackNode));
   
    if(pnode == NULL)
        return NULL;

    pnode->data = value;
    pnode->prev = NULL;

    return pnode;
   
}

static int destory_stack_node(PStackNode *pnode)
{
    if(pnode == NULL)
    {
        return -1;
    }

    free(*pnode);
    *pnode = NULL;

    return 0;
}

/**********************************************
 * ===============    End   ===================
 * Create a stack node and destory the node
 * static:           Just use in this file.
 **********************************************/



/**********************************************
 * Create a stack list and destory the list
 *
 **********************************************/

PStackList create_stack_list()
{
    PStackList L = NULL;
   
    if((L = (PStackList)malloc(sizeof(StackList))) == NULL)
    {
        printf("/nError malloc !/n");
        exit(-1);
    }
   
    L->top = NULL;

    return L;
}

int destory_stack_list(PStackList *plist)
{
    PStackNode pnode = NULL;

    if(plist == NULL)   return -1;

    

    //栈里元素的释放
    while((*plist)->top != NULL)
    {
        pnode = ((*plist)->top)->prev;
        destory_stack_node(&((*plist)->top));      
        (*plist)->top = pnode;
    }
    

    //栈的释放,这里即使对top占的内存空间的释放
    free(*plist);              
    *plist = NULL;

    return 0;
}

/**********************************************
 * ================== End =====================
 * Create a stack list and destory the list
 *
 **********************************************/


/*push a data to the stack list*/
int push_stack_list(PStackList plist, int value)
{
    PStackNode pnode;
   
    if(plist == NULL)
    {
        printf("In the push: plist is NULL!/n");
        return -1;
    }

    if((pnode = create_stack_node(value)) == NULL)
    {
        printf("Create a node Error!/n");
        return -1;
    }

    if(plist->top == NULL)
    {
        plist->top = pnode;
    }
    else
    {
        pnode->prev = plist->top;
        plist->top = pnode;
    }

    return 0;
}

/*pop a data from the stack list*/
int pop_stack_list(PStackList plist, int *gvalue)
{
    PStackNode pnode;

    if(plist == NULL)
    {
        printf("In the pop : plist is NULL!/n");
        return -1;
    }

    *gvalue = (plist->top)->data;

    //退栈要释放栈顶的元素
    pnode = (plist->top)->prev;
    destory_stack_node(&(plist->top));
    plist->top = pnode;

    return 0;
}

/*visit the data of the stack list*/
void visit_stack_list(PStackList plist, FunVisit fun)
{
    int value;

    if(plist == NULL)
    {
        printf("The stack is empty!/n");
        return ;
    }

    while(plist->top != NULL)
    {
        pop_stack_list(plist, &value);
        fun(value);
    }

}
 

 

以上代码是我在课余时间实现的,可能显得很粗糙,若有什么不足或不对的地方,欢迎指出!

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值