#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int score;
struct student *next;
}Linklist;
Linklist *creat(int n);//输入函数
void newprintf(Linklist *head);
void Pfree(Linklist *head);
int main()
{
int n=15;
Linklist *head;
head=creat(n);
newprintf(head);
Pfree(head);
system("pause");
return 0;
}
Linklist *creat(int n)
{
Linklist *head = NULL,*node,*p;
// for(int i=0;i<n;i++)
while(p->score!=1)
{
node=(Linklist *)malloc(sizeof(Linklist));
//scanf("%d",&node->score);
node->next = NULL;
if(head == NULL)
{
node->score=n;
head = node;
head->next = NULL;
p = head;
}
else
{
if(n%2==0)
node->score=n/2;
else
node->score=3*n+1;
n=node->score;
p->next = node;
p = node;
}
}
p->next=NULL;
return head;
}
void newprintf(Linklist *head)
{
Linklist *p = head;
if(p==NULL)
{
printf("此链表为空\n");
return;
}
while(p!=NULL)
{
printf("我是%d\n",p->score);
p=p->next;
}
}
void Pfree(Linklist *head)
{
Linklist *p = head;
while(p!=NULL)
{
p=head->next;
free(head);
head=p;
}
}
问题在哪
Linklist *creat(int n)//创造链表
{
Linklist *head=NULL,*node,*end;
end=(Linklist *)malloc(sizeof(Linklist));
end->next=NULL;
end->score=0;
for(int i=0;end->score!=1;i++)
{
node=(Linklist *)malloc(sizeof(Linklist));
//scanf("%d",&node->score);
node->next=NULL;
if(head==NULL)
{
node->score=n;
head=node;
head->next=NULL;
end=head;
}
else
{
if(n%2==0)
node->score=n/2;
else
node->score=3*n+1;
n=node->score;
end->next=node;
end=node;
}
}
end->next=NULL;
return head;
}