链表是相当重要的数据结构,对于链表的操作,只有多多练习才会有感觉。以下是我用链表实现的栈:
/*
*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);
}
}
以上代码是我在课余时间实现的,可能显得很粗糙,若有什么不足或不对的地方,欢迎指出!