氨氮测定仪厂家:来帮我分析一个简单的程序(C语言)

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/30 08:49:49
关于单链表创建的问题
问题是:程序运行不能显示出所创建的链表。不知道是创建环节出问题了,还是显示环节出问题了。
请大家分析一下指点迷津。
不胜感激。

程序如下:
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define OK 1

typedef struct LNode /*单链表结构定义*/
{
int data;
struct LNode *next;
}LNode;

void InitList(LNode *L, int n);
void DisplayList(LNode *L);

void main()
{
LNode *L;
int n;
printf("Please enter the number of nodes you want.\n");
scanf("%d",&n);
printf("Now enter the element.\n");
InitList(L,n);
DisplayList(L);
}

void InitList(LNode *L, int n) /*创建链表,逆位序创建*/
{
int i;
LNode *p;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
for(i=n;i>0;--i)
{
p=(LNode*)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=L->next;L->next=p;
}
free(p);
}

void DisplayList(LNode *L) /*显示链表*/
{
LNode *q=L->next;
while (q!=NULL)
{
printf("%d ",q->data);
q=q->next;
}
printf("\n");
free(q);
}

我已经调试通过..并加了注释...
#include <stdio.h>
#include <stdlib.h>
/*#define NULL 0 这个用不着,系统有这个的定义,自己定义了反而不好*/
#define OK 1

typedef struct LNode /*单链表结构定义*/
{
int data;
struct LNode *next;
}LNode;

void InitList(LNode **L, int n); /*接收的是指针的地址,所以的用指针的指针*/
void DisplayList(LNode *L); /*不用改变链表的值,所以只传指针就可以*/

void main()
{
LNode *L;
int n;
/*指针初始化*/
L=NULL;
printf("Please enter the number of nodes you want.\n");
scanf("%d",&n);
printf("Now enter the element.\n");
InitList(&L,n); /*要想用L得到链表的把地址给函数*/
DisplayList(L);
}

void InitList(LNode **L, int n) /*创建链表,逆位序创建*/
{
int i;
LNode *p,*q;
p=NULL;
q=NULL;
if(n<1)/*防止输入的n小于0*/
return;
(*L)=(LNode*)malloc(sizeof(LNode));
if(!(*L))/*申请内存失败*/
return;
(*L)->next=NULL;/*已经创建了一个 ,这个是第一个节点*/
scanf("%d",&((*L)->data) );
p=(*L);
for(i=0;i<n-1;i++) /*所以这个地方的N应该减1*/
{
q=(LNode*)malloc(sizeof(LNode)); /*这是后面的节点*/
if(!q)/*申请内存失败*/
return;
scanf("%d",&(q->data));/*最好加上括号*/
//printf("%d",q->data);
q->next =NULL;/*申请之后马上给指针初始化*/

p->next=q; /*L是头节点,是不能改变的,它是主函数传过来的接收一个链的指针*/
p=p->next ;
}
/*p和q 现在还在指向有用的空间不能释放掉*/
}

void DisplayList(LNode *L) /*显示链表*/
{
printf("DisplayList:\n");
while(L)
{
printf("%d,",L->data );
L=L->next ;
}

printf("\n");
}

我修改了下:
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define OK 1

typedef struct LNode /*单链表结构定义*/
{
int data;
struct LNode *next;
}LNode;
LNode *L,*head;
void InitList(LNode *L, int n);
void DisplayList(LNode *L);

void main()
{
head=NULL;
int n;
printf("Please enter the number of nodes you want.\n");
scanf("%d",&n);
printf("Now enter the element.\n");
InitList(L,n);
DisplayList(head);
}

void InitList(LNode *L, int n) /*创建链表,逆位序创建*/
{
int i;
LNode *p;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
for(i=1;i<=n;++i)
{
if(i==1)head=L;
p=(LNode*)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=L->next;L->next=p;L=p;
}
printf("\n");
}

void DisplayList(LNode *L) /*显示链表*/
{
LNode *q=L->next;
while (q!=NULL)
{
printf("%d \n",q->data);
q=q->next;
}
printf("\n");
free(q);
}

#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define OK 1

typedef struct LNode /*单链表结构定义*/
{
int data;
struct LNode *next;
}LNode;

void InitList(LNode *L, int n);
void DisplayList(LNode *L);

void main()
{
LNode *L;
int n;
printf("Please enter the number of nodes you want.\n");
scanf("%d",&n);
printf("Now enter the element.\n");
InitList(L,n);
DisplayList(L);
}

void InitList(LNode *L, int n) /*创建链表,逆位序创建*/
{
int i;
LNode *p;
/*L=(LNode*)malloc(sizeof(LNode)); */
L->next=NULL;
for(i=n;i>0;--i)
{
p=(LNode*)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=L->next;L->next=p;
}
/*free(p); */
}

void DisplayList(LNode *L) /*显示链表*/
{
LNode *q=L->next;
while (q!=NULL)
{
printf("%d ",q->data);
q=q->next;
}
printf("\n");
free(q);
}
=======================
这样就行了,你试试看,

void InitList(LNode *L, int n) /*创建链表,逆位序创建*/

这个函数中的free一句去掉试试

我觉得是free的使用有问题