栈 ------ 知识学习2

1.栈类型

2.链式栈

3.实例

cpp 复制代码
#ifndef LINKSTACK_H
#define LINKSTACK_H


typedef struct{
    char name[32];
    char sex;
    int age;
    int score;
}DATATYPE;

typedef struct LinkStackNode {
    DATATYPE data;
    struct LinkStackNode *next;
}LinkStackNode;

typedef struct{
    LinkStackNode *top;// head
    int clen;
}LinkStackList;

LinkStackList* CreateLinkStackList();
int DestroyLinkStack(LinkStackList*ls);
int PushLinkStack(LinkStackList*ls,DATATYPE* data);
int PopLinkStack(LinkStackList*ls);
int GetSizeLinkStack(LinkStackList*ls);
DATATYPE*GetTopLinkStack(LinkStackList*ls);
int IsEmptyLinkStack(LinkStackList*ls);


#endif // LINKSTACK_H
cpp 复制代码
#include "linkstack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
LinkStackList *CreateLinkStackList()
{
   LinkStackList *ls=(LinkStackList*)malloc(sizeof(LinkStackList));
   if(NULL == ls)
   {
       perror("CreateLinkStackList malloc");
       return NULL;
   }
   ls->top =NULL;
   ls->clen = 0 ;
   return ls;

}

int PushLinkStack(LinkStackList *ls, DATATYPE *data)
{
    LinkStackNode* newnode = malloc(sizeof(LinkStackNode));
    if(NULL == newnode)
    {
        perror("PushLinkStack malloc");
        return 1;
    }
    memcpy(&newnode->data,data,sizeof(DATATYPE));
    newnode->next = NULL;
    if(IsEmptyLinkStack(ls))
    {
        ls->top = newnode;
    }
    else
    {
        newnode->next = ls->top;
        ls->top = newnode;
    }
    ls->clen++;
    return 0;
}

int PopLinkStack(LinkStackList *ls)
{
    if(IsEmptyLinkStack(ls))
    {
        return 1;
    }
    LinkStackNode* tmp = ls->top;
    ls->top = ls->top->next;
    free(tmp);
    ls->clen--;
    return 0 ;
}

int IsEmptyLinkStack(LinkStackList *ls)
{
    return 0 == ls->clen;
}

int GetSizeLinkStack(LinkStackList *ls)
{
    return ls->clen;
}

DATATYPE *GetTopLinkStack(LinkStackList *ls)
{
    if(IsEmptyLinkStack(ls))
        return NULL;
    return &ls->top->data;
}

int DestroyLinkStack(LinkStackList *ls)
{
    int len = GetSizeLinkStack(ls);
    int i = 0 ;
    for(i = 0 ;i<len;i++)
    {
        PopLinkStack(ls);
    }
    free(ls);
    return 0;
}

(ls->top = ls->top->next 时中间的tmp 则可被释放 // )

cpp 复制代码
#include <stdio.h>
#include "linkstack.h"
int main()
{
    LinkStackList *ls = CreateLinkStackList();
    DATATYPE data[5]={
           {"zhangsan",'m',20,70},
           {"lisi",'f',21,60},
           {"wangmazi",'m',25,80},
           {"liubei",'f',30,85},
           {"caocao",'f',40,90},
       };

    int  i = 0 ;
    for(i = 0 ;i<5;i++)
    {
        PushLinkStack(ls,&data[i]);
    }

    int size = GetSizeLinkStack(ls);
    DATATYPE* tmp;
    for(i=0;i<size;i++)
    {
        tmp = GetTopLinkStack(ls);
        printf("name:%s score:%d\n",tmp->name,tmp->score);
        PopLinkStack(ls);
    }
    DestroyLinkStack(ls);
    printf("Hello World!\n");
    return 0;
}

4.检查是否泄漏

相关推荐
待什么青丝16 分钟前
【TMS570LC4357】之相关驱动开发学习记录2
c语言·arm开发·驱动开发·单片机·学习
行云流水剑27 分钟前
【学习记录】如何使用 Python 提取 PDF 文件中的内容
python·学习·pdf
虾球xz1 小时前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习
蓝婷儿2 小时前
6个月Python学习计划 Day 17 - 继承、多态与魔术方法
开发语言·python·学习
持续前进的奋斗鸭3 小时前
Postman测试学习(1)
学习·postman
hello kitty w3 小时前
Python学习(7) ----- Python起源
linux·python·学习
一叶知秋秋3 小时前
python学习day39
人工智能·深度学习·学习
永日456704 小时前
学习日记-day24-6.8
开发语言·学习·php
安和昂4 小时前
【iOS】 Block再学习
学习·ios·cocoa
pop_xiaoli4 小时前
OC学习—命名规范
学习·ios