关于堆栈

好久没来了,除除草,最近忙于完成数据挖掘的作业时候,没来得及学习数据结构,罪过。这几天有时间了,抓紧时间学习数据结构。温习了下之前写的程序,看堆栈的时候,灵机一动,我为什么非得一个一个得pop和push呢?主函数一次一次的调用好繁琐。我为什么不能设置一个和链表一样的结构,调用一次就可以push,pop好多个值。说干就干,立马动手改了程序。吶,如下。

//动态堆栈的创建
# include<stdio.h>
# include<stdlib.h>
//定义堆栈结构体
struct stack
{
    int data;
    struct stack *last;
 //   struct stack *p;
};
//堆栈push函数
//struct stack *top;//头指针为空;
int n=0;//n为计算堆栈里有几个元素
struct stack* push()
{
    struct stack *p1,*p2,*p3,*top;
   // struct stack *top=NULL;//头指针为空;
    p2=p1=(struct stack*)malloc(sizeof(struct stack));//申请动态存储区
    top=NULL;//头指针为空
    scanf("%d",&p1->data);//给堆栈赋第一个值
    while(p1->data!=0)//当输入数值不为0时,堆栈累积
    {
         n++;
         if(n==1)
         p1->last=NULL;//之前在这出错了,写成p2->last了,不对,应该是p1,之后p1的值都会赋给p2
         else
         p1->last=p2;
         p2=p1;
         top=p2;
         printf("the stack's top is %d\nthe push number is %d\n",top,p2->data);
         p1=(struct stack*)malloc(sizeof(struct stack));//申请动态存储区
         scanf("%d",&p1->data);
    }
   return top;
}
//堆栈pop函数
struct stack* pop(struct stack *top)
{
    struct stack *p1,*p2;
    p1=top;//弹出的值
    while (p1->last!=NULL)
    {
         printf("\nnum:%ld,next:%ld",p1->data,p1->last);
         p1=p1->last;
         n--;
         if(p1->last==NULL)
            printf("\nnum:%ld,next:%ld",p1->data,p1->last);
      }
   // return(p1);
    //free(p2);
}
//主函数
void main()
{
    struct stack *p1,*p2,*p3,*p4,*top;
    top=(struct stack*)malloc(sizeof(struct stack));//申请动态存储区
    top=push();
    printf("堆栈里总共有%d个数\n",n);
   // printf("the stack's top is %d\nthe push number is %d\n",top,p1->data);
   /* p3=(struct stack*)malloc(sizeof(struct stack));//申请动态存储区
    p3=push();
    printf("堆栈里总共有%d个数\n",n);
    printf("the stack's top is %d\nthe push number is %d\n",top,p3->data);*/
   /* p4=(struct stack*)malloc(sizeof(struct stack));//申请动态存储区
    p4=push();
    printf("堆栈里总共有%d个数\n",n);
    printf("the stack's top is %d\nthe push number is %d\n",top,p4->data);*/
    pop(top);
   // printf("堆栈里总共有%d个数\n",n);
   // printf("the stack's top is %d\nthe pop number is %d\n",top,p2->data);
}

然后晚上回宿舍的路上和同学讨论这个思路,被鄙视。说,“你见过一次push or pop好多个的堆栈函数吗?堆栈函数就是主函数一次次的调用,push or pop几个,就调用几次呗,没什么繁琐的”。醍醐灌顶,好像真的是这么回事。不过我这种思路改进了也可以用,根据需要push or pop几个数,微调push or pop子函数,也是可以哒。聪明反被聪明误。我重新写一下每次push or pop一个值的函数。这个事情告诉我们,改程序前要留底。。

还有一个注意点,不要随便设置全局变量!!!以后做工程万一其他人子函数里设置了和我这全局变量里一样的变量,那就太可怕了!

//然后晚上回宿舍的路上和同学讨论这个思路,被鄙视。说,“你见过一次push or pop
//好多个值的堆栈函数吗?堆栈函数就是主函数一次次的调用,push or pop几个,
//就调用几次呗,没什么繁琐的”。醍醐灌顶,好像真的是这么回事。
//不过我这种思路改进了也可以用,根据需要push or pop几个数,微调push or pop子函数,
//也是可以哒。
//聪明反被聪明误。我重新写一下每次push or pop一个值的函数。
//这个事情告诉我们,改程序前要留底。。

//动态堆栈的创建
# include<stdio.h>
# include<stdlib.h>
//定义堆栈结构体
struct stack
{
    int data;
    struct stack *last;
 //   struct stack *p;
};
//堆栈push函数
//struct stack *top;//头指针为空;
int n=0;//n为计算堆栈里有几个元素
struct stack* push()
{
    struct stack *p1,*p2,*p3,*top;
   // struct stack *top=NULL;//头指针为空;
    p2=p1=(struct stack*)malloc(sizeof(struct stack));//申请动态存储区
    scanf("%d",&p1->data);//给堆栈赋第一个值
    p1->last=NULL;
    top=p1;
    n++;
   return top;
}
//堆栈pop函数
struct stack* pop(struct stack *top)
{
    struct stack *p1,*p2;
    if(top->data!=0)
   {
       p1=top;//弹出的值
       printf("\n弹出的值为num:%ld,则目前的top地址为:%ld",p1->data,p1->last);
       top=p1->last;
       n--;
   }
}
//主函数
void main()
{
    struct stack *p1,*p2,*p3,*p4,*top;
    top=(struct stack*)malloc(sizeof(struct stack));//申请动态存储区
    top=push();
    printf("堆栈里总共有%d个数\n",n);
   // printf("the stack's top is %d\nthe push number is %d\n",top,p1->data);
    p3=(struct stack*)malloc(sizeof(struct stack));//申请动态存储区
    p3=push();
    printf("堆栈里总共有%d个数\n",n);
    printf("the stack's top is %d\nthe push number is %d\n",top,p3->data);
   /* p4=(struct stack*)malloc(sizeof(struct stack));//申请动态存储区
    p4=push();
    printf("堆栈里总共有%d个数\n",n);
    printf("the stack's top is %d\nthe push number is %d\n",top,p4->data);*/
    pop(top);
   // printf("堆栈里总共有%d个数\n",n);
   // printf("the stack's top is %d\nthe pop number is %d\n",top,p2->data);
}

给大神看了,说我的程序可读性太差,让我看看人家写的程序。好吧。

#include <stdio.h>
#include <stdlib.h>


struct stack{
    int data;
    struct stack* last;
};

struct stack* push(struct stack* top)
{
    int data;
    if(top == NULL)
    {
        top = (struct stack*)malloc(sizeof(struct stack));
        printf("请输入数据");
        scanf("%d",&data);
        top->last = NULL;
        top->data = data;
        return top;
    }else{
        struct stack* insert = (struct stack*)malloc(sizeof(struct stack));
        printf("请输入数据");
        scanf("%d",&data);
        insert->data = data;
        insert->last = top;
        top = insert;
        return top;
    }
}

void output(struct stack* top) //显示目前堆栈内的值
{
    struct stack* top_temp;
    top_temp = top;
    printf("栈内数据从顶部到底部:");
    while(top_temp != NULL){
        printf("%d   ",top_temp->data);
        top_temp = top_temp->last;
    }
    printf("\n");
}
   struct stack* pop(struct stack* top)
   {
       if(top!=NULL)
       {
            struct stack* p1 = (struct stack*)malloc(sizeof(struct stack));
            p1=top;
            printf("弹出的值为%d\n",p1->data);
            top=p1->last;
       }
       else
       printf("当前堆栈为空\n");
   }
void main()
{
    struct stack* top = NULL;
    int i = 0;
    for(;i < 5; i++)
    {
        top = push(top);
        output(top);
    }
    for(i=0;i < 5; i++)
    {
        top = pop(top);
        output(top);
    }
}

 嗯,对比之下,我的问题太明显了,程序不够简介明了,变量设置太乱,输出栏太机器化,输出的是给人看的,应该尽量看起来清楚明白,该有的文字要有,让人一目了然。还有人家只需要设置个for循环,就可以控制push和pop值的个数。我蠢了。相应的输出函数也只需要有一个,push or pop完之后都调用就好了。

前路漫漫,加油吧!

原文地址:https://www.cnblogs.com/sunmarvell/p/6061082.html