链表的增删改查以及动态链表的创建

链表

链表引入

前不久遇到一个比较有意思的课题,课题如下:

1.定义一维数组,长度为realsize(20);

2.随机生成realsize个两位整数,存放在数组中,并输出查看;

3.求数组中的最大值和最小值,交换最大值和最小值的位置后再输出调整后的数组;

4.求数组的平均值,并输出查看;

5.将数组按升序排序,然后输出查看排序后的结果(选择排序和冒泡排序均可)。

6.在数组中用二分查找法查找数值key(48),若找到则给出提示信息,若没有找到则插入这个数。

刚开始看到将查找不到的数据存放到数组里,这个操作对于已经确定好数组大小是比较困难的,因为数组在定义的时候就确定了大小。所以如果你刚开始就开辟一个比它长度大1的空间,这个空间其实是有些浪费的。后来我想到了使用malloc函数去动态的开辟空间,如果不够我就使用realloc函数就重新分配空间,相比较直接去改变数组的大小还是比较容易的。代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define realsize 20
#define INIT_CMD 		 1
#define GET_MAX_MIN_CMD  2
#define GET_AVERAGE_CMD  3
#define SORT_ARRAY_CMD 	 4
#define SEARCH_DATA_CMD  5
#define QUIT_CMD 		 6

int* Init_arr(int *parr)
{
	int i = 0;

	parr = (int *)malloc(realsize * sizeof(int));	//malloc动态开辟空间大小
	memset(parr,'\0',sizeof(int *));
	if(parr == NULL)
	{
		printf("failed to malloc\n");
		return NULL;
	}

	srand(time(NULL));	//设置随机数种子
	for(i=0;i<realsize;i++)
	{
		parr[i] = rand() % 90 + 10;		//rand根据种子生成对应的随机数
	}

	return parr;
}

void Print_arr(int *parr, int len)
{
	int i = 0;

	for(i=0;i<len;i++)
	{
		printf("%d ",*(parr+i));
	}
	printf("\n");
}	

void GET_MAX_MIN_Number(int *parr, int len, int *max_index, int *min_index,int *max, int *min)
{
	int i;

	*max = *parr;
	*min = *parr;

	for(i=0;i<len;i++)
	{
		if(*max < parr[i])
		{
			*max = parr[i];
			*max_index = i;
		}
		if(*min > parr[i])
		{
			*min = parr[i];
			*min_index = i;
		}
	}
}

void Exchange_order(int *parr, int len, int max_index, int min_index)
{
	int tmp;

	tmp = parr[max_index];
	parr[max_index] = parr[min_index];
	parr[min_index] = tmp;
}

float GET_AVERAGE_array(int *parr, int len)
{
	int i = 0;
	int sum = 0;
	float average = 0.0;

	for(i=0;i<len;i++)
	{
		sum += parr[i];
	}
	average = (float)sum / len;

	return average;
}

void Selection_Sort_array(int *parr, int len)
{
	int i,j,tmp;

	for(i=0;i<len-1;i++)
	{
		for(j=i+1;j<len;j++)
		{
			if(parr[i] > parr[j])
			{
				tmp = parr[i];
				parr[i] = parr[j];
				parr[j] = tmp; 
			}
		}
	}
}

void Dichotomy_array(int *parr, int len, int *mid, int key, int *mark)
{
	int left = 0,right = len - 1;		//首先确定数组的两边,左边元素的下标是0,右边元素的下标是数组大小减一

	while(left <= right)
	{
		*mid = (left + right) / 2;		//获取中间的数据,然后和用户设定值进行比较

		if(parr[*mid] == key)
		{
			*mark = 1;			//如果相同就说明找到了,将标志位置为1同时跳出循环
			break;
		}
		else if(parr[*mid] < key)	//如果中间值比设定值小,那么就说明设定值位于中间值的右边,将左下标调整为当前中间值的下一个元素的下标
		{
			left = *mid + 1;
		}
		else if(parr[*mid] > key)	//如果中间值比设定值大,那么就说明设定值位于中间值的左边吗,将右下标调整为当前中间值的上一个元素的下标
		{
			right = *mid - 1;
		}
	}
}

int* Add_New_element(int *parr, int len, int key)
{
	parr = (int *)realloc(parr,(len+1) * sizeof(int));		//使用函数realloc重新分配空间,大小为realsize+1
	if(parr == NULL)
	{
		printf("failed to realloc\n");
		return NULL;
	}	

	parr[len] = key;

	return parr;
}

int main(void)
{
	int *arr = NULL;
	int max = 0,min = 0,max_index = 0,min_index = 0;
	int mark = 0,mid = 0,key;
	int CMD;

	while(1)
	{
		puts("please enter the CMD");
		if (scanf("%d", &CMD) != 1)
        {
            printf("Invalid command. Please enter a number.\n");
            while (getchar() != '\n'); // clear the input buffer
            continue;
        }
        
		switch(CMD)
		{
			case INIT_CMD:
				arr = Init_arr(arr);
				puts("============================================================");
				printf("获取到的%d个两位随机数如下\n",realsize);
				Print_arr(arr,realsize);
				puts("============================================================");
				break;

			case GET_MAX_MIN_CMD:
				if(arr == NULL)
				{
					printf("请先初始化数组\n");
					continue;
				}
				puts("============================================================");
				GET_MAX_MIN_Number(arr,realsize,&max_index,&min_index,&max, &min);
				printf("数组中最大值为:max = %d,对应的下标是max_index = %d\n",max,max_index+1); 
				printf("数组中最小值为:min = %d,对应的下标是min_index = %d\n",min,min_index+1);
				puts("=====================原始数据是:============================");
				Print_arr(arr,realsize);
				puts("=====================交换后数据是:==========================");
				Exchange_order(arr,realsize,max_index,min_index);
				Print_arr(arr,realsize);
				break;

			case GET_AVERAGE_CMD:
				if(arr == NULL)
				{
					printf("请先初始化数组\n");
					continue;
				}
				puts("============================================================");
				printf("数组中%d个元素的平均值为:average = %.2f\n",realsize,GET_AVERAGE_array(arr,realsize));
				puts("============================================================");
				break;

			case SORT_ARRAY_CMD:
				if(arr == NULL)
				{
					printf("请先初始化数组\n");
					continue;
				}
				puts("============================================================");
				puts("原始数据是:");
				Print_arr(arr,realsize);
				puts("选择排序后的数据为:");
				Selection_Sort_array(arr,realsize);
				Print_arr(arr,realsize);
				puts("============================================================");
				break;

			case SEARCH_DATA_CMD:
				if(arr == NULL)
				{
					printf("请先初始化数组\n");
					continue;
				}

				key = 0;
				mark = 0;

				puts("============================================================");
				puts("请输入你想查找的数");
				scanf("%d",&key);

				Selection_Sort_array(arr, realsize); 	//确保在二分法查找之前数组是有序序列
				Dichotomy_array(arr,realsize,&mid,key,&mark);
				if(mark == 1)
				{
					printf("successfully find this data and the location is %d\n",mid+1);
				}
				else
				{
					printf("can't find this data\n");
					puts("插入新元素后的数组是");
					puts("===============================================================");
					arr = Add_New_element(arr,realsize,key);
					Print_arr(arr,realsize+1);
				}
				puts("===============================================================");
				break;

			case QUIT_CMD:
				puts("QUIT");
				free(arr);
				exit(EXIT_SUCCESS);
				break;

			default:
				puts("==================指令错误,请重新输入!!!=================");
				break;
		}
	}

	return 0;
}

经过上边的代码不难看出如果想要去在一个已有的数列里边添加数据是比较困难的,那么有没有一种数据的存放方式是比较灵活的。那自然是有的,那就是链表链表的存放方式其实是无序的,它的增加一个元素也只需要将这个元素加上去而已。由于数组的特性是它的地址是连续的,所以我们并不方便在数组上动手脚。因为如果一旦添加元素那么就必须要对它的地址进行操作,那么它的工作量是比较大的。虽然使用指针也能满足要求,但是可见还是没有那么灵活的添加元素,下边就引入链表,来看一下链表对于数据的添加是多么简单。

链表说明白了就是数据的一种存放方式。在C语言中它基本与结构体无异。那么如果去定义一个链表?其实就是在之前定义结构体的基础上多了一个指向自己的指针。所以链表可以定义为:

struct Link
{
    int idata;
    struct Link *next;
};

链表这个名字最重要的就是“链”,那么如何去体现这个字的重要性呢?,就拿普通变量来说,如果定义了struct Link s1struct Link s2struct Link s3等变量后,它们里边每一项元素都包含一个整型变量和一个指向自己的指针。那么相信大家都知道指针是用来保存地址的变量,所以就靠这个地址来进行链接。使用上一个结构体变量的s1.next去保存下一个变量&s2的地址。以此类推,直到结构体变量中的最后一个元素,它指向的地址是空。这也侧面反映出来链表的存储方式是十分灵活的,所以可以多使用链表。下面就对链表进行初始化、打印等操作:

链表的静态创建

#ifndef __LINK__H
#define __LINK__H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

struct Link
{
	int idata;
	struct Link *next;
};

void Judgement_the_number_of_parameters(int parameters);
void Init_struct_array_With_Random_Function(struct Link *psarr, int size);
void Print_Link_Function(struct Link *psarr);
void Counting_the_number_of_Link_Function(struct Link *psarr, int *cnt);
char * Find_the_data_of_Link_Function(struct Link *psarr, char *debug_information, int *position);
char* Change_data_of_Link_Function(struct Link *head, char *debug_information);
struct Link * Insert_data_from_Forward(struct Link *head, struct Link *new, char **debug_information);
char * Insert_data_from_behind(struct Link *head, struct Link *new, char *debug_information);
struct Link * Delete_the_node_lists_of_Link(struct Link *head, char **debug_information);


#endif 
#include "link.h"

void Init_struct_array_With_Random(struct Link *psarr, int size)
{
	int i;

	memset(psarr,0,size);		//清空结构体数组
		
	srand((unsigned int)time(NULL));
	for(i=0;i<size;i++)
	{
		(psarr+i)->idata = rand() % 100;

		if(i+1 <size)
		{
			(psarr+i)->next = (psarr+1+i);
		}
		else
		{
			(psarr+i)->next = NULL;
		}
	}
}

void Init_struct_array_With_Scanf(struct Link *psarr, int size)
{
	int i;

	memset(psarr,0,size);

	for(i=0;i<size;i++)
	{
		printf("please enter the %d number\n",i+1);
		scanf("%d",&((psarr+i)->idata));

		if(i+1 < size)
		{
			(psarr+i)->next = (psarr+i+1);
		}
		else
		{
			(psarr+i)->next = NULL;
		}
	}
}

void Print_link_Function(struct Link *psarr)
{
	while(psarr != NULL)
	{
		printf("%d ",psarr->idata);
		psarr = psarr->next;
	}
	puts("");
}

#include "link.h"

int main()
{
	struct Link sarr[6];
	int size = sizeof(sarr)/sizeof(sarr[0]);

	Init_struct_array_With_Random(sarr,size);
//	Init_struct_array_With_Scanf(sarr,size);

	Print_link_Function(sarr);

	return 0;
}

以上就是关于链表的遍历,那么说到链表肯定最常见就是链表的增删改查,那么下边以代码进行详细的解释

统计链表结点个数

/****************************************************
 * @Function Description
 * @breif 使用遍历数组的功能能同时来统计链表的节点个
 * 数,利用的原理还是链表节点最后一个元素的next为NULL
 * @param psarr是指向结构体数组的指针,*cnt是指向main
 * 函数中的cnt的指针,通过指针来修改main函数中的值
 * @retval NONE
 * *************************************************/
void Counting_the_number_of_Link_Function(struct Link *psarr, int *cnt)
{
	while(psarr)
	{
		(*cnt)++;
		psarr = psarr->next;
	}
}

修改链表里边的元素

/****************************************************
 * @Function Description
 * @breif 链表的查找就是从main函数里传一个数过来然后
 * 遍历链表判断链表里边的每一个值是否和要找的值相同,
 * 如果找到这个数那么就将调试信息返回在main函数中打印,
 * 并且将这个值在链表中的位置打印出来,如果没有这个值
 * 就将position置为-1表示没有这个值
 * @param psarr是指向结构体数组的指针,debug_information
 * 是从main函数传的一个用来打印调试信息的一个指针,在
 * 被调函数中通过malloc动态分配空间,然后在主函数中将
 * 其释放防止造成内存泄漏
 * @retval char *debug_information	通过返回值来修改main
 * 函数中的指向,在使用完以后就释放掉
 * *************************************************/
char * Find_the_data_of_Link_Function(struct Link *psarr, char *debug_information, int *position)
{
	int Find_data;
	
	debug_information = (char *)malloc(sizeof(char) * 64);
	if(!debug_information)
	{
		printf("malloc error\n");
		exit(EXIT_FAILURE);
	}

	puts("please enter which data do you want to find?");
	scanf("%d",&Find_data);

	while(psarr)
	{
		if(psarr->idata == Find_data)
		{
			strcpy(debug_information,"successfully find this data");
	
			return debug_information;
		}
		psarr = psarr->next;
		(*position)++;
	}
	strcpy(debug_information,"failed to find this data");
	*position = -1;

	return debug_information;
}

链表从数据的前方插入

/****************************************************
 * @Function Description
 * @breif	通过遍历链表后来查找用户需要在哪里插入这
 * 个值,然后同样要获取用户的要插入的值,插入后判断插
 * 入的值和用户需要插入的值是否相等,如果相等就说明插
 * 入成功,那么就通过指向调试信息的指针修改里边的内容
 * 并打印出来。这里从前方插入数据共有两种情况,一种直
 * 接在头部插入另外一种直接在其他数据前方插入,那么就
 * 涉及到可能需要修改链表的头,所以这里的返回值是一个
 * 指向结构体的指针,而调试信息使用二级指针的方法去访
 * 问。同时也要注意释放动态开辟出来的空间
 * @param head是指向结构体数组头的指针,*new是指向要
 * 插入数据的指针,*debug_information是从main函数传
 * 的一个用来打印调试信息的一个指针,在被调函数中通
 * 过malloc动态分配空间,然后在主函数中将其释放防止
 * 造成内存泄漏
 * @retval struct Link *通过返回值来修改main函数中
 * 的指向,因为数据可能插入到了链表的头部
 * *************************************************/
struct Link * Insert_data_from_Forward(struct Link *head, struct Link *new, char **debug_information)
{
	int Find_data = 0;
	struct Link *psarr = head;
	
	*debug_information = (char *)malloc(sizeof(char) * 64);
	if(!(*debug_information))
	{
		printf("malloc error\n");
		exit(EXIT_FAILURE);
	}

	puts("please enter the data you want to find from the link and insert to the link");
	scanf("%d",&Find_data);

	if(psarr->idata == Find_data)
	{
		 new->next = psarr;
		 head = new;
		
		 if(head->idata == new->idata)
		 {
			 strcpy(*debug_information, "successfully insert the data to the link");
			 
			 return head;
		 }
	}
	while(psarr->next)
	{
		if(psarr->next->idata == Find_data)
		{
			new->next = psarr->next;
			psarr->next = new;
			 if(psarr->next->idata == new->idata)
			 {
				 strcpy(*debug_information, "successfully insert the data to the link");
				 
				 return head;
			 }
		}
		psarr = psarr->next;
	}

	strcpy(*debug_information, "failed to  insert the data to the link");

	return head;
}

链表从数据的后方插入

/****************************************************
 * @Function Description
 * @breif	通过遍历链表后来查找用户需要往那个位置的值,
 * 然后同样要获取用户的要插入的值,改变后判断插入的值和
 * 用户要插入的值是否相等,如果相等就说明插入成功,那么
 * 就将对应的调试信息返回并打印出来,同时也要注意释放动
 * 态开辟出来的空间
 * @param head是指向结构体数组的指针,*new是指向新元素
 * 的地址,debug_information是从main函数传的一个用来打
 * 印调试信息的一个指针,在被调函数中通过malloc动态分
 * 配空间,然后在主函数中将其释放防止造成内存泄漏
 * @retval char *debug_information	通过返回值来修改main
 * 函数中的指向,在使用完以后就释放掉
 * *************************************************/
char * Insert_data_from_behind(struct Link *head, struct Link *new, char *debug_information)
{
	int Find_data = 0;
	struct Link *psarr = head;

	debug_information = (char *)malloc(sizeof(char) * 64);
	if(!debug_information)
	{
		printf("malloc error\n");
		exit(EXIT_FAILURE);
	}

	puts("please enter the data you want to find and insert the data from the behind of link");
	scanf("%d",&Find_data);

	while(psarr)
	{
		if(psarr->idata == Find_data)
		{
			new->next = psarr->next;
			psarr->next = new;				//需要注意的是这两步顺序不能换,如果换了以后就再也找不到之前psarr的下一个数据了
			
			if(psarr->next->idata == new->idata)		//这里判断一下是否将数据插入到链表中,如果插入链表后就将调试信息拷贝给debug_information
			{
				strcpy(debug_information, "successfully insert data to th link");	
				
				return debug_information;
			}			
		}
		psarr = psarr->next;
	}

	strcpy(debug_information,"failed to find this data");

	return debug_information;
}

链表的删除

/****************************************************
 * @Function Description
 * @breif	通过遍历链表后来查找用户需要删除的值,删除
 * 后判断它的值和现在里边存放的值是否相等,如果不相等
 * 就说明删除成功,那么就通过指向调试信息的指针修改里
 * 边的内容并打印出来。这里删除数据共有两种情况,一种直
 * 接删除头节点另外一种就是删除中间节点,那么就涉及到可
 * 能需要修改链表的头,所以这里的返回值是一个
 * 指向结构体的指针,而调试信息使用二级指针的方法去访
 * 问。同时也要注意释放动态开辟出来的空间
 * 这里需要注意的问题是链表的最后一个节点它的下个节点是
 * NULL,如果和上边使用同样的判断条件就会造成段错误,所
 * 以在判断tmp和此时psarr->next->idata是否相等时要加一
 * 层判断,判断它是否到了链表的最后一个节点,如果到了链
 * 表的最后一个节点,那么就判断的是它的tmp和psarr->idata
 * 里边的值
 * @param head是指向结构体数组头的指针,*debug_information
 * 是从main函数传的一个用来打印调试信息的一个指针,在被调
 * 函数中通过malloc动态分配空间,然后在主函数中将其释放防止
 * 造成内存泄漏
 * @retval struct Link *通过返回值来修改main函数中
 * 的指向,因为数据可能删除了链表的头部
 * *************************************************/
struct Link * Delete_the_node_lists_of_Link(struct Link *head, char **debug_information)
{
	int Find_data = 0;
	int tmp;
	struct Link *psarr = head;

	puts("please enter which data do you want to find and delete?");
	scanf("%d",&Find_data);
	
	*debug_information = (char *)malloc(sizeof(char) * 64);

	if(psarr->idata == Find_data)
	{
		tmp = psarr->idata;
		psarr = psarr->next;
		head = psarr;
		
		if(tmp != psarr->idata)
		{
			strcpy(*debug_information,"successfully find the data and delete the data");

			return head;
		}
	}

	while(psarr->next)
	{
		if(psarr->next->idata == Find_data)
		{
			tmp = psarr->next->idata;
			psarr->next = psarr->next->next;
		
			if(psarr->next != NULL)
			{
				if(tmp != psarr->next->idata)
				{
					strcpy(*debug_information, "successfully find the data and delete the data");

					return head;
				}
			}
			else
			{
					if(tmp != psarr->idata)
					{
						strcpy(*debug_information, "successfully find the data and delete the data");

						return head;
					}
			}
		}
		psarr = psarr->next;
	}

	strcpy(*debug_information,"failed to find the data and delete the data");

	return head;
}
静态创建链表的代码
#include "link.h"

/****************************************************
 * @Function Description
 * @breif 判断外部传入参数的个数,如果小于;两个就直接提示
 * 用户需要输入更多的参数,并且退出程序防止代码造成
 * 段错误
 * @param 外部传入参数的个数
 * @retval NONE
 * *************************************************/
void Judgement_the_number_of_parameters(int parameters)
{
	if(parameters < 2)
	{
		printf("parameters is not enough,please enter more parameters,such as argv[1]\n");
		exit(EXIT_FAILURE);
	}
}

/****************************************************
 * @Function Description
 * @breif 使用rand函数来自动的生成随机数来填充结构体数
 * 组里边的idata,并且将结构体里边的成员变量next指向它
 * 的下一个
 * @param psarr是指向结构体数组的指针,size是结构体数组
 * 的大小,通过这两个参数就能修改里边的元素内容
 * @retval NONE
 * *************************************************/
void Init_struct_array_With_Random_Function(struct Link *psarr, int size)
{
	int i;

	memset(psarr,0,size);				//将psarr野指针清空
	srand((unsigned int )time(NULL));	//设置随机数种子
	for(i=0;i<size;i++)
	{
		psarr[i].idata = rand() % 100;
		
		if(i+1 < size)
		{
			(psarr+i)->next = (psarr+i+1);		//将当前成员除最后一个成员外将里边的next都指向它的下一个
		}
		else
		{
			(psarr+i)->next = NULL;
		}
	}
}

/****************************************************
 * @Function Description
 * @breif 由于上边使用Init函数对结构体数组进行了初始化
 * 所以这里能够利用链表最后一个节点为NULL的特性来进行
 * 遍历数组
 * @param psarr是指向结构体数组的指针
 * @retval NONE
 * *************************************************/
void Print_Link_Function(struct Link *psarr)
{
	while(psarr)
	{
		printf("%d ",psarr->idata);
		psarr = psarr->next;		//遍历链表就是根据这句话来进行的,所以这句话不能丢
	}
	puts("");
}

/****************************************************
 * @Function Description
 * @breif 使用遍历数组的功能能同时来统计链表的节点个
 * 数,利用的原理还是链表节点最后一个元素的next为NULL
 * @param psarr是指向结构体数组的指针,*cnt是指向main
 * 函数中的cnt的指针,通过指针来修改main函数中的值
 * @retval NONE
 * *************************************************/
void Counting_the_number_of_Link_Function(struct Link *psarr, int *cnt)
{
	while(psarr)
	{
		(*cnt)++;
		psarr = psarr->next;
	}
}

/****************************************************
 * @Function Description
 * @breif 链表的查找就是从main函数里传一个数过来然后
 * 遍历链表判断链表里边的每一个值是否和要找的值相同,
 * 如果找到这个数那么就将调试信息返回在main函数中打印,
 * 并且将这个值在链表中的位置打印出来,如果没有这个值
 * 就将position置为-1表示没有这个值
 * @param psarr是指向结构体数组的指针,debug_information
 * 是从main函数传的一个用来打印调试信息的一个指针,在
 * 被调函数中通过malloc动态分配空间,然后在主函数中将
 * 其释放防止造成内存泄漏
 * @retval char *debug_information	通过返回值来修改main
 * 函数中的指向,在使用完以后就释放掉
 * *************************************************/
char * Find_the_data_of_Link_Function(struct Link *psarr, char *debug_information, int *position)
{
	int Find_data;
	
	debug_information = (char *)malloc(sizeof(char) * 64);
	if(!debug_information)
	{
		printf("malloc error\n");
		exit(EXIT_FAILURE);
	}

	puts("please enter which data do you want to find?");
	scanf("%d",&Find_data);

	while(psarr)
	{
		if(psarr->idata == Find_data)
		{
			strcpy(debug_information,"successfully find this data");
	
			return debug_information;
		}
		psarr = psarr->next;
		(*position)++;
	}
	strcpy(debug_information,"failed to find this data");
	*position = -1;

	return debug_information;
}

/****************************************************
 * @Function Description
 * @breif	通过遍历链表后来查找用户需要修改的值,然后
 * 同样要获取用户的要改变后的值,改变后判断和要改的值
 * 是否相等,如果相等就说明修改成功,那么就将对应的
 * 调试信息返回并打印出来,同时也要注意释放动态开辟出
 * 来的空间
 * @param psarr是指向结构体数组的指针,debug_information
 * 是从main函数传的一个用来打印调试信息的一个指针,在
 * 被调函数中通过malloc动态分配空间,然后在主函数中将
 * 其释放防止造成内存泄漏
 * @retval char *debug_information	通过返回值来修改main
 * 函数中的指向,在使用完以后就释放掉
 * *************************************************/
char* Change_data_of_Link_Function(struct Link *head, char *debug_information)
{
	int Find_data = 0;
	int Change_data = 0;
	struct Link *psarr = head;

	debug_information = (char *)malloc(sizeof(char) * 64);
	if(debug_information == NULL)
	{
		printf("malloc error\n");
		exit(EXIT_FAILURE);
	}

	puts("please enter the data you want to find and change");
	scanf("%d%d",&Find_data,&Change_data);

	while(psarr)
	{
		if(psarr->idata == Find_data)
		{
			psarr->idata = Change_data;
			if(psarr->idata == Change_data)
			{
				strcpy(debug_information,"successfully change the data of link");

				return debug_information;
			}
		}
		psarr = psarr->next;
	}

	strcpy(debug_information,"failed to change the data");

	return debug_information;
}

/****************************************************
 * @Function Description
 * @breif	通过遍历链表后来查找用户需要在哪里插入这
 * 个值,然后同样要获取用户的要插入的值,插入后判断插
 * 入的值和用户需要插入的值是否相等,如果相等就说明插
 * 入成功,那么就通过指向调试信息的指针修改里边的内容
 * 并打印出来。这里从前方插入数据共有两种情况,一种直
 * 接在头部插入另外一种直接在其他数据前方插入,那么就
 * 涉及到可能需要修改链表的头,所以这里的返回值是一个
 * 指向结构体的指针,而调试信息使用二级指针的方法去访
 * 问。同时也要注意释放动态开辟出来的空间
 * @param head是指向结构体数组头的指针,*new是指向要
 * 插入数据的指针,*debug_information是从main函数传
 * 的一个用来打印调试信息的一个指针,在被调函数中通
 * 过malloc动态分配空间,然后在主函数中将其释放防止
 * 造成内存泄漏
 * @retval struct Link *通过返回值来修改main函数中
 * 的指向,因为数据可能插入到了链表的头部
 * *************************************************/
struct Link * Insert_data_from_Forward(struct Link *head, struct Link *new, char **debug_information)
{
	int Find_data = 0;
	struct Link *psarr = head;
	
	*debug_information = (char *)malloc(sizeof(char) * 64);
	if(!(*debug_information))
	{
		printf("malloc error\n");
		exit(EXIT_FAILURE);
	}

	puts("please enter the data you want to find from the link and insert to the link");
	scanf("%d",&Find_data);

	if(psarr->idata == Find_data)
	{
		 new->next = psarr;
		 head = new;
		
		 if(head->idata == new->idata)
		 {
			 strcpy(*debug_information, "successfully insert the data to the link");
			 
			 return head;
		 }
	}
	while(psarr->next)
	{
		if(psarr->next->idata == Find_data)
		{
			new->next = psarr->next;
			psarr->next = new;
			 if(psarr->next->idata == new->idata)
			 {
				 strcpy(*debug_information, "successfully insert the data to the link");
				 
				 return head;
			 }
		}
		psarr = psarr->next;
	}

	strcpy(*debug_information, "failed to  insert the data to the link");

	return head;
}

/****************************************************
 * @Function Description
 * @breif	通过遍历链表后来查找用户需要往那个位置的值,
 * 然后同样要获取用户的要插入的值,改变后判断插入的值和
 * 用户要插入的值是否相等,如果相等就说明插入成功,那么
 * 就将对应的调试信息返回并打印出来,同时也要注意释放动
 * 态开辟出来的空间
 * @param head是指向结构体数组的指针,*new是指向新元素
 * 的地址,debug_information是从main函数传的一个用来打
 * 印调试信息的一个指针,在被调函数中通过malloc动态分
 * 配空间,然后在主函数中将其释放防止造成内存泄漏
 * @retval char *debug_information	通过返回值来修改main
 * 函数中的指向,在使用完以后就释放掉
 * *************************************************/
char * Insert_data_from_behind(struct Link *head, struct Link *new, char *debug_information)
{
	int Find_data = 0;
	struct Link *psarr = head;

	debug_information = (char *)malloc(sizeof(char) * 64);
	if(!debug_information)
	{
		printf("malloc error\n");
		exit(EXIT_FAILURE);
	}

	puts("please enter the data you want to find and insert the data from the behind of link");
	scanf("%d",&Find_data);

	while(psarr)
	{
		if(psarr->idata == Find_data)
		{
			new->next = psarr->next;
			psarr->next = new;				//需要注意的是这两步顺序不能换,如果换了以后就再也找不到之前psarr的下一个数据了
			
			if(psarr->next->idata == new->idata)		//这里判断一下是否将数据插入到链表中,如果插入链表后就将调试信息拷贝给debug_information
			{
				strcpy(debug_information, "successfully insert data to th link");	
				
				return debug_information;
			}			
		}
		psarr = psarr->next;
	}

	strcpy(debug_information,"failed to find this data");

	return debug_information;
}

/****************************************************
 * @Function Description
 * @breif	通过遍历链表后来查找用户需要删除的值,删除
 * 后判断它的值和现在里边存放的值是否相等,如果不相等
 * 就说明删除成功,那么就通过指向调试信息的指针修改里
 * 边的内容并打印出来。这里删除数据共有两种情况,一种直
 * 接删除头节点另外一种就是删除中间节点,那么就涉及到可
 * 能需要修改链表的头,所以这里的返回值是一个
 * 指向结构体的指针,而调试信息使用二级指针的方法去访
 * 问。同时也要注意释放动态开辟出来的空间
 * 这里需要注意的问题是链表的最后一个节点它的下个节点是
 * NULL,如果和上边使用同样的判断条件就会造成段错误,所
 * 以在判断tmp和此时psarr->next->idata是否相等时要加一
 * 层判断,判断它是否到了链表的最后一个节点,如果到了链
 * 表的最后一个节点,那么就判断的是它的tmp和psarr->idata
 * 里边的值
 * @param head是指向结构体数组头的指针,*debug_information
 * 是从main函数传的一个用来打印调试信息的一个指针,在被调
 * 函数中通过malloc动态分配空间,然后在主函数中将其释放防止
 * 造成内存泄漏
 * @retval struct Link *通过返回值来修改main函数中
 * 的指向,因为数据可能删除了链表的头部
 * *************************************************/
struct Link * Delete_the_node_lists_of_Link(struct Link *head, char **debug_information)
{
	int Find_data = 0;
	int tmp;
	struct Link *psarr = head;

	puts("please enter which data do you want to find and delete?");
	scanf("%d",&Find_data);
	
	*debug_information = (char *)malloc(sizeof(char) * 64);

	if(psarr->idata == Find_data)
	{
		tmp = psarr->idata;
		psarr = psarr->next;
		head = psarr;
		
		if(tmp != psarr->idata)
		{
			strcpy(*debug_information,"successfully find the data and delete the data");

			return head;
		}
	}

	while(psarr->next)
	{
		if(psarr->next->idata == Find_data)
		{
			tmp = psarr->next->idata;
			psarr->next = psarr->next->next;
		
			if(psarr->next != NULL)
			{
				if(tmp != psarr->next->idata)
				{
					strcpy(*debug_information, "successfully find the data and delete the data");

					return head;
				}
			}
			else
			{
					if(tmp != psarr->idata)
					{
						strcpy(*debug_information, "successfully find the data and delete the data");

						return head;
					}
			}
		}
		psarr = psarr->next;
	}

	strcpy(*debug_information,"failed to find the data and delete the data");

	return head;
}

链表的动态创建

那么已经有了静态链表为什么还需要动态链表呢?那是因为静态链表它是在栈上开辟空间,所以它的大小在编译的时候就已经确定了,而动态链表可以通过malloc函数来增加节点,因此相比较静态链表而言,动态链表更加的灵活。

从头部插入

/**********************************************
 * @Function Description
 * @breif	动态头插法创建链表就是将下一个链表
 * 的节点变成头,最后插入的节点就是链表的头,也
 * 是是链表的头一直在变,所以打印链表的时候它和
 * 输入的顺序刚好是相反的
 * param struct Link *head是一个指向链表的指针,
 * 通过在被调函数中动态的给它开辟空间,然后就可
 * 以保证链表是在堆空间上进行存放,而之前的静态
 * 链表都是在栈空间上进行存放,函数调用结束后它
 * 就被释放掉了,而堆空间上的链表需要通过free函
 * 数来确保它能正常的释放,不会造成内存泄漏等问题
 * @retval	struct Link *通过返回指向链表的指针来
 * 改变它的指向
 * ********************************************/
struct Link *Create_link(struct Link *head)
{
	struct Link *new = NULL;
	
	while(1)
	{
		new = (struct Link *)malloc(sizeof(struct Link));
		if(!new)
		{
			printf("malloc error\n");
			exit(EXIT_FAILURE);
		}

		srand((unsigned int)time(NULL));
		puts("please enter the new node's idata");
		scanf("%d",&(new->idata));
		new->next = NULL;
		
		if(new->idata == 0)
		{
			free(new);
			new = NULL;
			puts("quit");
			return head;
		}

		if(!head)
		{
			head = new;
		}
		else
		{
			new->next = head;
			head = new;

		}
	}
}

从尾部插入

/**********************************************
 * @Function Description
 * @breif	动态尾插法创建链表就是将下一个链表
 * 的节点插入到当前节点的下一个,最后插入的节点
 * 就是链表的尾,也是是链表的尾一直在变,所以打
 * 印链表的时候它的输入的顺序是一致的
 * param struct Link *head是一个指向链表的指针,
 * 通过在被调函数中动态的给它开辟空间,然后就可
 * 以保证链表是在堆空间上进行存放,而之前的静态
 * 链表都是在栈空间上进行存放,函数调用结束后它
 * 就被释放掉了,而堆空间上的链表需要通过free函
 * 数来确保它能正常的释放,不会造成内存泄漏等问题
 * @retval	struct Link *通过返回指向链表的指针来
 * 改变它的指向
 * ********************************************/
struct Link * Create_Link_Tail(struct Link *head)
{
	struct Link *new = NULL;
	struct Link *point = NULL;

	while(1)
	{
		new = (struct Link *)malloc(sizeof(struct Link));
		if(new == NULL)
		{
			printf("malloc error");
			exit(EXIT_FAILURE);
		}
		puts("please enter the node that you want to create");
		scanf("%d",&(new->idata));
		new->next = NULL;

		if(new->idata == 0)
		{
			free(new);
			new = NULL;
			puts("quit");
			return head;
		}

		if(!head)
		{
			head = new;
		}
		else
		{
			point = head;
			while(point->next != NULL)
			{
				point = point->next;
			}
			point->next = new;
		}
	}
}
动态创建链表代码
#include "link.h"

/**********************************************
 * @Function Description
 * @breif	动态头插法创建链表就是将下一个链表
 * 的节点变成头,最后插入的节点就是链表的头,也
 * 是是链表的头一直在变,所以打印链表的时候它和
 * 输入的顺序刚好是相反的
 * param struct Link *head是一个指向链表的指针,
 * 通过在被调函数中动态的给它开辟空间,然后就可
 * 以保证链表是在堆空间上进行存放,而之前的静态
 * 链表都是在栈空间上进行存放,函数调用结束后它
 * 就被释放掉了,而堆空间上的链表需要通过free函
 * 数来确保它能正常的释放,不会造成内存泄漏等问题
 * @retval	struct Link *通过返回指向链表的指针来
 * 改变它的指向
 * ********************************************/
struct Link *Create_link(struct Link *head)
{
	struct Link *new = NULL;
	
	while(1)
	{
		new = (struct Link *)malloc(sizeof(struct Link));
		if(!new)
		{
			printf("malloc error\n");
			exit(EXIT_FAILURE);
		}

		srand((unsigned int)time(NULL));
		puts("please enter the new node's idata");
		scanf("%d",&(new->idata));
		new->next = NULL;
		
		if(new->idata == 0)
		{
			free(new);
			new = NULL;
			puts("quit");
			return head;
		}

		if(!head)
		{
			head = new;
		}
		else
		{
			new->next = head;
			head = new;

		}
	}
}

void Finsh_Link(struct Link *head)
{
	struct Link *point = NULL;

	while(head)
	{
		point = head;
		head = head->next;
		free(point);
		point = NULL;
	}
}

/**********************************************
 * @Function Description
 * @breif	动态尾插法创建链表就是将下一个链表
 * 的节点插入到当前节点的下一个,最后插入的节点
 * 就是链表的尾,也是是链表的尾一直在变,所以打
 * 印链表的时候它的输入的顺序是一致的
 * param struct Link *head是一个指向链表的指针,
 * 通过在被调函数中动态的给它开辟空间,然后就可
 * 以保证链表是在堆空间上进行存放,而之前的静态
 * 链表都是在栈空间上进行存放,函数调用结束后它
 * 就被释放掉了,而堆空间上的链表需要通过free函
 * 数来确保它能正常的释放,不会造成内存泄漏等问题
 * @retval	struct Link *通过返回指向链表的指针来
 * 改变它的指向
 * ********************************************/
struct Link * Create_Link_Tail(struct Link *head)
{
	struct Link *new = NULL;
	struct Link *point = NULL;

	while(1)
	{
		new = (struct Link *)malloc(sizeof(struct Link));
		if(new == NULL)
		{
			printf("malloc error");
			exit(EXIT_FAILURE);
		}
		puts("please enter the node that you want to create");
		scanf("%d",&(new->idata));
		new->next = NULL;

		if(new->idata == 0)
		{
			free(new);
			new = NULL;
			puts("quit");
			return head;
		}

		if(!head)
		{
			head = new;
		}
		else
		{
			point = head;
			while(point->next != NULL)
			{
				point = point->next;
			}
			point->next = new;
		}
	}
}

int main()
{
	struct Link *head = NULL;
	
//	head = Create_link(head);
	head = Create_Link_Tail(head);
	Print_Link_Function(head);
	Finsh_Link(head);

	return 0;
}
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日落星野

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值