/*
* 动态链表——头插法
* 1、创建结构体
* 2、创建新节点
* 3、
*/
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* NewNodeList(struct Node* head)
{
struct Node* new;
new = (struct Node*)malloc(sizeof(struct Node));
new->next = NULL;
printf("请输入新节点数据:\n");
scanf("%d",&new->data);
if(head == NULL)
{
head = new;
}
else
{
new->next = head;
head = new; //新节点成为新的头
}
return head;
}
void bianli(struct Node* head)
{
while(head != NULL)
{
printf("%d ",head->data); //这里的输出必须在前面,否则链表头将不会被打印。
head = head->next;
}
}
int main()
{
struct Node* head = NULL;
head = NewNodeList(head);
head = NewNodeList(head);
head = NewNodeList(head);
bianli(head);
}
C语言复习——链表
于 2023-11-10 15:23:38 首次发布