| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1548 人关注过本帖
标题:[数据结构]栈
取消只看楼主 加入收藏
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册: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
快速回复:[数据结构]栈
数据加载中...
 
   



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

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