栈stack(2):栈的链表实现

定义


 

 

  从上一篇我们知道,栈(stack)是一个只允许一端进行删除插入操作的线性表。同时,我们联想到线性表的链式结构,其特点是用一组任意的存储单元存储线性表的数据元素,因此我们选择使用链表去实现栈,规定这种实现办法叫做链栈。

  

主要过程


 

 

一.申明结构体类型

 1.node  结点结构体类型    

typedef struct Node
{
        SElemType data;  //SElemType 指任意数据类型,如int,float
        struct Node *next;
}node;

  

 2.stack  栈结构体类型        

typedef struct Stack
{
        struct Node *top;  //记录栈顶位置
        int count;  //记录链栈长度
}stack;

 

二.栈初始化

 1.为结点top申请内存,这里我们假设申请到的内存的地址为为0X01。

 2.count=0

  

三.入栈

 1.定义一个node *型变量p,为其申请内存,这里我们假设申请到的内存的地址为0x02

      

 2.将新结点串入链栈,即是新结点p指向top,p->next=stack->top,注意:指向的方向与链表有所不同,这里是指向旧结构体,即栈底

      

  3.将结点串入下一结点,即是top=p;同时count++

      

  四.出栈

   1.定义一个node *型的变量p,令p=top,结点top串入下一结点

      

   2.把结点p free();释放,即0X02所在的数据域与指针域与被释放。 

       

用处 


 

 

那么初学者都会想:教练,我学了链栈能干什么?

  可以做以下projects,这些projects都是符合后进先出(LIFO)原则的:

    

 

 

代码


 

  

/*************************************************************************
	> File Name:链栈(link_stack)
	> Author: Bw98
	> Mail: 786016746@qq.com
	> Blog: www.cnblogs.com/Bw98blogs/
	> Created Time: MON 17th Jul. 2017
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define OK 1
typedef int SElemType;

typedef struct Node
{
        SElemType data;
        struct Node *next;
}node;

typedef struct Stack
{
        struct Node *top;
        int count;  //长度
}stack;

int init_stack(stack *s);  //初始化为空栈
int if_empty_stack(stack *s);
int push(stack *s,int elem);  //入栈
int pop(stack *s,SElemType *e);  //出栈
int get_top(stack *s);  //显示栈顶元素
int show_stack(stack *s);
int clear_stack(stack *s);  //链栈置空
int destroy_stack(stack *s);  //摧毁栈
int length_stack(stack *s);  //链栈长度

int main()
{
        stack s;
        SElemType elem;
        int a;
        SElemType *elem2=&a;
        init_stack(&s);
        is_empty_stack(&s);
        printf("入栈开始:\n");
        while(scanf("%d",&elem)!=EOF)
        {
                getchar();
                push(&s,elem);
        }
        show_stack(&s);
        printf("该栈的栈顶元素为:\n");
        get_top(&s);
        pop(&s,elem2);
        printf("出栈元素为:%d\n",*elem2);
        clear_stack(&s);
        is_empty_stack(&s);
        return 0;
}

int init_stack(stack *s)
{
        s->top=(node *)malloc(sizeof(node));
        s->count=0;
        return OK;
}

int is_empty_stack(stack *s)
{
        if(s->count==0)
                printf("链栈为空!\n");
        else
                printf("链栈不为空!\n");
        return OK;
}

int clear_stack(stack *s)
{
        node *p=(node *)malloc(sizeof(node));
        p=s->top;
        while(s->count)
        {
                p=s->top;
                s->top=s->top->next;
                free(p);
                s->count--;
        }
        //s->count=0;
        printf("清除完毕!\n");
        return OK;
}
int length_stack(stack *s)
{
        return s->count;
}

int push(stack *s,SElemType x)
{
        node *p=(node *)malloc(sizeof(node));
        p->next=s->top;
        s->top=p;
        s->count++;
        s->top->data=x;
        printf("已入栈\n");
        return OK;
}

int get_top(stack *s)
{
        if(s->count!=0)
        {
                 printf("%d\n",s->top->data);
        }
        else
                printf("栈为空,无法返回栈顶元素!\n");
        return OK;
}

int pop(stack *s,SElemType *e)
{
        if(s->count==0)
                printf("栈为空,无法实现出栈\n");
        else
        {
                *e=s->top->data;
                node *p=(node*)malloc(sizeof(node));
                p=s->top;
                s->top=s->top->next;
                free(p);
                printf("出栈完毕!\n");
                s->count--;
        }
        return OK;
}

int show_stack(stack *s)
{
        printf("栈内元素如下\n");

        if(s->count)
        {
                node *p=s->top;
                int i;
                for(i=1;i<=s->count;i++)
                {
                        printf("%d ",s->top->data);
                        s->top=s->top->next;
                }
                s->top=p;
        }
        if(s->count==0)
                printf("空栈,无法查看栈内元素!\n");
        return OK;
}

 

posted @ 2017-07-17 14:50  bw98  阅读(437)  评论(2编辑  收藏  举报