链表的插入又分头部插入和尾部插入。
先说尾部插入:思路如下:
假如一个链表有5个数据,要在3后面插入一个100,先找到3这个节点,再把新节点的下一个节点指向3的下一个节点,再把3的下一个节点指向新节点就完成了插入。注意(2,3步骤不能调换,不然4就丢掉了)。
代码如下:
#include<stdio.h>
typedef struct date
{
int date;
struct date *next;
}stu,*pstu;
void printLink(stu *head)
{
stu *p=head;
while(p!=NULL){
printf("%d ",p->date);
p=p->next;
}
putchar('\n');
}
weiInsert(stu *head,int date,stu *new)
{
stu *p=head;
while(p!=NULL){
if(p->date==date){
new->next=p->next;
p->next=new;
}
p=p->next;
}
}
int main()
{
stu t1={1,NULL};
stu t2={2,NULL};
stu t3={3,NULL};
stu t4={4,NULL};
stu t5={5,NULL};
stu new1={9,NULL};
t1.next=&t2;
t2.next=&t3;
t3.next=&t4;
t4.next=&t5;
stu *head=&t1;
printLink(head);
weiInsert(head,5,&new1);
printLink(head);
return 0;
}
结果:
1 2 3 4 5
1 2 3 4 5 9
头部插入的话,会引起头部的变化,所以有俩个情况,一种是再头部前插入,所以要改头部,一种是在第二个以后的前部插入。大概思路如下:
第一种情况:改头。 让new->next =head; 再把head返回。
第二种情况: 在头部后面的前面插入。 遍历先去判断下一个节点是不是我们要找的节点,是的话,
new->next=head->next , head->next =new;
pstu touInsert(stu *head,int date,stu *new)
{
stu *p=head;
if(p->date==date){
new->next=head;
return new;
}
while(p->next!=NULL){
if(p->next->date==date){
new->next=p->next;
p->next=new;
return head;
}
p=p->next;
}
}
int main()
{
stu t1={1,NULL};
stu t2={2,NULL};
stu t3={3,NULL};
stu t4={4,NULL};
stu t5={5,NULL};
stu new1={9,NULL};
stu new2={8,NULL};
t1.next=&t2;
t2.next=&t3;
t3.next=&t4;
t4.next=&t5;
stu *head=&t1;
printLink(head);
//weiInsert(head,5,&new1);
head=touInsert(head,5,&new2);
printLink(head);
return 0;
}
结果:
1 2 3 4 5
1 2 3 4 8 5
删除指定节点:
思路:
删的是头节点的话,要注意头节点更改的返回。直接用head=head->next
如果不是头节点,head->next=head->next->next 直接绕过要删除的那个节点。
pstu delect(stu *head,int date)
{
stu *p=head;
if(p->date==date){
head=head->next;
return head;
}
while(p->next!=NULL){
if(p->next->date==date){
p->next=p->next->next;
return head;
}
p=p->next;
}
}
int main()
{
stu t1={1,NULL};
stu t2={2,NULL};
stu t3={3,NULL};
stu t4={4,NULL};
stu t5={5,NULL};
stu new1={9,NULL};
stu new2={8,NULL};
t1.next=&t2;
t2.next=&t3;
t3.next=&t4;
t4.next=&t5;
stu *head=&t1;
printLink(head);
//weiInsert(head,5,&new1);
//head=touInsert(head,5,&new2);
head=delect(head,5);
printLink(head);
return 0;
}
修改指定节点的数据:
修改头节点。 直接让新节点等于原本头节点的下一个节点。再把新节点返回。
new->next=head->next; return new;
修改不是头节点;
找到要修改的节点,新节点的下一个节点等于头节点的下一个的下一个节点,直接绕过要修改的那个数据。再让头节点的下一个等于新节点(注意:因为要找节点。头节点是移动的,可以定义一个临时变量头节点)
new->next=head->next->next;
head->next=new;
pstu gai(stu *head,int date,stu *new)
{
stu *p=head;
if(p->date==date){
new->next=head->next;
return new;
}
while(p->next!=NULL){
if(p->next->date==date){
new->next=p->next->next;
p->next=new;
return head;
}
p=p->next;
}
}
int main()
{
stu t1={1,NULL};
stu t2={2,NULL};
stu t3={3,NULL};
stu t4={4,NULL};
stu t5={5,NULL};
stu new1={9,NULL};
stu new2={8,NULL};
stu new3={7,NULL};
t1.next=&t2;
t2.next=&t3;
t3.next=&t4;
t4.next=&t5;
stu *head=&t1;
printLink(head);
//weiInsert(head,5,&new1);
//head=touInsert(head,5,&new2);
//head=delect(head,5);
head=gai(head,5,&new3);
printLink(head);
return 0;
}
查找结点就去遍历节点,在判断要找的节点跟遍历的节点一不一样。一样就代表找到了。原理跟上面差不多。很简单。
动态的头插:
#include<stdio.h>
#include<stdlib.h>
typedef struct date
{
int date;
struct date *next;
}stu,*pstu;
void printLink(pstu head){
stu *p=head;
while(p!=NULL){
printf("%d ",p->date);
p=p->next;
}
putchar('\n');
}
pstu toucha(pstu head,pstu new)
{
if(head==NULL){
head=new;
}else{
new->next=head;
head=new;
}
return head;
}
pstu creatLink(pstu head){
pstu new;
while(1){
new=(pstu)malloc(sizeof(stu));
printf("input you date\n");
scanf("%d",&new->date);
if(new->date==0){
printf("over\n");
return head;
}
head=toucha(head,new);
}
}
int main()
{
pstu head=NULL;
stu new={99,NULL};
head=creatLink(head);
head=toucha(head,&new);
printLink(head);
return 0;
}
动态尾部插入:
pstu weicha(pstu head,pstu new)
{
pstu p=head;
if(head==NULL){
head=new;
return head;
}
while(p->next!=NULL){
p=p->next;
}
p->next=new;
return head;
}