栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c语言单链表实现栈

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c语言单链表实现栈

栈的元素可以自己定义。通过自己定义结构体,修改结构体初始化的函数和输出结构体的函数,就可以实现类似template的功能。

// List.h

// If change the type of nodes of list, CHANGE typedef *** ElemType;
// change dispElemType() and getElemType() according to the struct you defined.

#include 
#include 
#include 

#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef struct
// 用于充当栈(链表)的基本元素,可自己定义。
// 注意需要修改dispElemType()和getElemType()。
{
    int data;
    char *string;
}student;



typedef student ElemType;
typedef int Status;


void dispElemType(ElemType e)
{
    printf("%d ", e.data);
    printf("%sn", e.string);
}
ElemType getElemType()
{
    ElemType e;
    e.data = 10;
    e.string = "hhh";
    return e;
}

typedef struct LNode
{
    struct LNode* next;
    struct LNode* prior;
    ElemType data;
}LNode, *List;

List getList()
{
    List l = (List)malloc(sizeof(LNode));
    ElemType s = getElemType();
    l->data = s;
    l->next = NULL;
    l->prior= NULL;

    return l;
}

Status insertListRear(List l, ElemType e)
{
    if (!l) return ERROR;
    List t = getList();
    t->data = e;
    while (l->next)
    {
        l = l->next;
    }
    l->next = t;
    t->next = NULL;
    t->prior = l;

    return OK;
}

void dispList(List l)
{
    while (l)
    {
        dispElemType(l->data);
        l = l->next;
    }
}

Status deleteListRear(List l)
{   
    if (!l) return ERROR;
    while (l->next)
    {
        l = l->next;
    }
    l = l->prior;
    List d = l->next;

    l->next = NULL; d->prior = NULL;
    free(d); d = NULL;

    return OK;
}

Status clearList(List l)
{
    if (!l) return ERROR;
    if (!l->next) return OK;
    List p = l->next;
    l->next = NULL;
    while (p)
    {
        List d = p;
        p = p->next;
        d->next = NULL;
        free(d); d = NULL;
    }
    return OK;
}

// Stack.h

#include "List.h"


typedef struct stack
{
    List back;
    List top;
    int StackSize;
}stack, *Stack;

Stack getStack()
{
    Stack s = (Stack)malloc(sizeof(stack));
    s->back = getList();
    s->top = s->back;
    s->StackSize = 0;
    return s;
}

void dispStack(Stack s)
{
    printf("Total %d numbers in stack, from bottom to top: n", s->StackSize);
    dispList(s->back->next);
}

Status pushStack(Stack s, ElemType e)
{
    if (!insertListRear(s->top, e)) return ERROR;
    s->top = s->top->next;
    s->StackSize++;
    printf("Success pop out:"); dispElemType(e);
    return OK;
}

Status popStack(Stack s)
{
    if (s->StackSize == 0) return ERROR;
    ElemType e = s->top->data;
    s->top = s->top->prior;
    deleteListRear(s->top);
    printf("Success push in:"); dispElemType(e);
    s->StackSize--;

    return OK;
}

int lenStack(Stack s)
{
    return s->StackSize;
}

Status clearStack(Stack s)
{
    if (!clearList(s->back)) return ERROR;
    s->StackSize = 0;
    printf("Stack has been cleared.n");
    return OK;
}


下面是简单测试和测试结果

#include "Stack.h"

int main()
{
    Stack s = getStack();
    ElemType e = getElemType();
    pushStack(s, e);
    pushStack(s, e);
    dispStack(s);
    popStack(s);
    dispStack(s);
    clearStack(s);
    dispStack(s);

    return 0;
}
Success push in:10 hhh
Success push in:10 hhh
Total 2 numbers in stack, from bottom to top: 
10 hhh
10 hhh
Success pop out:10 hhh
Total 1 numbers in stack, from bottom to top: 
10 hhh
Stack has been cleared.
Total 0 numbers in stack, from bottom to top: 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号