C语言学习(十五)—链表初步学习

一、前言

本文用来记录自己的学习过程,主要是C语言中链表的初步学习。包括数组与链表、链表的使用、链表的遍历、链表的插入。

二、数组与链表

  • 数组中的每个元素地址是按顺序排列的,遍历时,可以直接使用指针++,进行地址的后推遍历数组。
  • 而使用结构体struct时,在添加或者删除某一个结构体节点时数组操作起来较为不便,因此使用链表使得原本地址不相邻的各个结构体按照制定的顺序进行指向,利用结构体struct中定义的指针指向下一节点。
#include <stdio.h>

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

int main()
{
	int i;
	int arrary[]={0,1,2,3,4,5,6,7,8,9,10};
	for(i=0;i<(sizeof(arrary)/sizeof(arrary[0]));i++){
		printf("%d",arrary[i]);
	}
	putchar('\n');

	struct Test t1={1,NULL};
	struct Test t2={2,NULL};
    struct Test t3={3,NULL};

	t1.p=&t2;
	t2.p=&t3;

    printf("use t1 print three numbers :%d %d %d",t1.data,t1.p->data,t1.p->p->data);

三、链表的使用

  • 对链表进行遍历、计数、查询时。传入头节点的地址,利用初始化的末尾节点地址NULL作为判断条件进行链表的遍历,并且在循环中进行下一节点指针的指向。
//遍历链表
void BianLi(struct Test *head )
{
	while(1){

		if( head != NULL){
			printf("%d",head->data);
			head=head->p;
		}
		else
		{
			break;
		}
	}
	putchar('\n');
}

//链表计数
int recordNumber(struct Test *head)
{
	int ret=0;

	  while(1)
	  {

             if( head != NULL)
	     {
                ret=ret+1;
                head=head->p;
                  }
	     else{
	     	break;
	     }
	  }	  
       	  return ret;

}

//链表查询
int reaschLine(struct Test *head,int ret)
{
	while(1){
		if(head !=NULL)
		{
			 if(head->data = ret)
			 {
				 return 1;
			 }
			 head=head->p;
		}
	}
	return 0;
}

四、链表的插入

1、指定点后插入链表

//指定点后插入链表
int joinInLineExcel(struct Test *head,int i,struct Test *New)
{
	while(1)
	{
		if (head !=NULL)
		{
			if(head->data==i)
			{
				New->p=head->p;
				head->p=New;
				return 1;
			}
			head=head->p;
		}
		else
		{	return 0;
			break;
		}
	}
}

2、指定点前插入链表

//指定点前面插入链表
struct Test * joinInlineExcelfor(struct Test *head,int i,struct Test *New)
{
	struct Test *point=head;

	if(head->data == i)
	{
		New->p=head;
		return New;
	}
	
 	while(point->p != NULL)
	{
		if(point->p->data ==i)
		{	
			New->p=point->p;//新节点指针指向目标节点
			point->p=New;//目标前节点指向新节点
			printf("find this number and insert ok  %d\n",i);
			return head;
		}
		point=point->p;
	}
	printf("no this number :%d\n",i);
	return head;}

五、链表节点的删除

//链表中删除某一节点
struct Test * deleteNode(struct Test *head,int i)
{
	struct Test *point=head;

	if(head->data == i)
	{
		return head->p;
	}
	
 	while(point->p != NULL)
	{
		if(point->p->data ==i)
		{	
			point->p=point->p->p;//新节点指针指向目标节点
			printf("delete this number and insert ok  %d\n",i);
			return head;
		}
		point=point->p;
	}
	printf("no this number :%d\n",i);
	return head;
}

示例源码

#include <stdio.h>

struct Test
{ 
	int data;
	struct Test *p; 
};
//遍历链表
void BianLi(struct Test *head )
{
	while(1){

		if( head != NULL){
			printf("%d",head->data);
			head=head->p;
		}
		else
		{
			break;
		}
	}
	putchar('\n');
}

//链表计数
int recordNumber(struct Test *head)
{
	int ret=0;

	  while(1)
	  {

             if( head != NULL)
	     {
                ret=ret+1;
                head=head->p;
                  }
	     else{
	     	break;
	     }
	  }	  
       	  return ret;

}

//链表查询
int reaschLine(struct Test *head,int ret)
{
	while(1){
		if(head !=NULL)
		{
			 if(head->data = ret)
			 {
				 return 1;
			 }
			 head=head->p;
		}
	}
	return 0;
}

//指定点后面插入链表
int joinInLineExcel(struct Test *head,int i,struct Test *New)
{
	while(1)
	{
		if (head !=NULL)
		{
			if(head->data==i)
			{
				New->p=head->p;
				head->p=New;
				return 1;
			}
			head=head->p;
		}
		else
		{	return 0;
			break;
		}
	}
}

//指定点前面插入链表
struct Test * joinInlineExcelfor(struct Test *head,int i,struct Test *New)
{
	struct Test *point=head;

	if(head->data == i)
	{
		New->p=head;
		return New;
	}
	
 	while(point->p != NULL)
	{
		if(point->p->data ==i)
		{	
			New->p=point->p;//新节点指针指向目标节点
			point->p=New;//目标前节点指向新节点
			printf("find this number and insert ok  %d\n",i);
			return head;
		}
		point=point->p;
	}
	printf("no this number :%d\n",i);
	return head;
}



//链表中删除某一节点
struct Test * deleteNode(struct Test *head,int i)
{
	struct Test *point=head;

	if(head->data == i)
	{
		return head->p;
	}
	
 	while(point->p != NULL)
	{
		if(point->p->data ==i)
		{	
			point->p=point->p->p;//新节点指针指向目标节点
			printf("delete this number and insert ok  %d\n",i);
			return head;
		}
		point=point->p;
	}
	printf("no this number :%d\n",i);
	return head;
}

int main()
{
	int i;
	int arrary[]={0,1,2,3,4,5,6,7,8,9,10};
	for(i=0;i<(sizeof(arrary)/sizeof(arrary[0]));i++){
		printf("%d",arrary[i]);
	}
	putchar('\n');

	struct Test t1={1,NULL};
	struct Test t2={2,NULL};
	struct Test t3={3,NULL};
	struct Test New={99,NULL};
	struct Test New2={101,NULL};

	t1.p=&t2;
	t2.p=&t3;

//	printf("use t1 print three numbers :%d %d %d",t1.data,t1.p->data,t1.p->p->data);
	BianLi(&t1);
	int ret= recordNumber(&t1);
	printf("Total Line Excel numbers is :%d \n",ret);

	int ii = reaschLine(&t1,1);
	if (ii==1){
	printf("Line have 1 ");
	}
	putchar('\n');

	joinInLineExcel(&t1,1,&New);
	printf("output join in line:\n");
	BianLi(&t1);

	struct Test *head=joinInlineExcelfor(&t1,99,&New2);
	printf("insert after:");
	BianLi(head);

        head = deleteNode(&t1,99);
	printf("Delete After:");
	BianLi(head);
	return 0;
}

程序运行

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值