mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5mobile wallpaper 6
559 字
1 分钟
链表——头部插入节点
2026-03-28

链表:在头部插入节点#

在这篇文章里,我将带大家实现操作链表的功能。使用的语言仍然是c/c++。

一、在头部插入一个节点#

#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node* next;
} Node;
struct Node* head;//这里声明了全局变量!
//跳过这个Insert,先往下看
void Insert(int x);
//跳过这个Print,先往下看
voit Print();
int main(){
head=NULL;//到目前为止,我们只有一个指向null的head指针
printf("How many numbers?\n");
int n,i,x;
scanf("%d",&n);//输入一个数保存在n中
for(i=0;i<n;i++){
printf("Enter the number \n");
scanf("%d",&x)//用x存储
Insert(x);//我们要实现,在头部插入一个节点
Print()://这里要实现打印完整链表
}
}

还没进入循环时,长这样。

ada

现在,来让我们实现Insert的逻辑吧

首先先创建一块内存并用temp指着,假设内存地址是100,阴影部分为用户输入的数字,要实现下面的操作。

aq

void Insert(int x){
Node* temp=(Node*)malloc(sizeof(struct Node));
temp->data=x;
temp->next=NULL;
head=temp;
}

head=temp后,temp可以被简化掉了,我们在插入个数字,假设地址是150,要插到头部。

aqq

这显然不对劲!我们只实现了temp和head之间的关系,而红色那条却没有实现!因此我们还需要实现新建的节点和之前建好的节点之间的关联。

我们回想一下链表的定义,既然要把新的Node节点里塞之前的Node的地址,那么就必须找到那个地址吧?在哪呢?

很显然就在head里!😎

我们改进一下c的代码

void Insert(int x){
Node* temp=(Node*)malloc(sizeof(struct Node));
temp->data=x;
temp->next=NULL;
if(head!=NULL)temp->next=head;//使得temp的next指向了被插在后面的那个节点的地址😋
head=temp;
}

然后就变成了这样,我们依然省略掉temp,把他放一边!

qqq

二、优化#

这里我们还可以在优化一下我们的代码

//if(head!=NULL)temp->next=head;和temp->next=NULL;可以合并起来,变成temp->next=head;
void Insert(int x){
Node* temp=(Node*)malloc(sizeof(struct Node));
temp->data=x;
temp->next=head;
head=temp;
}

if(head!=NULL)temp->next=head;目的是什么?

无非就是检查一下是不是空链表嘛,既然头指针指向无效地址,那不就一定是空链表了吗!😎

所以直接temp->next=head即使head是NULL也无所谓!

三、实现打印功能#

好啦,现在我们要做的就只剩打印了!

为啥还要用零食变量,依旧是不像head被污染🐧

void Print(){
struct Node *temp=head;
printf("链表是:");
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->next;
}
printf("\n");
}

四、完整c代码#

#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
struct Node* head;//这里声明了全局变量!
void Insert(int x){
Node* temp=(Node*)malloc(sizeof(struct Node));
temp->data=x;
temp->next=head;
head=temp;
};
void Print(){
struct Node *temp=head;
printf("链表是:");
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->next;
}
printf("\n");
};
int main(){
head=NULL;//到目前为止,我们只有一个指向null的head指针
printf("How many numbers?\n");
int n,i,x;
scanf("%d",&n);//输入一个数保存在n中
for(i=0;i<n;i++){
printf("Enter the number \n");
scanf("%d",&x);//用x存储
Insert(x);//我们要实现,在头部插入一个节点
Print();//这里要实现打印完整链表
}
return 0;
};

五、c++实现#

还记得我们利用了全局变量head吗?那么如何在不使用全局变量的情况下,实现链表的添加呢?🤔

#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
Node* Insert(Node* head,int x){
Node* temp=(Node*)malloc(sizeof(struct Node));//这里可以用new
temp->data=x;
temp->next=head;
head=temp;
return head;
};
void Print(Node* head){
printf("链表是:");
while(head!=NULL){
printf("%d",head->data);
head=head->next;
}
printf("\n");
};
int main(){
Node* head=NULL;//到目前为止,我们只有一个指向null的head指针
printf("How many numbers?\n");
int n,i,x;
scanf("%d",&n);//输入一个数保存在n中
for(i=0;i<n;i++){
printf("Enter the number \n");
scanf("%d",&x);//用x存储
head = Insert(head,x);//我们要实现,在头部插入一个节点
Print(head);//这里要实现打印完整链表
}
return 0;
};

既然做不了全局变量, 那我们就将他作为函数的参数嘛😋

c++的函数参数和python有些不一样,他是会复制一个副本当作参数!

Isert函数第二种改法就是引用传递

void Insert(Node** pointerToHead,int x){
Node* temp=(Node*)malloc(sizeof(struct Node));//这里可以用new
temp->data=x;
temp->next=NULL;
if(*pointerToHead !=NULL)temp->next=*pointerToHead;
*pointerToHead=temp;
};
int main(){
Node* head=NULL;//到目前为止,我们只有一个指向null的head指针
printf("How many numbers?\n");
int n,i,x;
scanf("%d",&n);//输入一个数保存在n中
for(i=0;i<n;i++){
printf("Enter the number \n");
scanf("%d",&x);//用x存储
Insert(&head,x);//我们要实现,在头部插入一个节点
Print(head);//这里要实现打印完整链表
}
return 0;
};
分享

如果这篇文章对你有帮助,欢迎分享给更多人!

链表——头部插入节点
https://fatdog.20060113.xyz/posts/lianbiao-insert-first/
作者
神秘大胖狗
发布于
2026-03-28
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

封面
Sample Song
Sample Artist
封面
Sample Song
Sample Artist
0:00 / 0:00