Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 栈(C语言实现,基于链式结构)

栈(C语言实现,基于链式结构)Stack.h文件/**
 * 栈(C语言实现,基于链式结构)
 * 指定数据类型为整型
 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0typedef int Status;
typedef int ElemType;//定义栈节点的结构
typedef struct StackNode{
    ElemType data;
    struct StackNode* next;
}StackNode;
//定义栈的结构
typedef struct Stack{
    StackNode* top;
    int count;
}Stack;/*
 * 初始化一个空栈
 */
Status InitStack(Stack* s);
/*
 * 销毁栈
 */
Status DestroyStack(Stack* s);
/*
 * 清空栈
 */
Status ClearStack(Stack* s);
/*
 * 判断栈是否为空
 */
Status StackEmpty(Stack s);
/*
 * 获取栈的长度
 */
int StackLength(Stack s);
/*
 * 获取栈顶元素
 */
Status GetTop(Stack s, ElemType* e);
/*
 * 将元素压入栈
 */
Status Push(Stack* s, ElemType e);
/*
 * 将元素弹出栈
 */
Status Pop(Stack* s, ElemType* e);
/*
 * 打印栈
 */
void PrintStack(Stack* s);Stack.c文件#include <stdio.h>
#include <stdlib.h>
#include "Stack.h"Status InitStack(Stack* s)
{
    s->top = NULL;
    s->count = 0;
    return OK;
}Status DestroyStack(Stack* s)
{
    StackNode* sn_tmp_ptr;
    while(s->top){
        sn_tmp_ptr = s->top;
        s->top = s->top->next;
        free(sn_tmp_ptr);
    }
    free(s);
}Status ClearStack(Stack* s)
{
    while(s->top){
        s->top->data = 0;
        s->top = s->top->next;
    }
}Status StackEmpty(Stack s)
{
    return s.count<1 ? TRUE : FALSE;
}int StackLength(Stack s)
{
    return s.count;
}Status GetTop(Stack s, ElemType* e)
{
    *e = s.top->data;
    return OK;
}Status Push(Stack* s, ElemType e)
{
    StackNode* snptr = (StackNode*)malloc(sizeof(StackNode));
    snptr->data = e;
    snptr->next = s->top;
    s->top = snptr;
    s->count++;
    return OK;
}Status Pop(Stack* s, ElemType* e)
{
    *e = s->top->data;
    StackNode* sn_tmp_ptr = s->top;
    s->top = s->top->next;
    s->count--;
    free(sn_tmp_ptr);
    return OK;
}void PrintStack(Stack* s)
{
    while(s->top){
        printf("%d ",s->top->data);
        s->top = s->top->next;
    }
}int main()
{
    Stack s;
    ElemType e_tmp;
    InitStack(&s);
    printf("Is Stack empty : %d ",StackEmpty(s));
    Push(&s, 3);
    Push(&s, 2);
    Push(&s, 1);
    Push(&s, 0);
    Pop(&s, &e_tmp);
    Pop(&s, &e_tmp);
    printf("Is Stack empty : %d ",StackEmpty(s));
    PrintStack(&s);
    printf("%d",s.count);}C++ Primer Plus 第6版 中文版 清晰有书签PDF+源代码 http://www.linuxidc.com/Linux/2014-05/101227.htm读C++ Primer 之构造函数陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm读C++ Primer 之智能指针 http://www.linuxidc.com/Linux/2011-08/40177.htm读C++ Primer 之句柄类 http://www.linuxidc.com/Linux/2011-08/40175.htm将C语言梳理一下,分布在以下10个章节中:
  1. Linux-C成长之路(一):Linux下C编程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
  2. Linux-C成长之路(二):基本数据类型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
  3. Linux-C成长之路(三):基本IO函数操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
  4. Linux-C成长之路(四):运算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
  5. Linux-C成长之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
  6. Linux-C成长之路(六):函数要义 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
  7. Linux-C成长之路(七):数组与指针 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
  8. Linux-C成长之路(八):存储类,动态内存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
  9. Linux-C成长之路(九):复合数据类型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
  10. Linux-C成长之路(十):其他高级议题
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-08/105234.htm