数据结构之栈的实现

利用双端链表实现栈的基本操作

栈的结构体及接口定义:

#ifndef STACK_H_
#define STACK_H_

#include "dlist.h"
#include "tools.h"

typedef struct Stack{
    Dlist *dlist;
}Stack;

Stack *init_stack(void);    //栈的初始化
void  destroy_stack(Stack **stack);   //栈的销毁
Boolean push_stack(Stack *stack, void *value);  //入栈
Boolean pop_stack(Stack *stack);  //出栈
Boolean is_empty(Stack *stack);   //判空
int get_stack_count(Stack *stack);  //得到栈的元素个数
Boolean get_stack_top(Stack *stack, void **value);  //得到栈顶元素


#endif

栈的接口实现:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "stack.h"
#include "tools.h"
#include "dlist.h"

Stack *init_stack(void)    //栈的初始化
{
     Stack *stack = NULL;
     stack = (Stack *)Malloc(sizeof(Stack));
     stack->dlist = init_dlist();
#if 0
     //不需要,程序退出时,stack会自动释放
     if(stack->dlist == NULL){
         free(stack);
         fprintf(stderr, "the memory is full!\n");
         exit(1);
     }
     bzero(stack->dlist, sizeof(Stack));
#endif
     return stack;
}

void  destroy_stack(Stack **stack)   //栈的销毁
{
    if(stack == NULL || *stack == NULL){
        return ;
    }
    destroy_dlist(&((*stack)->dlist));
    free(*stack);
    *stack = NULL;
}

Boolean push_stack(Stack *stack, void *value)  //入栈
{
    if(stack == NULL || value == NULL){
        return FALSE;
    }
    push_front(stack->dlist, value);
}

Boolean pop_stack(Stack *stack)  //出栈
{
    if(stack == NULL || is_empty(stack)){
        return FALSE;
    }
    return pop_front(stack->dlist);
}

Boolean is_empty(Stack *stack)   //判空
{
    return stack->dlist->count == ZERO;
}

int get_stack_count(Stack *stack)  //得到栈的元素个数
{
    if(stack == NULL){
        return -1;
    }
    return get_dlist_count(stack->dlist);
}

Boolean get_stack_top(Stack *stack, void **value)  //得到栈顶元素
{
    if(stack == NULL || value == NULL){
        return FALSE;
    }
    if(value != NULL){
        return get_front(stack->dlist, value);
    }
    return FALSE;
}

实现栈所需的包裹函数和双端链表的接口及其实现:

包裹函数:

#ifndef _TOOLS_H_
#define _TOOLS_H_

//定义布尔类型
#define TRUE (1)
#define FALSE (0)

typedef unsigned char Boolean;

//定义接口
void *Malloc(size_t size);

#endif

#include <stdio.h>
#include <stdlib.h>
#include "tools.h"

void *Malloc(size_t size)
{
    void *result = malloc(size);
    if(result == NULL){
        fprintf(stderr, "the memory is full!\n");
        exit(1);
    }

    return result;
}

栈所需的双端链表的接口及其实现:

#ifndef _DLIST_H_
#define _DLIST_H_
#include "tools.h"

#define ZERO   (0)
#define ONE    (1)

typedef void (*Print_func)(void *value);   //回调函数

//链表节点类型
typedef struct Dlist_node{
    struct Dlist_node *prev;  //前驱
    struct Dlist_node *next;  //后继
    void              *data;  //可以接收任意类型的指针(达到通用的效果)
}Dlist_node;

//链表控制信息
typedef struct Dlist{
    struct Dlist_node *head; //头结点
    struct Dlist_node *tail; //尾节点
    int               count; //数量

    //data所指向内容的释放策略
    void (*free)(void *ptr); //释放任意指针的空间(函数指针)
    //data所指向内容的相等策略
    Boolean (*match)(void *value1, void *value2);
    //data所指向内容的拷贝策略
    void *(*copy_node)(void *value);
}Dlist;

//通用链表的接口定义
Dlist   *init_dlist(void)                                       ;  //双端链表初始化
void    destroy_dlist(Dlist **dlist)                            ;  //双端链表的销毁
Boolean push_front(Dlist *dlist, void *value)                   ;  //头部插入
Boolean push_back(Dlist *dlist, void *value)                    ;  //尾部插入
Boolean pop_front(Dlist *dlist)                                 ;  //头部删除
Boolean pop_back(Dlist *dlist)                                  ;  //头部删除

Dlist_node *find_node(Dlist *dlist, void *value)                ;
Boolean insert_prev(Dlist *dlist, Dlist_node *node, void *value);  //插入到指定节点前边
Boolean insert_next(Dlist *dlist, Dlist_node *node, void *value);  //插入到指定节点后边
Boolean remove_dlist_node(Dlist *dlist, Dlist_node *node)       ;  //删除指定节点
void    print_dlist(Dlist *dlist, Print_func print)             ;  //链表的打印
Boolean get_front(Dlist *dlist, void **value)                   ;  //得到头结点的data
//Boolean get_front(Dlist *dlist, void *value) ; //得到头结点的data
Boolean get_tail(Dlist *dlist, void **value)                     ;  //得到尾节点的data
int     get_dlist_count(Dlist *dlist)                           ;   //得到链表的数量


#endif

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "dlist.h"

static Dlist_node *create_node(void);   //生成链表节点
static Dlist_node *create_node(void)
{
    Dlist_node *result = NULL;

    result = (Dlist_node *)Malloc(sizeof(Dlist_node));
    bzero(result, sizeof(Dlist_node));

    return result;
}

Dlist   *init_dlist(void)                                         //双端链表初始化
{
    Dlist *dlist = NULL;

    dlist = (Dlist *)Malloc(sizeof(Dlist));
    bzero(dlist, sizeof(Dlist));

    return dlist;
}


void    destroy_dlist(Dlist **dlist)                              //双端链表的销毁
{
    Dlist_node *p_node = NULL;

    if(dlist == NULL || *dlist == NULL){
        return ;
    }

#if 0
    p_node = (*dlist)->head;

    while((*dlist)->head != NULL){
        (*dlist)->head = p_node->next;
        if((*dlist)->free != NULL){
            (*dlist)->free(p_node->data);
        }
        free(p_node);
        p_node = (*dlist)->head;
    }
#endif

    while((*dlist)->count){
        pop_front(*dlist);
    }
    free(*dlist);
    *dlist = NULL;
}


Boolean push_front(Dlist *dlist, void *value)                     //头部插入
{
    //*value记录数据的地址
    Dlist_node *p_node = NULL;

    if(dlist == NULL || value == NULL){
        return FALSE;
    }

    //生成链表节点
    p_node = create_node();
    p_node->data = value;

    if(dlist->count == ZERO){     //插入前无元素
        dlist->head = dlist->tail = p_node;
    }else{      //插入前有元素
        p_node->next = dlist->head;
        dlist->head->prev = p_node;
        dlist->head = p_node;
    }

    dlist->count++;
    return TRUE;
}

Boolean push_back(Dlist *dlist, void *value)                      //尾部插入
{
    Dlist_node *p_node = NULL;

    if(dlist == NULL || value == NULL){
        p_node->prev = dlist->tail;
        return FALSE;
    }

    p_node = create_node();
    p_node->data = value;

    if(dlist->count == ZERO){     //插入前无元素
        dlist->head = dlist->tail = p_node;
    }else{    //插入前有元素
        dlist->tail->next = p_node;
        p_node->prev = dlist->tail;
        dlist->tail = p_node;
    }

    dlist->count++;
    return TRUE;
}

Boolean pop_front(Dlist *dlist)                                   //头部删除
{
    Dlist_node *p_node = NULL; 

    if(dlist == NULL || dlist->count == ZERO){
        return FALSE;
    }

    p_node = dlist->head;

    if(dlist->count == ONE){
        dlist->head = dlist->tail = NULL;
    }else{
        dlist->head = p_node->next;
        dlist->head->prev = NULL;
    }

    //释放节点,要对free指针作判断(data指向为堆还是栈)
    if(dlist->free != NULL){   //堆上(才可用free函数)
        dlist->free(p_node->data);
    }

    free(p_node);   //栈上
    dlist->count--;
    return TRUE;
}

Boolean pop_back(Dlist *dlist)                                    //头部删除
{
    Dlist_node *p_node = NULL;

    if(dlist == NULL || dlist->count == ZERO){
        return FALSE;
    }

    p_node = dlist->tail;

    if(dlist->count == ONE){   //只有一个元素
        dlist->head = dlist->tail = NULL;
    }else{    //多个元素
        dlist->tail = p_node->prev;
        dlist->tail->next = NULL;
    }

    if(dlist->free != NULL){
        dlist->free(p_node->data);
    }

    free(p_node);
    dlist->count--;

    return TRUE;
}

Dlist_node *find_node(Dlist *dlist, void *value)
{
    Dlist_node *p_node = NULL;

    if(dlist == NULL || value == NULL){
        return NULL;
    }

#if 0
    for(p_node = dlist->head; p_node; p_node = p_node->next){
        if(dlist->match){     //用户指定相等条件
            if(!dlist->match(p_node->data, value)){
                return p_node;
            }
        }else{
            if(p_node->data == value){
                return p_node;
            }
        }
    }
#endif

    if(dlist->match){
        for(p_node = dlist->head; p_node; p_node = p_node->next){
            if(!dlist->match(p_node->data, value)){
                return p_node;
            }
        }
    }else{ 
        for(p_node = dlist->head; p_node; p_node = p_node->next){
            if(p_node->data == value){
                return p_node;
            }
        }
    }

    return p_node;
}

Boolean insert_prev(Dlist *dlist, Dlist_node *node, void *value)  //插入到指定节点前边
{
    Dlist_node *p_node = NULL;

    if(dlist == NULL || node == NULL || value == NULL){
        return FALSE;
    }

    p_node = create_node();
    p_node->data = value;

    p_node->next = node;
    p_node->prev = node->prev;

    if(node->prev == NULL){
        dlist->head = p_node;
    }else{
        node->prev->next = p_node;
    }

    node->prev = p_node;
    dlist->count++;

    return TRUE;
}

Boolean insert_next(Dlist *dlist, Dlist_node *node, void *value)  //插入到指定节点后边
{
    Dlist_node *p_node = NULL;

    if(dlist == NULL || node == NULL || value == NULL){
        return FALSE;
    }

    p_node = create_node();
    p_node->data = value;

    p_node->prev = node;
    p_node->next = node->next;

    if(node->next == NULL){
        dlist->tail = p_node;
    }else{
        node->next->prev = p_node;
    }

    node->next = p_node;
    dlist->count++;

    return TRUE;
}

Boolean remove_dlist_node(Dlist *dlist, Dlist_node *node)         //删除指定节点
{   //被删除节点的位置1.头删2.尾删3.中间删
    if(dlist == NULL || node == NULL ){
        return FALSE;
    }

    if(node->next == NULL){   //尾删
        pop_back(dlist);
    }else if(node->prev == NULL){  //头删
        pop_front(dlist);
    }else{   //中间删
        node->prev->next = node->next;
        node->next->prev = node->prev;

        if(dlist->free != NULL){
            dlist->free(node->data);
        }
        free(node);
        dlist->count--;
    }

    return TRUE;
}

void    print_dlist(Dlist *dlist, Print_func print)               //链表的打印
{
    Dlist_node *p_node = NULL;   

    if(dlist == NULL || print == NULL || dlist->count == ZERO){
        return ;
    }

    p_node = dlist->head;

    while(p_node){
        print(p_node->data);    //print由使用者选择是哪种类型
        p_node = p_node->next;
    }

    printf("\n");
}

Boolean get_front(Dlist *dlist, void **value)                     //得到头结点的data
{
    if(dlist == NULL || value == NULL || dlist->count == ZERO){
        return FALSE;
    }

    *value = dlist->head->data;

    return TRUE;
}

Boolean get_tail(Dlist *dlist, void **value)                       //得到尾节点的data
{
     if(dlist == NULL || dlist->count == ZERO || value == NULL){
         return FALSE;
     }

     *value = dlist->tail->data;

     return TRUE;
}

int     get_dlist_count(Dlist *dlist)                             //得到链表的数量
{
    if(dlist == NULL){
        return -1;
    }

    return dlist->count;
}

栈的测试代码:

#include <stdio.h>
#include "stack.h"
#include "dlist.h"
#include "tools.h"

int main(int argc, char **argv)
{
    Stack *stack = NULL;
    int array[] = {12, 23, 34, 4, 1, 33, 24};
    int arr_len = sizeof(array) / sizeof(array[0]);
    int i  = 0;   
    int *value = NULL;

    stack = init_stack();   //双端链表的初始化
    //初始化完成后,如果有需要,应修改free、match和dup的指向

    for(i = 0; i < arr_len; ++i){
        push_stack(stack, &array[i]);               //尾部插入数组元素 
    }

    for(i = 0; i < arr_len; ++i){
        get_stack_top(stack, (void **)&value);
        pop_stack(stack);
        printf("%d ", *value);
    }
    printf("\n");

    destroy_stack(&stack);    //双端链表的销毁
    return 0;
}

栈的测试结果:

[root@localhost stack_dlist]# ls
dlist.c  dlist.h  main.c  stack  stack.c  stack.h  tools.c  tools.h
[root@localhost stack_dlist]# ./stack 
24 33 1 4 34 23 12 
[root@localhost stack_dlist]# 

你可能感兴趣的:(数据结构,栈,双端链表)