1.链表的修改
int gaiLink(struct Test *head,int data,int newdata)
{
while(head!=NULL){
if(head->data==data){
head->data=newdata;
return 1;
}
head=head->next;
}
return 0;
}
2.链表动态创建之头插法
struct Test* insertFromHead(struct Test *head)
{
struct Test *new;
while(1){
new=(struct Test *)malloc(sizeof(struct Test));
printf("input new data\n");
scanf("%d",&(new->data));
//一直输入数据,如果输入的数据为0,则结束输入,退出循环
if(new->data==0){
printf("quit!\n");
return head;
}
//如果链表为空,则新输入的就是头节点
if(head==NULL){
head=new;
//return head;
}else{
new->next=head;
head=new;
}
}
return head;
}
3.头插法优化
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("input new data\n");
scanf("%d",&(new->data));
if(new->data==0){
printf("quit!\n");
return head;
}
head=insertFromHead(head,new);
}
}
4.尾插法创建链表
struct Test* insertBehind(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;
}
struct Test* createLink2(struct Test *head)
{
struct Test *new;
while(1){
new=(struct Test *)malloc(sizeof(struct Test));
printf("input new data\n");
scanf("%d",&(new->data));
if(new->data==0){
printf("quit!\n");
return head;
}
head=insertBehind(head,new);
}
}