栈的链式存储

Posted on 2016-06-01 13:16  洪爵士  阅读(191)  评论(0编辑  收藏  举报

LinkStack.h文件

#ifndef _LINKSTACK_H_
#define _LINKSTACK_H_
//定义节点
typedef struct _StackNode
{
    void *data;
    struct _StackNode* next;
}StackNode;
//栈的结构体
typedef struct _LinkStack
{
    StackNode* head;
    int length;
}LinkStack;

//初始化:创建栈
LinkStack* Creat_LinkStack();
//入栈
void Push_LinkStack(LinkStack* stack,void* data);
//出栈:弹出栈顶元素
void Pop_LinkStack(LinkStack* stack);
//求栈的大小
int Size_LinkStack(LinkStack* stack);
//获得栈顶元素
void* Top_LinkStack(LinkStack* stack);
//判断栈是否为空
int IsEmpty_LinkStack(LinkStack* stack);
//销毁栈
void Destroy_LinkStack(LinkStack* stack);
#endif

LinkStack.c文件

#include <stdio.h>
#include <stdlib.h>
#include "LinkStack.h"
//初始化:创建栈
LinkStack* Creat_LinkStack()
{
    LinkStack* stack=(LinkStack*)malloc(sizeof(LinkStack));
    if(stack==NULL)
    {
        return NULL;
    }
    //初始化
    stack->head=NULL;
    stack->length=0;
    return stack;
}
//入栈
void Push_LinkStack(LinkStack* stack,void* data)
{
    StackNode* newnode=NULL;
    if(stack==NULL||data==NULL)
    {
        return ;
    }
    //创建新节点
    newnode=(StackNode*)malloc(sizeof(StackNode));
    if(newnode==NULL)
    {
        printf("创建节点失败\n");
        return ;
    }
    newnode->data=data;
    newnode->next=NULL;
    //将新节点入栈
    if(stack->head==NULL)//如果是个空栈
    {
        stack->head=newnode;
        stack->length++;
        return ;
    }
    newnode->next=stack->head;
    stack->head=newnode;
    stack->length++;
}
//出栈:弹出栈顶元素
void Pop_LinkStack(LinkStack* stack)
{
    StackNode* pDel=NULL;//缓存要删除的节点
    if(stack==NULL)
    {
        return ;
    }
    if(stack->length==0)
    {
        return ;
    }
    pDel=stack->head;
    stack->head=pDel->next;
    //free(pDel);//释放节点
    stack->length--;
}
//求栈的大小
int Size_LinkStack(LinkStack* stack)
{
    if(stack==NULL)
    {
        return -1;
    }
    return stack->length;
}
//获得栈顶元素
void* Top_LinkStack(LinkStack* stack)
{
    if(stack==NULL)
    {
        return NULL;
    }
    if(stack->length==0)
    {
        return NULL;
    }
    return stack->head->data;
}
//判断栈是否为空
int IsEmpty_LinkStack(LinkStack* stack)
{
    if(stack==NULL)
    {
        return -1;
    }
    if(stack->length==0)
    {
        return 1;
    }
    return 0;
}
//销毁栈
void Destroy_LinkStack(LinkStack* stack)
{
    while(stack->length>0)
    {
        Pop_LinkStack(stack);
    }
    if(stack!=NULL)
    {
        free(stack);
    }
}

testLinkStack.c文件为了测试栈的链式存储

#include <stdio.h>
#include "LinkStack.h"
typedef struct Student
{
    char name[256];
    int age;
}Student;
int main()
{
    Student s1={"aaa",10};
    Student s2={"bbb",20};
    Student s3={"ccc",30};
    Student s4={"ddd",40};
    Student *sp=NULL;
    LinkStack* stack=Creat_LinkStack();
    if(stack==NULL)
    {
        printf("创建栈失败\n");
        return -1;
    }
    Push_LinkStack(stack,&s1);
    Push_LinkStack(stack,&s2);
    Push_LinkStack(stack,&s3);
    Push_LinkStack(stack,&s4);
    sp=(Student*)Top_LinkStack(stack);
    printf("栈顶:name:%s age:%d\n",sp->name,sp->age);
    Pop_LinkStack(stack);
    while(!IsEmpty_LinkStack(stack))
    {
        sp=(Student*)Top_LinkStack(stack);
        if(sp==NULL)
        {
            return -1;
        }
        printf("栈顶:name:%s age:%d\n",sp->name,sp->age);
        Pop_LinkStack(stack);
    }
    Destroy_LinkStack(stack);
    return -1;
}