关于C语言建立动态链表的问题。在线等。急急急!!!
#include"stdio.h"#include"stdlib.h"#include"string.h"structstudent{intnum;charname[20...
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct student
{
int num;
char name[20];
int age;
struct student *next;
};
typedef struct student st;
st * create()
{
int i;
st *head,*p;
head=p=(st *)malloc(sizeof(st));
scanf("%d%s%d",&(p->num),p->name,&(p->age));
for(i=0;i<4;i++)
{
p->next=(st *)malloc(sizeof(st));
p=p->next;
scanf("%d%s%d",&(p->num),p->name,&(p->age));
}
p->next=NULL;
return head;
}
print(st *head)
{
while(head!=NULL)
{
printf("\n%d %s %d",head->num,head->name,head->age);
head=head->next;
}
}
main()
{
st *head;
head=create();
print(head);
getch();
}
就是一开始开辟的P1 和P2。。那P2对的那快结构体的地址里面不是就没有保存数据了么? 展开
#include "stdlib.h"
#include "string.h"
struct student
{
int num;
char name[20];
int age;
struct student *next;
};
typedef struct student st;
st * create()
{
int i;
st *head,*p;
head=p=(st *)malloc(sizeof(st));
scanf("%d%s%d",&(p->num),p->name,&(p->age));
for(i=0;i<4;i++)
{
p->next=(st *)malloc(sizeof(st));
p=p->next;
scanf("%d%s%d",&(p->num),p->name,&(p->age));
}
p->next=NULL;
return head;
}
print(st *head)
{
while(head!=NULL)
{
printf("\n%d %s %d",head->num,head->name,head->age);
head=head->next;
}
}
main()
{
st *head;
head=create();
print(head);
getch();
}
就是一开始开辟的P1 和P2。。那P2对的那快结构体的地址里面不是就没有保存数据了么? 展开
展开全部
st * create()
{
int i;
st *head,*p, *node;
head=p=(st *)malloc(sizeof(st));
if( head != NULL )
{
scanf("%d%s%d",&(head->num),head->name,&(head->age));
head->next = NULL;
}
p = head;
for(i=0;i<4;i++)
{
node=(st*)malloc(sizeof(st));
if( node != NULL )
{
scanf("%d%s%d",&(node->num),node->name,&(node->age));
node->next = NULL;
p->next = node;
p = node;
}
}
return head;
}
在循环里面每次创建一个新节点,用一个指针来把他们串连起来。你把这个方法替换,然后调试下。
{
int i;
st *head,*p, *node;
head=p=(st *)malloc(sizeof(st));
if( head != NULL )
{
scanf("%d%s%d",&(head->num),head->name,&(head->age));
head->next = NULL;
}
p = head;
for(i=0;i<4;i++)
{
node=(st*)malloc(sizeof(st));
if( node != NULL )
{
scanf("%d%s%d",&(node->num),node->name,&(node->age));
node->next = NULL;
p->next = node;
p = node;
}
}
return head;
}
在循环里面每次创建一个新节点,用一个指针来把他们串连起来。你把这个方法替换,然后调试下。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询