数据结构 c语言 作用域问题

问题用//标出来了,同样的语句为什么放入循环结果就完全不同了?图片描述

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define NO 0
int nn=0;
typedef struct Node
{
    int data;
    struct Node *next;
}Node,*Linkstackptr;
typedef struct
{
    Linkstackptr top;
    int count;
}Linkstack;

Linkstackptr Createstacktail(Linkstack *S,int n)   
{
    Linkstackptr r,p;
    S->count=0;

    Linkstackptr *L;
    *L=(Node*)malloc(sizeof(Node));
    (*L)->data=++nn;(*L)->next=NULL;
    r=*L;
    S->top=*L; S->count++;
    printf("%d\n",(*L)->data);          //
    int j;
    for(j=1;j<n;j++)
    {   printf("%d\n",(*L)->data);       //
        p=(Node*)malloc(sizeof(Node));
        p->data=++nn;
        p->next=NULL;
        r->next=p;
        r=p;
        S->count++;
    }
    //printf("%d\n",(*L)->data);
    //printf("%d\n",(*L)->next);
    return *L;
}

//bool push()
void Showlist(Linkstackptr L)
{
    while(L)
    {
        printf("%d\n",L->data);
        L=L->next;
    }
}
void main()
{
    Linkstackptr first;
    Linkstack S;
    first=Createstacktail(&S,3);
    //Showlist(first);
    //printf("%d\n",S.count);

    //Showlist(first);
}
阅读 2.7k
2 个回答

Code style 挺醉人的……

还是赶紧扔掉谭浩强吧

$ gcc linkstack.c -o link && ./link
[1]    6174 segmentation fault  ./link

$ gcc --version
gcc (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1 20160904
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

在学数据结构 可以去学堂在线上课,选择这堂课的资料学习,丢掉你现在手上的书,这代码风格,现代编译器一堆报错

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题