先上代码
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int date;
struct Node* next;
};
struct Node* creat_link(struct Node *head)
{
head = (struct Node*)malloc(sizeof(struct Node));
head->next = NULL;//头结点不动
struct Node* tail = head;//尾节点先和头结点对齐
for (int i = 0; i < 10; i++)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); //创建临时变量接收
temp->date = i;
temp->next = NULL; //先将next设为NULL
tail->next = temp; 尾结点指向创建的变量 建立联系
tail = temp; //更新tail
}
//head ->0->0->tail
//
return head;
}
void print(struct Node* head)
{
head=head->next;
while (head!= NULL)
{
printf("%d\n", head->date);
head = head->next;
}
}
int main()
{
struct Node* head;
head=creat_link(head);//初始化
print(head);
return 0;
}
单链表的创建
最新推荐文章于 2024-06-11 07:17:12 发布