#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
int newincreat(struct Test *head, int data,struct Test *new) 想要指定的data 节点后边
{
struct Test *p=head;
while( p != NULL )
{
if(p->data ==data)
{
new->next=p->next; //使原来p指向的节点,和新节点同时指向
p->next=new; //让新节点 为 p 的下一个节点
return 1;
}
p=p->next;
}
return 0;
}
// **指定节点前边插入** 有两种头部 或者 非头部
struct Test *intserfromfor( struct Test *head, int data, struct Test *new)
{
struct Test *p=head;
if( p->data == data ) //data头节点插入
{
new->next=head;
return new;
}
while(p->next != NULL ) // 非头部
{
if(p->next->data == data) 想要指定的data节点前边
new->next=p->next;
p->next=new;
printf("insert ok \n");
return head;
}
p=p->next;
return head;
}
此函数返回的是指针,调用时
struct Test *head=NULL;
head=intserfromfor(head,2,&new)
//
void printflink( struct Test *head) //打印地址
{
struct Test *p;
p=head;
while(p !=NULL )
{
printf("%d ",p->data);
p=p->next;
}
putchar('\n');
}
int main()
{
struct Test *head;
struct Test new={100,NULL};
struct Test t1={1,NULL}; //创建静态链表
struct Test t2={2,NULL};
struct Test t3={3,NULL};
struct Test t4={4,NULL};
struct Test t5={5,NULL};
t1.next=&t2; //节点地址
t2.next=&t3;
t3.next=&t4;
t4.next=&t5;
head=&t1;
newincreat(head,2,&new); //选择 在data=2后面添加 新的节点
printflink(head);
system("pause");
return 0;
}
重新学习 2022 4 26
静态链表 指定节点后方 插入 新的节点
#include <stdio.h>
struct Test
{
int data;
struct Test *next;
};
int PrintfLink(struct Test *head)
{
struct Test *q=head;
while(q != NULL)
{
printf("%d ",q->data);
q=q->next;
}
return 0;
}
int InsertLinkFromdata(struct Test *head,int data ,struct Test *new)
{
struct Test *p=head;
while(p != NULL)
{
if( p->data == data)
{
new->next = p->next; // 先 new 再 P 1 2 3 new 4 5
p->next =new;
return 1; // 找到目标节点 完成插入后返回
}
p=p->next;
}
return 0;
}
int main()
{
int data;
struct Test t1 ={10,NULL};
struct Test t2 ={20,NULL};
struct Test t3 ={30,NULL};
struct Test t4 ={40,NULL};
struct Test t5 ={50,NULL};
struct Test new={100,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
printf("please input your want area \n");
scanf("%d",&data);
int flag=InsertLinkFromdata(&t1,data,&new);
if(flag == 1)
{
printf("inset success !\n");
PrintfLink(&t1);
putchar('\n');
}
else
{
printf("inser error !");
}
return 0;
}
动态创建链表 头插法和尾插法
头插
struct Test *InsertFromHead(struct Test *head)
{
struct Test *new = head;
while(1)
{
new = (struct Test *)malloc(sizeof(struct Test));
printf("please input your link\n");
scanf("%d",&(new->data));
if(new->data == 0)
{
printf("link error \n");
return head;
}
if( head == NULL)
{
head=new;
}
else
{
new->next=head;
head=new;
}
}
return head;
}
输出结果
模块化优化
struct Test *InsertFromHead(struct Test *head,struct Test *new)
{
if( head == NULL)
{
head=new;
}
else
{
new->next=head;
head=new;
}
return head;
} //头插模块化
struct Test *createLink(struct Test *head)
{
struct Test *new ;
while(1)
{
new = (struct Test *)malloc(sizeof(struct Test));
printf("please input your link\n");
scanf("%d",&(new->data));
if(new->data == 0)
{
printf("link finish \n");
free(new);
return head;
}
head=InsertFromHead(head,new);
}
}
尾插
struct Test *insertlinkBehind(struct Test *head ,struct Test *new)
{
struct Test *p=head;
if(p == NULL)
{
head=new;
return head;
}
while(p->next != NULL)
{
p=p->next;
}
p->next=new;
return head;
}
输出结果