C语言链表问题,请大家帮忙看看!

愤怒的豆沙包 2011-10-24 08:48:51
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
int n;
struct student
{long num;
float score;
struct student *next;
};

void main()
{
struct student *p;
struct student *cre(struct student *p);
// struct student *ins(void);
// struct student *del(void);
// void pri(struct student *p);
void pra(struct student *p);

int d;
n=0;
p=NULL;

do
{
printf("please enter 0\\1\\2\\3\\4\\5:");
scanf("%d",&d);
switch(d)
{
case 1 : p=cre(p);break;
// case 2 : p=ins();break;
// case 3 : p=del();break;
// case 4 : pri(p);break;
case 5 : pra(p);break;
}
}while(d!=0);

printf("\nEND!!\n");
}





#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
extern n;
struct student
{long num; float score; struct student *next;};

struct student *cre(struct student *p)
{
struct student *head,*p1,*p2;

head=p;
p1=(struct student *)malloc(LEN);

if (p1!=NULL)
{
n=n+1;
printf("enter the num and score:");
scanf("%ld,%f",p1->num,p1->score);

if (head!=NULL)
{
p2=p+n-1;
p2->next =p1;
p2=p1;
p2->next=NULL;
printf("\n OK!! \n");
}
else
{
head=p1;
p2=p1;
p2->next =NULL;
printf("\n OK!! \n");
}
}
else
printf("error!!!\n");

return(head);
}




#include<stdio.h>
extern n;
struct student
{
long num;
float score;
struct student *next;
};

void pra(struct student *p)
{
int i;
printf("\n**********************************\n");

for(i=0;i<n;i++)
printf("%ld,%5.2f",(p+i)->num,(p+i)->score);

printf("\n**********************************\n");
}





我昨天看书的时候,看到了链表的地方就自己动手试着做一个可以插入,删除,输出的链表。

照着书本的解释,例题做了一个,刚刚完成了创建并插入和输出功能,但是不知道为啥 输出功能还好,插入功能只要是插入一个数据,程序就会自动结束。

昨天折腾了一个下午也没发现哪里有问题,请大伙帮帮忙,谢谢了!!(貌似貌似刚刚注册不到一周的帐号不能上传图片,就没法把结果发上来了,抱歉哈


...全文
140 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
愤怒的豆沙包 2011-10-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 caikuowei 的回复:]

单步跟踪下就知道了。你输入的时候用错了:用下面的就可以了:
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
int n;
struct student
{
long num;
float score;
struct student *nex……
[/Quote]


嗯嗯 谢谢了哈 解决了 真是那个问题的,老是忘了区地址符号!!
愤怒的豆沙包 2011-10-24
  • 打赏
  • 举报
回复
翻看了下以前的帖子,貌似应该是指针问题,但是确切在哪里就没找到了!!
愤怒的豆沙包 2011-10-24
  • 打赏
  • 举报
回复
刚刚看的结果显示是“Unhandled exception in 00.exe access violation” 是由于主函数中的指针未初始化么? 没发现哪个没进行初始化的啊!
愤怒的豆沙包 2011-10-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 anyidan 的回复:]

设断点,单步运行
[/Quote]

嗯嗯 谢谢哈 我先去看看 跟踪调试怎么搞的,第一次接触到这么麻烦的调试。
AnYidan 2011-10-24
  • 打赏
  • 举报
回复
设断点,单步运行
愤怒的豆沙包 2011-10-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]

跟踪啊,这么明显的现象,很容易调试的
[/Quote]

额,那个啥跟踪怎么做啊? 刚学C没多久的 只会看错误信息 然后跟着修改。
bdmh 2011-10-24
  • 打赏
  • 举报
回复
跟踪啊,这么明显的现象,很容易调试的
caikuowei 2011-10-24
  • 打赏
  • 举报
回复
单步跟踪下就知道了。你输入的时候用错了:用下面的就可以了:
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
int n;
struct student
{
long num;
float score;
struct student *next;
};
struct student *cre(struct student *p);
void pra(struct student *p);
void main()
{
struct student *p;
struct student *cre(struct student *p);
void pra(struct student *p);

int d;
n=0;
p=NULL;

do
{
printf("please enter 0\\1\\2\\3\\4\\5:");
scanf("%d",&d);
switch(d)
{
case 1 : p=cre(p);break;
case 5 : pra(p);break;
}
}while(d!=0);

printf("\nEND!!\n");
}

struct student *cre(struct student *p)
{
struct student *head,*p1,*p2;

head=p;
p1=(struct student *)malloc(LEN);

if (p1!=NULL)
{
n=n+1;
printf("enter the num and score:");
scanf("%ld,%f",&(p1->num),&(p1->score));

if (head!=NULL)
{
p2=p+n-1;
p2->next =p1;
p2=p1;
p2->next=NULL;
printf("\n OK!! \n");
}
else
{
head=p1;
p2=p1;
p2->next =NULL;
printf("\n OK!! \n");
}
}
else
printf("error!!!\n");

return(head);
}

void pra(struct student *p)
{
int i;
printf("\n**********************************\n");

for(i=0;i<n;i++)
printf("%ld,%5.2f",(p+i)->num,(p+i)->score);

printf("\n**********************************\n");
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧