链表:插入数据
开头插入
插入数据方法
方法1
struct Node* temp=(struct Node*)malloc(sizeof(struct Node)); //创建新的内存空间
temp->date=x; //赋予你要插入的值X
temp->next=NULL; //让新的节点的后继指针指向NULL
if(head!=NULL)
{
temp->next=head;
} //如果头指针head不等于空,则说明链表里面已经有元素了,不能直接让头指针等于该节点,应该让该节点的后继指针指向head指向的值,因为之前head指向的第一个节点,现在又新建节点指向第一个节点,所以第一个节点变成了第二个节点,该新节点成了第一个节点。如果把链表比作是用桥连接的岛,那么节点就是小岛,而head只是一架桥(指针指向指针就像桥直接连接了桥,只有岛才是数据)
head=temp; //现在又重新将头指针head指向第一个节点,也就是该节点,这样又重新让head指向了第一个节点。如果head最开始指向空,也就是链表里面没有元素,那新的元素就是第一个元素,直接让head指向该元素就可以了
方法2
Node* temp=(struct Node*)malloc(sizeof(struct Node));
temp->date=x;
temp->next=head;
head=temp;
//该方法是直接让新建节点的后继指针指向头指针head指向的值,如果链表里面不为空,则因为之前head指向的第一个节点,现在又新建节点指向第一个节点,所以第一个节点变成了第二个节点,该新节点成了第一个节点。如果head最开始指向空,也就是链表里面没有元素,那新的元素就是第一个元素,直接让head指向该元素就可以了
打印数据
void Print()
{
struct Node* temp=head;//从头开始打印数据
printf(“链表为: “);
while(temp!=NULL)//直到打印到空时没有数据了
{
printf(“%d “,temp->date);
temp=temp->next;
}
printf(“\n\n”);
}
释放链表的所有节点内存
手动释放空间,如果没有该语句则在程序关闭时自己释放空间,释放空间后最好将节点=NULL,这样可以方便检查是否为NULL来确定节点是否已被释放,还能防止下次不小心调用到节点的时候出错,这种做法有助于提高程序的健壮性和稳定性。
while (head != NULL)
{
struct Node* temp = head;
head = head->next;
free(temp);
temp=NULL;
}
手动释放内存并将指针设置为NULL是一个良好的编程习惯,可以有效地管理内存,并且能够增加代码的可读性和可维护性。
完整版
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int date;
struct Node* next;
};
struct Node* head;//头指针
void Insert(int x)//插入
{
//方法1
struct Node* temp=(struct Node*)malloc(sizeof(struct Node));
temp->date=x;
temp->next=NULL;
if(head!=NULL)
{
temp->next=head;
}
head=temp;
//方法2
/*
Node* temp=(struct Node*)malloc(sizeof(struct Node));
temp->date=x;
temp->next=head;
head=temp;
*/
}
void Print()//打印
{
struct Node* temp=head;
printf(“链表为: “);
while(temp!=NULL)
{
printf(“%d “,temp->date);
temp=temp->next;
}
printf(“\n\n”);
}
int main ()
{
head=NULL;
int x,n,i;
printf(“您想输入多少个数字?\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“请输入数据\n”);
scanf(“%d”,&x);
Insert(x);
Print();
}
// 释放链表的所有节点内存
while (head != NULL)
{
struct Node* temp = head;
head = head->next;
free(temp);
}
return 0;
}
任意位置插入数据
插入数据方法
void Insert(int date,int n)//date表示插入的数据,n表示插入的位置
{
struct Node* temp1=(struct Node*)malloc(sizeof(struct Node));
temp1->date=date;
temp1->next=NULL;
if(n==1)//在头部插入
{
temp1->next=head;//在头部插入的时候用的是头部插入数据法二
head=temp1;
return;
}
struct Node* temp2=head;//如果不是在第一个位置插入数据则再新建一个节点
int i;
for(i=0;i<n-2;i++)//如果要插在第n个节点,则要先找到第n-1个节点。如果要插在第5个节点,则要先找到第4个节点,进行3次循环
temp2=temp2->next; //到达n-1个节点

temp1->next=temp2->next;新节点的后继指针指向第n个节点
temp2->next=temp1; 第n-1个节点的后继指针指向新的节点,则新的节点成功成为第n个数据,插入数据成功

}
打印数据
void Print()
{
printf(“链表为:”);
struct Node* temp=head;
while(temp!=NULL)
{
printf(“%d “,temp->date);
temp=temp->next;
}
printf(“\n\n”);
}
手动释放数据
while (head != NULL)
{
struct Node* temp = head;
head = head->next;
free(temp);
temp=NULL;
}
##完整版
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int date;
struct Node* next;
};
struct Node* head;
void Insert(int date,int n)
{
struct Node* temp1=(struct Node*)malloc(sizeof(struct Node));
temp1->date=date;
temp1->next=NULL;
if(n==1)//在头部插入
{
temp1->next=head;
head=temp1;
return;
}
struct Node* temp2=head;
int i;
for(i=0;i<n-2;i++)
temp2=temp2->next;
temp1->next=temp2->next;
temp2->next=temp1;
}
void Print()
{
printf(“链表为:”);
struct Node* temp=head;
while(temp!=NULL)
{
printf(“%d “,temp->date);
temp=temp->next;
}
printf(“\n\n”);
}
int main ()
{
head=NULL;
Insert(5,1);//5
Insert(3,2);//5 3
Insert(4,1);//4 5 3
Insert(1,3);//4 5 1 3
Print();
// 释放链表的所有节点内存
while (head != NULL)
{
struct Node* temp = head;
head = head->next;
free(temp);
temp=NULL;
}
return 0;
}

