2021-03-12 链表内容学习

在这里插入代码片#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;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值