题目:建立链表,并实现链表反向链接
调整的地方有点多,反转输出这块你都没实现,简单用递归实现了一下
#include <stdio.h>
#include <stdlib.h>
typedef struct nodef
{
int data;
struct nodef* next;
} *linklist;
linklist nailcreate(int n, linklist head)
{
linklist node, p;
p = head;
int i = 0;
while (true)
{
i++;
// head 节点你外面已经申请了空间,还没用呢,先把 head 用掉
scanf("%d", &p->data);
if (i < n)
{
// 申请新的内存地址,初始化 node 节点
node = (linklist)malloc(sizeof(linklist));
node->next = NULL;
p->next = node;
p = node;
}
else
{
break;
}
}
p->next = NULL;
return head;
}
void output(linklist head)
{
//通过函数递归来来反向打印
if (head->next != NULL)
{
output(head->next);
}
printf("%d ", head->data);
}
int main()
{
linklist head;
int n;
head = (linklist)malloc(sizeof(linklist));
head->next = NULL;
printf("请输入链表长度:");
scanf("%d", &n);
printf("请输入链表:");
nailcreate(n, head);
printf("反转后的链表:");
output(head);
return 0;
}