在这里插入代码片#include<stdio.h>
#include<stdlib.h>
struct test
{
int data;
struct test *next;
};
void printLink(struct test *head)
{
struct test *point;
point = head;//找一个临时工来 不然变量被修改了
while(point!=NULL)
{
if(point!=NULL)
{
printf("%d",point->data);
point=point->next;//传递到下一个链表
}
}
printf("\n");
}
int countLink(struct test *head)
{
int count=0;
struct test *point;
point = head;//找一个临时工来 不然变量被修改了
while(point!=NULL)
{
count++;
point=point->next;
}
printf("链表结点个数为%d",count);
printf("\n");
return 0;
}
int searchLink(struct test *head)
{
int searchdata;
int count=0;
struct test *point;
point = head;//找一个临时工来 不然变量被修改了
printf("请输入你要找的数据");
scanf("%d",&searchdata);
while(point!=NULL)
{ count++;
if(searchdata==point->data)
{
printf("你要找的链表结点是第%d个",count);
return 0;
}
point=point->next;
}
printf("老子没找到你说的位置!");
printf("\n");
return 0;
}
int insertLinkBehind(struct test *head,int data, struct test *newlink)//后插法
{
struct test *point;
point = head;
while(point!=NULL)
{
if(data==point->data)
{
newlink->next = point->next;
// 画图理解一下
point->next = newlink;
printf("插入成功!");
return 0;
}
point=point->next ;
}
printf("老子找不到你要的结点!");
printf("\n");
return 0;
}
struct test* insertLinkFom(struct test *head,int data, struct test *newlink)
{
struct test *point;
point = head;
if(point->data ==data)
{ newlink->next=point;
head=newlink;
return newlink;
}
//开头就是我们需要的
//开头不是我们需要的
while(point!=NULL)
{
if(data==point->next->data)
{
// 画图理解一下
printf("point->data:%d",point->data);
newlink->next=point->next; //必须先让新来的连接后一个 不然会乱 画图理解下 !!!!!!!!!!!!!!非常重要
point->next=newlink;
printf("\n");
printf("插入成功!");
printf("\n");
return newlink;
break;
}
point=point->next ;
}
printf("老子找不到你要的结点!");
printf("\n");
return 0;
}
struct test* delLink(struct test *head,int data)//后插法
{
struct test *point;
point = head;
if(data==point->data)
{
head=head->next;
printf("删除成功");
return head;
}
while(point!=NULL)
{
if(data==point->next->data)
{
point->next=point->next->next;
printf("删除成功!");
return head;
}
point=point->next ;
}
printf("老子找不到你要的结点!");
printf("\n");
return 0;
}
int main()
{
struct test t1={1,NULL};
struct test t2={2,NULL};
struct test t3={3,NULL};
struct test t4={4,NULL};
struct test newlink={1000,NULL};
t1.next=&t2;
t2.next=&t3;
t3.next=&t4;
struct test *head=NULL;
head=&t1;
printf("use t1 to print 3 words");
printf("\n");
// printf("%d %d %d",t1.data,t1.next->data,t1.next->next->data);
printf("\n");
printLink(head);
countLink(head);
//insertLinkBehind(&t1,3,&newlink);
//insertLinkFom(&head,2,&newlink);
head=delLink(head,4);
printLink(head);
countLink(head);
searchLink(head);
return 0;
}
2021-03-12 链表内容学习
最新推荐文章于 2024-03-29 16:47:33 发布