链式栈的实现

链式栈:
链式栈的结构与链表类似,如所示,其中,top为栈顶指针,始终指向栈顶元素,栈顶元素的指针指向下一个元素,依此类推,直到栈底,栈底元素的指针区域为空如果top为空,表示该栈是个空栈。

我使用了三个文件实现了链式栈,首先是linked_stack.h,包含了我的结构体和函数声明

#ifndef __LINKED_STACK_H__
#define __LINKED_STACK_H__

#define datatype int
#include<stdio.h>
#include<stdlib.h>

typedef struct linked_stack
{
    int num;//编号
    datatype data;//保存的数据
    struct linked_stack *pnext;////指针域
}Stack,*PStack;


PStack init_linked_stack(PStack phead);

PStack travelsal(PStack phead);

PStack push(PStack phead,int num,datatype data);

PStack Pop(PStack phead,  PStack out_data);

PStack empty_linked_stack(PStack phead);



#endif // !__LINKED_STACK_H__

第二个是linked_stack.c文件,包含了我的函数实现部分。

#define _CRT_SECURE_NO_WARNINGS 1

#include"linked_stack.h"


//初始化栈
PStack init_linked_stack(PStack phead)
{
    return NULL;
}


//压栈
PStack push(PStack phead, int num, datatype data)
{
    PStack pnew = (PStack)malloc(sizeof(Stack));
    pnew->num = num;
    pnew->data = data;
    pnew->pnext = NULL;//这里是尾部。
    if (phead == NULL)
    {
        phead = pnew;

    }
    else
    {
        PStack p = phead;
        while (p->pnext != NULL)
        {
            p = p->pnext;//进行寻找最后的
        }
        p->pnext = pnew;//进行插入

    }
    return phead;
}


//遍历栈
PStack travelsal(PStack phead)
{
    PStack p = phead;

    if (phead == NULL)
    {
        return NULL;
    }
    else
    {
        while (p)
        {
            printf("%d,%d\n", p->num, p->data);
            p = p->pnext;
        }
    }
    return phead;
}

//出栈
PStack Pop(PStack phead, PStack out_data)
{
    if (phead == NULL)
    {
        return NULL;
    }
    else if (phead->pnext==NULL)//只有一个节点
    {
        out_data->num = phead->num;
        out_data->data = phead->data;
        phead = NULL;
        free(phead);
        return NULL;
    }
    else
    {
        PStack p = phead;
        while (p->pnext->pnext != NULL)//有多个节点
        {
            p = p->pnext;
        }
        out_data->num = p->pnext->num;
        out_data->data = p->pnext->data;
        free(p->pnext);
        p->pnext = NULL;
    }
    return phead;
}


//清空栈
PStack empty_linked_stack(PStack phead)
{
    if (phead == NULL)
    {
        return NULL;
    }
    else
    {
        PStack p = phead;
        PStack q = NULL;
        while (p->pnext != NULL)
        {
            q = p->pnext;
            p->pnext = q->pnext;//进行了删除
            free(q);
        }
        free(phead);
    }
    return NULL;
}

第三个是我的测试函数部分,包含了我的main函数以及函数调用。

#define _CRT_SECURE_NO_WARNINGS 1

#include"linked_stack.h"


int main()
{
    int num = 0;
    datatype data = 0;

    PStack phead = NULL;


    //测试初始化栈
    phead=init_linked_stack(phead);


    //测试压栈
    printf("请输入你所需要压栈的个数:");
    scanf("%d", &num);
    fflush(stdin);
    while (num)
    {
        printf("请输入你所压栈的值:");
        scanf("%d", &data);
        fflush(stdin);
        phead = push(phead, num, data);
        num--;
    }


    //测试遍历栈
    travelsal(phead);
    printf("\n");


    //测试出栈
    while (phead)
    {
        PStack out_data = (PStack)malloc(sizeof(Stack));
        phead = Pop(phead, out_data);//将出栈保存在一块中
        printf("出栈结果\n");
        travelsal(phead);
        printf("出栈的元素\n");
        printf("%d,%d\n", out_data->num, out_data->data);
        printf("\n");
    }



    //测试清空栈
    phead=empty_linked_stack(phead);
    travelsal(phead);

    system("pause");
    return 0;

}


你可能感兴趣的:(链式栈的实现)