标题:[数据结构]栈
只看楼主
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:705
专家分:2043
注 册:2010-11-11
结帖率:96.55%
 问题点数:0 回复次数:0 
[数据结构]栈
Stack.h
程序代码:
#ifndef STACK_H
#define STACK_H

#include <stdbool.h>
#include <stddef.h>

typedef signed int ssize_t;

typedef struct StackNode_struct
{
    int data;
    struct StackNode_struct * Prev;
} StackNode_t;

typedef struct
{
    StackNode_t * Top;
    StackNode_t * Bottom;
    size_t Count;
} Stack_t;

bool Stack_Init(Stack_t * stack);
bool Stack_Push(Stack_t * stack, int data);
bool Stack_Pop(Stack_t * stack, int * data);
bool Stack_Top(Stack_t * stack, int * data);
bool Stack_IsEmpty(const Stack_t * stack);
ssize_t Stack_Count(const Stack_t * stack);

#endif

Stack.c
程序代码:
#include <stdlib.h>
#include "Stack.h"

bool Stack_Init(Stack_t * stack)
{
    StackNode_t * node = NULL;

    if(!stack)
        return false;

    node = malloc(sizeof(StackNode_t));
    if(!node)
        return false;
   

    node->Prev = NULL;
    node->data = 0;

    stack->Top = node;
    stack->Bottom = node;
    stack->Count = 0;

    return true;
}

bool Stack_Push(Stack_t * stack, int data)
{
    StackNode_t * node = NULL;

    if(!stack)
        return false;
   

    node = malloc(sizeof(StackNode_t));
    if(!node)
        return false;
   

    node->data = data;
    node->Prev = stack->Top;

    stack->Top = node;
    stack->Count++;

    return true;
}

bool Stack_Pop(Stack_t * stack, int * data)
{
    StackNode_t * node = NULL;

    if(!stack)
        return false;
   

    if(stack->Count == 0)
        return false;
   

    *data = stack->Top->data;

    node = stack->Top;
    stack->Top = node->Prev;
    stack->Count--;
    free(node);

    return true;
}

bool Stack_Top(Stack_t * stack, int * data)
{
    if(!stack)
        return false;
   

    if(stack->Count == 0)
        return false;
   

    *data = stack->Top->data;

    return true;
}

bool Stack_IsEmpty(const Stack_t * stack)
{
    if(!stack)
        return false;
   

    return stack->Count == 0;
}

ssize_t Stack_Count(const Stack_t * stack)
{
    if(!stack)
        return -1;
   

    return stack->Count;
}

搜索更多相关主题的帖子: Stack bool node data return 
2019-08-14 22:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-496399-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.113754 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved