数据结构链栈数据封装

链栈

定义数据结构:

typedef void (stack_op_t)(const void *);


//抽象栈数据类型
typedef struct stack_t{
	void *stack;//数据
	struct stack_t *next;
}STACK;

typedef struct lstack{
    STACK head;
	STACK *tail;
    int size;//栈中数据类型
	int top;//栈顶指针
	int max;//栈大小
}LSTACK;

//初始化
LSTACK *lstack_creat(int size, int max);
//入栈
int lstack_push(LSTACK *handle, void *data);
//出栈
void *lstack_pop(LSTACK *handle);
//判断满栈
int lstack_is_full(LSTACK *handle);
//判断空栈
int lstack_is_empty(LSTACK *handle);
//遍历
void lstack_travel(LSTACK *handle, stack_op_t *op);
//销毁
void lstack_destroy(LSTACK **handle);
//清空
void lstack_clean(LSTACK *handle);
//num
int lstack_num(LSTACK *handle);
//存储
void lstack_store(LSTACK *handle, const char *path);
//加载
LSTACK *lstack_load(const char *path);

//init creat

LSTACK *lstack_creat(int size, int max)
{
    LSTACK *handle = NULL;
    handle = (LSTACK *)malloc(sizeof(LSTACK));
    ERRP(handle == NULL, handle malloc, goto ERR1);

    handle->head.stack = NULL;
    handle->head.next = NULL;
    handle->tail = &handle->head; // 初始化tail指针
    handle->size = size;
    handle->max = max;
    handle->top = 0;

    return handle;

ERR1:
    return NULL;
}

//push

int lstack_push(LSTACK *handle, void *data)
{
    STACK *new = NULL;
    if (lstack_is_full(handle))
    {
        return 0;
    }
    else
    {
        new = (STACK *)malloc(sizeof(STACK));
        ERRP(NULL == new, new malloc, goto ERR1);

        new->stack = (void *)malloc(handle->size);
        ERRP(NULL == new->stack, new->stack malloc, goto ERR2);

        memmove(new->stack, data, handle->size);
        new->next = NULL;

        handle->tail->next = new;
        handle->tail = new; // 更新tail指针
        handle->top++;
    }
    return 1;

ERR2:
    free(new);
ERR1:
    return 0;
}

//pop

void *lstack_pop(LSTACK *handle)
{
    if (lstack_is_empty(handle))
    {
        return NULL;
    }
    else
    {
        STACK *save = &handle->head;
        STACK *tail = save->next;
        void *data = handle->tail->stack;

        if (save->next == handle->tail) // Only one element in the stack
        {
            free(handle->tail);
            save->next = NULL;
            handle->tail = save; // Reset tail to head
        }
        else
        {
            while (tail->next != handle->tail)
            {
                save = tail;
                tail = tail->next;
            }
            save->next = NULL;
            free(handle->tail);
            handle->tail = save;
        }

        handle->top--;
        return data;
    }
    return NULL;
}

//is_full

int lstack_is_full(LSTACK *handle)
{
    if (handle->top == handle->max)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

//is_empty

int lstack_is_empty(LSTACK *handle)
{
    if (handle->top == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

//destroy

void lstack_destroy(LSTACK **handle)
{
    STACK *tail = NULL;
    STACK *sava = NULL;

    for (tail = (*handle)->head.next; tail != NULL; tail = sava)
    {
        sava = tail->next;
        free(tail->stack);
        free(tail);
    }
    free(*handle);
    *handle = NULL;
}

//traval

void lstack_travel(LSTACK *handle, stack_op_t *op)
{
    STACK *tail = handle->head.next;
    while (tail != NULL)
    {
        op(tail->stack);
        tail = tail->next;
    }
}

//clean

void lstack_clean(LSTACK *handle)
{
    while (handle->top)
    {
        lstack_pop(handle);
    }
}

//num count

int lstack_num(LSTACK *handle)
{
    return handle->top;
}

//fileio

void lstack_store(LSTACK *handle, const char *path)
{
    FILE *fp = NULL;
    STACK *tail = handle->head.next;
    fp = fopen(path, "w");
    ERRP(NULL == fp, fopen, goto ERR1);

    if (fwrite(handle, sizeof(*handle), 1, fp) != 1)
    {
        perror("fwrite");
        goto ERR2;
    }

    while (tail != NULL)
    {
        if (fwrite(tail->stack, handle->size, 1, fp) != 1)
        {
            perror("fwrite");
            goto ERR2;
        }
        tail = tail->next;
    }
ERR2:
    fclose(fp);
ERR1:
    return;
}
// 加载
LSTACK *lstack_load(const char *path)
{
    FILE *fp;
    int i;
    LSTACK *handle = NULL;
    void *new = NULL;

    fp = fopen(path, "r");
    ERRP(NULL == fp, fopen, goto ERR1);

    handle = (LSTACK *)malloc(sizeof(LSTACK));
    ERRP(NULL == handle, handle malloc, goto ERR2);

    if (fread(handle, sizeof(*handle), 1, fp) != 1)
    {
        perror("fread");
        goto ERR3;
    }

    handle->head.stack = NULL;
    handle->head.next = NULL;

    for (i = 0; i < handle->top; i++)
    {
        new = (void *)malloc(handle->size);
        ERRP(NULL == new, new malloc, goto ERR3);

        if(fread(new , handle->size , 1 , fp) != 1)
        {
            perror("fread");
            goto ERR3;
        }   
        handle->top--;
        lstack_push(handle , new);
    }
    return handle;

ERR3:
    free(handle);
ERR2:
    fclose(fp);
ERR1:
    return NULL;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值