学习链表写的一个程序,编译通过,运行出错

时间:2008-05-09 19:07:13   来源:论坛整理  作者:  编辑:chinaitzhe
估计是地址访问出错了,但是又看不出来哪里错了? 帮我看看??

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

#define OK 0
#define ERROR -1
#define __SETUP__(type,nn) if( !(nn=(type *)malloc(sizeof(type))) ) { return ERROR; }

typedef struct n
{
int data;
struct n *next;
}NODE;

typedef struct
{
NODE *head;
int total;
}LIST;

int init_LIST(LIST *L)
{
__SETUP__(LIST,L)
L->head=NULL;
L->total=0;
return OK;
}

int add_to_LIST(LIST *L,int n)
{
NODE *temp;
__SETUP__(NODE,temp)
temp->data=n;
temp->next=L->head;
L->head=temp;
L->total ;
return OK;
}

int show_LIST(LIST *L)
{
if(L->head==NULL)
{
return OK;
}
NODE *temp=L->head;
int n;
while(temp)
{
n=temp->data;
printf("%d\n",n);
temp=temp->next;
}
return OK;
}

int main()
{
LIST L;
init_LIST(&L);
add_to_LIST(&L,2);
show_LIST(&L);
return OK;
}

网友回复:怎么缩进都没了??
网友回复:再来一次

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

#define OK 0
#define ERROR -1
#define __SETUP__(type,nn) if( !(nn=(type *)malloc(sizeof(type))) ) { return ERROR; }

typedef struct n {
int data;
struct n *next;
}NODE;

typedef struct {
NODE *head;
int total;
}LIST;

int init_LIST(LIST *L) {
__SETUP__(LIST,L)
L->head=NULL;
L->total=0;
return OK;
}

int add_to_LIST(LIST *L,int n) {
NODE *temp;
__SETUP__(NODE,temp)
temp->data=n;
temp->next=L->head;
L->head=temp;
L->total ;
return OK;
}

int show_LIST(LIST *L) {
if (L->head==NULL) {
return OK;
}
NODE *temp=L->head;
int n;
while (temp) {
n=temp->data;
printf("%d\n",n);
temp=temp->next;
}
return OK;
}

int main() {
LIST L;
init_LIST(&L);
add_to_LIST(&L,2);
show_LIST(&L);
return OK;
}

网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



#include <stdio.h> 

#include <stdlib.h> 



#define OK 0 

#define ERROR -1 

#define __SETUP__(type,nn) if( !(nn=(type *)malloc(sizeof(type))) ) { return ERROR; } 



typedef struct n { 

    int data; 

    struct n *next; 

}NODE; 



typedef struct { 

    NODE *head; 

    int total; 

}LIST; 



int init_LIST(LIST *L) {         

    L->head=NULL;

    L->total=0; 

    return OK; 

} 



int add_to_LIST(LIST *L,int n) { 

    NODE *temp; 

    __SETUP__(NODE,temp) 

        temp->data=n; 

    temp->next=L->head; 

    L->head=temp; 

    L->total  ; 

    return OK; 

} 



int show_LIST(LIST *L) { 

    if (L->head==NULL) { 

        return OK; 

    } 

    NODE *temp=L->head; 

    int n; 

    while (temp) { 

        n=temp->data; 

        printf("%d\n",n); 

        temp=temp->next; 

    } 

    return OK; 

} 



int main() { 

    LIST L; 

    init_LIST(&L);

    add_to_LIST(&L,2); 

    show_LIST(&L); 

    return OK; 

} 


网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



#include <stdio.h> 

#include <stdlib.h> 



#define OK 0 

#define ERROR -1 

#define __SETUP__(type,nn) if( !(nn=(type *)malloc(sizeof(type))) ) { return ERROR; } 



typedef struct n { 

    int data; 

    struct n *next; 

}NODE; 



typedef struct { 

    NODE *head; 

    int total; 

}LIST; 



int init_LIST(LIST **L) { 

    __SETUP__(LIST,*L) 

    (*L)->head=NULL; 

    (*L)->total=0; 

    return OK; 

} 



int add_to_LIST(LIST **L,int n) { 

    NODE *temp; 

    __SETUP__(NODE,temp) 

    temp->data=n; 

    temp->next=(*L)->head; 

    (*L)->head=temp; 

    (*L)->total  ; 

    return OK; 

} 



int show_LIST(LIST **L) { 

    if ((*L)->head==NULL) { 

        return OK; 

    } 

    NODE *temp=(*L)->head; 

    int n; 

    while (temp) { 

        n=temp->data; 

        printf("%d\n",n); 

        temp=temp->next; 

    } 

    return OK; 

} 



int main() { 

    LIST *L; 

    init_LIST(&L); 

    add_to_LIST(&L,2); 

    show_LIST(&L); 

    return OK; 

} 


网友回复:LZ 在 main 在链表中所使用的 L 是一个局部变量,
是在栈中给他分配的空间,
在你调用完你的各个子函数后他所指向的空间地址根本没有变,
可以考虑换种方法实现,
尽量用指针,不要用局部变量!
网友回复:int init_LIST(LIST **L) {
__SETUP__(LIST,*L)
(*L)->head=NULL;
(*L)->total=0;
return OK;
}

网友回复:解决办法:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



#include <stdio.h> 

#include <stdlib.h> 



#define OK 0 

#define ERROR -1 

#define __SETUP__(type,nn) if( !(nn=(type *)malloc(sizeof(type))) ) { return ERROR; } 



typedef struct n 

{ 

    int data; 

    struct n *next; 

}NODE; 



typedef struct 

{ 

    NODE *head; 

    int total; 

}LIST; 



int init_LIST(LIST **L) 

{ 

    __SETUP__(LIST,*L) 

        (*L)->head=NULL; 

    (*L)->total=0; 

    return OK; 

} 



int add_to_LIST(LIST **L,int n) 

{ 

    NODE *temp; 

    __SETUP__(NODE,temp) 

        temp->data=n; 

    temp->next=(*L)->head; 

    (*L)->head=temp; 

    (*L)->total  ; 

    return OK; 

} 



int show_LIST(LIST **L) 

{ 

    if((*L)->head==NULL) 

    { 

        return OK; 

    } 

    NODE *temp=(*L)->head; 

    int n; 

    while(temp) 

    { 

        n=temp->data; 

        printf("%d\n",n); 

        temp=temp->next; 

    } 

    return OK; 

} 



int main(int argc, char* argv[])

{

    LIST *L; 

    init_LIST(&L); 

    add_to_LIST(&L,2); 

    show_LIST(&L); 

    return OK; 

}


网友回复:
引用 5 楼 zixin_yu 的回复:
LZ 在 main 在链表中所使用的 L 是一个局部变量,
是在栈中给他分配的空间,
在你调用完你的各个子函数后他所指向的空间地址根本没有变,
可以考虑换种方法实现,
尽量用指针,不要用局部变量!

说的非常有道理。注重指针变量在参数传递
网友回复:我用单步执行看了L->head的值在
int init_LIST(LIST *L) {
__SETUP__(LIST,L)
L->head=NULL; // L->head=0x000000
L->total=0;
return OK;
}
但是int add_to_LIST(LIST *L,int n) {
NODE *temp;
__SETUP__(NODE,temp)
temp->data=n;
temp->next=L->head;//此处L->head=0xcccccc
L->head=temp;
L->total ;
return OK;
}
所以后面while (temp) {
n=temp->data;
printf("%d\n",n);
temp=temp->next;
}
因为temp->next到了最后一个发现是0xcccccc就继续循环,访问了不能访问的空间,所以出错了!

网友回复:啊,指针真是非常神奇的东西,谢谢大家了
网友回复:
引用 5 楼 zixin_yu 的回复:
LZ 在 main 在链表中所使用的 L 是一个局部变量,
是在栈中给他分配的空间,
在你调用完你的各个子函数后他所指向的空间地址根本没有变,
可以考虑换种方法实现,
尽量用指针,不要用局部变量!

我感觉可以理解成:在主函数里定义好的L在子函数里又分配了一次空间,就不是原来的L了,对吧?
关键字:学习,链表,一个,程序,编译,
上一篇:3G的问题

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面