链表
  sLjOnCldvVSM 2023年11月02日 56 0

数据的创建,前插后插,遍历,计数,动态内存开辟动态创建等

链表_#define

#include <stdio.h>

#define dedata 8

struct Test
{
	int data;
	struct Test *next;
};

void Printlink(struct Test *point)
{
	//struct Test *point;
	//point = head;
	int count = 0;
	while(point != NULL)
	{
		printf("%d ",point->data);
		point = point->next;
		count++;
	}
	putchar('\n');
	printf("Total number of link is %d\n",count);

}

int search_number(struct Test *point,int data)
{
	int count = 0;
	while(point != NULL)	
	{
		count++;
		if(point->data == data)
		{
			return 1;
		}
		point = point->next;
	}
	return 0;
}

int insert_number_from_beh(struct Test *point,int insert_data,struct Test *new)
{
	while(point != NULL)
	{
		if(point->data == insert_data)
		{
			new->next = point->next;
			point->next = new;
			return 1;
		}
		point = point->next;	
	}
	return 0;
}

struct Test* insert_number_from_head(struct Test *head,int insert_data,struct Test *new)
{
	struct Test *p;
	p = head;
	if(p->data == insert_data)
	{
		new->next = p;
		head = new;
		return head;
	}	
	while(p != NULL)
	{
		if(p->next->data == insert_data)
		{
			new->next = p->next;
			p->next = new;
			printf("find it\n");
			return head;
		}
	p = p->next;

	}
	printf("Not find it\n");
}


int main()
{
	int arr[] = {1,2,3,4,5,6,7,8,9,};
	int i = 0;

//	struct Test *retr;

	for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
		printf("%d ",arr[i]);
	}
	putchar('\n');
	
	struct Test t1 = {1,NULL};
	struct Test t2 = {2,NULL};
	struct Test t3 = {3,NULL};
	struct Test t4 = {4,NULL};

	struct Test t5 = {100,NULL};
	
	struct Test t6 = {110,NULL};	
	

	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;

	printf("use t1 to print three umbers.\n");
 	//printf("%d %d %d\n",t1.data,t1.next->data,t1.next->next->data);
	Printlink(&t1);
	int findata = dedata;
	int ret = search_number(&t1,findata);
	if(ret == 1)
		printf("find the number %d\n",findata);
	else
		printf("Not find\n");


	insert_number_from_beh(&t1,4,&t5);
	Printlink(&t1);
	
	struct Test *retr = insert_number_from_head(&t1,1,&t6);
	Printlink(retr);


	return 0;


}

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  sLjOnCldvVSM   2023年11月12日   37   0   0 #include#definec++
  UYSNSBVoGd8R   2023年12月08日   26   0   0 引脚#include#define
sLjOnCldvVSM