【自学】数据结构-链表的操作(详细注释)

本文介绍了链表的概念,包括其作为线性表的链式存储结构,以及链表的基本操作如创建、插入、删除、遍历、查找和修改等,并提供了C语言实现的示例。

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

一、什么是链表?

指线性表的链式存储结构,数据存储的时候可以存储当前数据记录,同时存储前驱结点或者后继结点数据记录的存储空间的地址,在访问的时候,可以通过地址访问到下一条数据记录。

链表结构

二、链表的操作

 链表的基本操作类型和顺序表差不多,直接看代码:

common.h

头文件主要是定义数据类型和链表结构,以及链表操作函数的声明:

#ifndef _COMM0N_H_
#define _COMMON_H_
//定义结点数据域的数据类型
typedef int data_t;
//定义整个链表结点数据类型
typedef struct node{
	data_t data;      /* 结点数据域:存储当前结点的数据 */
	struct node *next;/* 结点指针域:存储下一个结点存储空间地址 */
}node_t;
//函数声明
node_t *CreateLinkList();//创建
int InsertLinkList(node_t *head,int local,data_t mydata);//插入
void DisplayLinkList(node_t *head);//遍历

int DeleteLinkList(node_t *head,int local);//删除
int SearchLinkList(node_t *head,int local,data_t *mydata);//查找
int ChangeLinkList(node_t *head,int local,data_t mydata);//修改

void ClearLinkList(node_t *head);//清空链表的数据结点,头结点保留
void DestoryLinkList(node_t **head);//销毁整个链表
int isFullLinkList(node_t *head);//判断链表是否为满表
int isEmptyLinkList(node_t *head);//判断链表是否为空
int LenthLinkList(node_t *head);//计算链表中有效数据结点个数,链表长度


#endif

linklist.c

链表操作的具体实现:注意添加头文件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"

//定义链表创建函数:实质是创建头结点
node_t *CreateLinkList()
{
	node_t *head;
	//动态开辟头结点空间的地址
	head = malloc(sizeof(node_t));
	if(head == NULL)//头结点空间开辟失败
		return NULL;
	memset(head,0,sizeof(node_t));
	head->next = NULL;//指针域没有指向,设置为空

	return head;
}
//链表插入数据元素
int InsertLinkList(node_t *head,int local,data_t mydata)
{
	int i;
	node_t *p=head;
	node_t *q;
	//判度表是否存在
	if(head == NULL)
		return;
	//插入的位置是否满足要求
	if(local<0)
		return -1;
	//遍历链表,找到需要插入结点的前一个结点P
	for(i=0;i<local;i++)
	{
		p = p->next;
		if(p == NULL)
			return -1;//插入结点的前一个结点没有
	}
	//创建需要插入的结点q并初始化
	q = malloc(sizeof(node_t));
	if(q == NULL)
		return -1;
	q->data=mydata;
	//插入结点
	q->next = p->next;
	p->next = q;

	return 0;
}
//遍历链表
void DisplayLinkList(node_t *head)
{
	node_t *p;
	//判断表是否存在
	if(head == NULL)
		return;
	//遍历输出
	p = head->next;
	while(p!=NULL)
	{
		printf("%d",p->data);
		p = p->next;	
	}
	printf("\n");
}
//删除链表的数据元素
int DeleteLinkList(node_t *head,int local)
{
	int i;
	node_t *p;
	node_t *q;
	//判断表是否存在
	if(head == NULL)
		return;
	//判断表是否为空表
	if(head->next == NULL)
		return -1;
	//删除的位置不满足
	if(local<0)
		return -1;
	//找到要删除的结点的上一个结点
	p = head;
	for(i=0;i<local;i++)
	{
		p = p->next;
		if(p == NULL)
			return -1;
	}
	//删除结点q
	q = p->next;
	p->next = q->next;
	free(q);

	return 0;
	
}
//查询链表中的某个元素
int SearchLinkList(node_t *head,int local,data_t *mydata)
{
	int i;
	node_t *p;
	//判断表是否存在
	if(head ==NULL)
		return;
	//表是否为空
	if(head->next == NULL)
		return -1;
	//查找的位置是否满足条件
	if(local<0)
		return -1;
	//得到起始有效结点位置
	p = head->next;
	for(i=0;i<local;i++)
	{
		p = p->next;
		if(p==NULL)
			return -1;
	}
	//得到数据
	*mydata = p->data;

	return 0;
}
//修改链表中的某个元素
int ChangeLinkList(node_t *head,int local,data_t mydata)
{
	int i;
	node_t *p;
	//判断表是否有
	if(head == NULL)
		return;
	//表是否为空
	if(head->next == NULL)
		return -1;
	//修改的位置是否满足
	if(local<0)
		return -1;
	//找到要修改的元素
	p=head->next;
	for(i=0;i<local;i++)
	{
		p = p->next;
		if(p == NULL)
			return -1;
	}
	//修改
	p->data = mydata;
}

    /*------ 其他操作------ */
//判断链表是否为空
int isEmptyLinkList(node_t *head)
{
	return (head->next == NULL);
}
//判断链表是否为满表
int isFullLinkList(node_t *head)
{
	return 0;
}
//计算链表中有效数据结点的个数,链表的长度
int LenthLinkList(node_t *head)
{
	int len = 0;
	node_t *p;
	if(head == NULL)
		return 0;
	p = head->next;
	while(p!=NULL)
	{
		len++;
		p=p->next;
	}
	return len;
}
//清空链表的数据结点,头结点保留
void ClearLinkList(node_t *head)
{
	node_t *p;
	//判断表是否存在
	if(head == NULL)
		return;
	//清空
	while(head->next != NULL)
	{
		p = head->next;
		head->next = p->next;
		free(p);
	}
}
//销毁整个链表
void DistroyLinkList(node_t **head)
{
	node_t *p;
	//表不存在
	if(*head == NULL)
		return;
	while((*head)->next != NULL)
	{
		p = (*head)->next;
		(*head)->next = p->next;
		free(p);
	}
	free(*head);
	*head = NULL;
}

app.c

#include <stdio.h>
#include "common.h"
int main()
{
	int i;
	node_t *head;
	data_t mydata;
	head = CreateLinkList();
	if(head == NULL)
		return -1;
	i=10;
	while(i)
	{
		printf("insert %d-%d\n",i,InsertLinkList(head,0,i));
		i--;
	}
	DisplayLinkList(head);
    
    //其他函数操作可以自行调用
    ......

	return 0;
}

 三、运行

在Linux系统中:

编译:gcc app.c linklist.c

                ./a.out

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值