急!用c语言实现链表

vitamine77 2009-08-05 09:45:33
就是链表的创建和输出,我写的为什么运行不了啊??

先是建立个空表头,然后依次插入,构成链表,最后是输出;

谢谢~

以下是我写的程序……

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct list)
struct list
{
int num;
long data;
struct list *next;
};
struct list*head;

struct list initlist (struct list * head)
{
head=(struct list *)malloc(LEN);
head->next=NULL;
}

struct list creat (struct list *head,int n)
{
struct list *p;
int i;
initlist(head);
for(i=n;i>0;--i)
{
p=(struct list *)malloc(LEN);
scanf("%d,%f",&p->num,&p->data);
p->next=head->next;
head->next=p;
}

}

int print(struct list*head)
{
struct list *p1;
p1=head;
if (head!=0)
{
printf("%d,%f",p1->num,p1->data);
p1=p1->next;
}while(p1!=0);
}

main()
{

initlist(head);
creat(head,3);
print(head);
getchar();
getchar();
}
...全文
68 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
希望楼主多多学习。

#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct list)

struct list
{
int num;
float data;
struct list *next;
};
struct list*head;

void initlist ()
{
head=(struct list *)malloc(LEN);
head->next=NULL;
}

void creat (int n)
{
struct list *p;
int i;
initlist();

struct list *pp = head;
for(i=n;i>0;--i)
{
p=(struct list *)malloc(LEN);
scanf("%d,%f",&p->num,&p->data);
p->next=NULL;
pp->next=p;
pp = p;
}

}

int print()
{
struct list *p1;
p1=head->next;

while(p1!=NULL)
{
printf("%d,%f\n",p1->num,p1->data);
p1=p1->next;
}

return 0;
}

int main()
{

//initlist(head);
creat(3);
print();
getchar();
getchar();
return 0;
}
  • 打赏
  • 举报
回复
额,问题好多啊。
只说几个吧?

#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct list)
struct list
{
int num;
long data;
struct list *next;
};
struct list*head;

struct list initlist (struct list * head) //没有返回值
{
head=(struct list *)malloc(LEN);
head->next=NULL;
}

struct list creat (struct list *head,int n)
{
struct list *p;
int i;
initlist(head);
for(i=n;i>0;--i)
{
p=(struct list *)malloc(LEN);
scanf("%d,%f",&p->num,&p->data);
p->next=head->next;
head->next=p;
}
没能实现楼主的意图

}

全局变量和局部变量 Head 混乱 ,头节点为空等等

int print(struct list*head)
{
struct list *p1;
p1=head;
if (head!=0)
{
printf("%d,%f",p1->num,p1->data);
p1=p1->next;
}while(p1!=0);
}

main()
{

initlist(head);
creat(head,3);
print(head);
getchar();
getchar();
}

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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