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

查看:83
本文介绍了java - 数据结构 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);
}

解决方案

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.

这篇关于java - 数据结构 c语言 作用域问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆