单向链表

本文详细介绍了单向链表的基本概念,包括其存储结构和关键操作,如初始化、插入、删除、查找、打印和释放。通过具体代码示例,展示了如何在C语言中实现这些功能,并提供了一个完整的示例程序,演示了链表的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单向链表

  • 单向链表存储空间是不连续的,它有数据和指向下一个节点的首地址组成

在这里插入图片描述


代码示例

list.h

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

//节点结构体
typedef struct listnode
{
		void* data;
		struct listnode* next;
		
}ListNode;


//链表信息结构体
typedef struct listinfo
{
		ListNode* head;
		int size;	
}ListInfo;


typedef void(*PRINTLISTNODE)(void*);//定义函数指针,给Print函数用,参数void*,返回值void
typedef int (*JUDGEEQUAL)(void*,void*);//Find_list的形参,判断相等函数指针,用户提供实现,传入



ListInfo* Init_List(); 

void Insert_list(ListInfo* list,int pos,void* data);

void RemoveByPos_list(ListInfo* list,int pos);

int Size_list(ListInfo* list);

int Find_list(ListInfo* list,void* data,JUDGEEQUAL judge_equal);

void* Front_list(ListInfo* list);

void Free_list(ListInfo* list);

void Print(ListInfo* list,PRINTLISTNODE print);


list.c
#include "../include/list.h"



//初始化链表
ListInfo* Init_List()
{
	ListInfo* list = (ListInfo*)malloc(sizeof(ListInfo));
    list->size=0;
	
	list->head=(ListNode*)malloc(sizeof(ListNode));
	list->head->data=NULL;
	list->head->next=NULL;

	return list;
}



//插入节点
void Insert_list(ListInfo* list,int pos,void* data)
{

	if(list == NULL) return;	

	if(pos<0 || pos>list->size)return;

	ListNode* NewNode=(ListNode*) malloc(sizeof(ListNode));
 	NewNode->data=data;
	NewNode->next=NULL;

	ListNode* pCurrent = list->head;
	for(int i=0; i<pos; i++)
	{
			pCurrent = pCurrent->next;
	}

	
	NewNode->next = pCurrent->next;
	pCurrent->next = NewNode;

	list->size++;
}


//删除节点
void RemoveByPos_list(ListInfo* list,int pos)
{

	if(pos<0 || pos>list->size)
			return ;

	ListNode* pCurrent = list->head;
	for(int i=0; i<pos; i++)
	{
			pCurrent=pCurrent->next;
	}

	ListNode* tmp=pCurrent->next;
	pCurrent->next=pCurrent->next->next;
	free(tmp);
		
	list->size--;
}



//返回链表长度
int Size_list(ListInfo* list)
{
	return list->size;

}


//查找节点,找到返回位置
int Find_list(ListInfo* list,void* data)
{

	if(list==NULL)return -1;

	ListNode* pCurrent=list->head;
	for(int i=0; i<list->size; i++)
	{
		pCurrent=pCurrent->next;
		if(judge_equal(pCurrent->data,data1))
		{	
			return i;
		}
	}
	return -1;
}


//返回第一个节点
void* Front_list(ListInfo* list)
{
		return list->head->next->data;
}


//释放链表
void  Free_list(ListInfo* list) 
{

	if(list == NULL)exit(1);
	
	ListNode* pCurrent = list->head;
	while(pCurrent != NULL)
	{
		ListNode* tmp =pCurrent->next;
		free(pCurrent);
		pCurrent = tmp;
	}

	free(list);	
}


//打印链表
void Print(ListInfo* list,PRINTLISTNODE print)
{

	if(list == NULL)return;

	ListNode* pCurrent = list->head->next;
	while(pCurrent != NULL)
	{
		print(pCurrent->data);
		pCurrent = pCurrent->next;
	}


}


#include <stdio.h>
#include <stdlib.h>
#include "../include/list.h"

typedef struct PERSON
{
	char name[64];
	int age;
	int score;
	
}Person;

void MyPrint(void* data)
{
	Person* p =(Person*)data;
	printf("name: %s,age: %d,score: %d\n",p->name,p->age,p->score);
}


int Judge_Equal(void* data1,void* data2)
{
	Person* p1 =(Person*)data1;
	Person* p2 =(Person*)data2;

	if(p1->age==p2->age && p1->name==p2->name && p1->score == p2->score)
	{
		return 1;
	}

	else
	{
		return  0;
	}

}

int main(void)
{
	ListInfo* list = Init_List();

	Person p1 ={"julian",18,90};
	Person p6 ={"kerr",12,60};
	Person p5 ={"mike",13,30};
	Person p4 ={"john",13,93};
	Person p3 ={"lucy",17,90};
	Person p2 ={"candy",17,80};
	
	Insert_list(list,0,&p1);		
	Insert_list(list,1,&p6);		
	Insert_list(list,2,&p2);		
	Insert_list(list,3,&p3);		
	Insert_list(list,4,&p4);		
	Insert_list(list,5,&p5);		

	Print(list,MyPrint);
	printf("----------删除第2个元素---------\n");


	RemoveByPos_list(list,2);
	Print(list,MyPrint);
	printf("--------返回第一个元素-----------\n");

   Person* tmp=	(Person*)Front_list(list);
   printf("name: %s,age: %d,score: %d\n",tmp->name,tmp->age,tmp->score);

   printf("---------查找p3,lucy------------------");
   int position = Find_list(list,&p3,Judge_Equal);                                                                 
   printf("pos: %d\n",position); 
   
	Free_list(list);

        
	return 0;
}


结果

name: julian,age: 18,score: 90
name: kerr,age: 12,score: 60
name: candy,age: 17,score: 80
name: lucy,age: 17,score: 90
name: john,age: 13,score: 93
name: mike,age: 13,score: 30
--------删除第二个元素-----------
name: julian,age: 18,score: 90
name: kerr,age: 12,score: 60
name: lucy,age: 17,score: 90
name: john,age: 13,score: 93
name: mike,age: 13,score: 30
---------返回第一个元素---------
name: julian,age: 18,score: 90
---------查找p3,lucy------------------
pos: 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值