数据结构和算法系列 - 单链表

单链表定义

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。

单向链表的每一个元素Item中,有一个元素会存放下一个Item的内存地址。当找到第一个元素,就能通过这个元素中存放的第二个元素的地址值找到第三个元素,继而找到第四个,第五个,第六个元素。

图1,单向链表的结构图。图中Item1元素中存放着Item2的元素地址值,Item2中存放着Item3的元素内存地址值。所以整个单链表看起来像一个链条一样串联起来。


单链表的基本操作

如何来操作单链表?我们这边只进行比较基本的操作。

1. 添加元素。添加元素有两种方法:头插法和尾插法。

头插法:将链表的左边称为链表头部,右边称为链表尾部。头插法将右边固定,每次新增的元素都在左边头部增加。


尾插法:将链表的左边称为链表头部,右边称为链表尾部。尾插法是将左边固定,每次新增都在链表的右边最尾部。



2. 查询元素。通过元素信息来查询相关的ITEM,需要从头部遍历到尾部。


3. 删除元素。删除一个元素后,需要将元素左边Item存储的next元素的值指向到该删除元素右边的值。


单链表结构体定义

我们定义了一个结构体,结构体中username和age分别用来存储用户名和年龄,而next则用来存储下一个Item元素的内存地址。

/**
 * 定义一个单链表的数据结构
 */
typedef struct single_linked_list_s {
	char * username;
	unsigned int age;
	struct single_linked_list_s * next;
} single_linked_list;

并且定义一个list指针用来存放单链表。

/**
 * 一定一个指针,存放单链表
 */
single_linked_list *list;

然后定义三个方法:

/**
 * 插入一条记录
 */
void insert(char *username, unsigned int age);

/**
 * 获取一条记录
 */
single_linked_list *get(char *username);

/**
 * 通过username去删除一个元素
 */
void delete(char *username);

具体实现

1. 添加操作,我们使用了尾插入法。

void insert(char *username, unsigned int age) {
	single_linked_list *temp = (single_linked_list *) malloc(
			sizeof(single_linked_list));
	if (temp != NULL ) {
		temp->username = username;
		temp->age = age;
		temp->next = NULL;
		if (list == NULL ) {
			list = temp;
		} else {
			single_linked_list *temp_list = list;
			while (temp_list->next != NULL ) {
				temp_list = temp_list->next;
			}
			temp_list->next = temp;
		}
	}
}

2. 查询操作。

single_linked_list *get(char *username) {
	if (list == NULL ) {
		return NULL ;
	}
	single_linked_list *temp = NULL;
	single_linked_list *temp_list = list;
	while (temp_list->next != NULL ) {
		if (strcmp(temp_list->username, username) == 0) {
			temp = temp_list;
			break;
		} else {
			temp_list = temp_list->next;
		}
	}
    if (temp_list->next == NULL ) {
        if (strcmp(temp_list->username, username) == 0) {
            temp = temp_list;
        }
    }
        return temp;
}

3. 删除操作。

void delete(char *username) {
	if (list == NULL ) {
		return;
	}
	single_linked_list *temp_pre = NULL; //前一个元素
	single_linked_list *temp_next = NULL; //下一个元素
	single_linked_list *temp_current = list; //当前元素
	while (temp_current->next != NULL ) {
		if (strcmp(temp_current->username, username) == 0) {
			if (temp_pre != NULL ) {
				temp_pre->next = temp_next;
			}
			break;
		}
		temp_pre = temp_current;
		temp_current = temp_current->next;
		if (temp_current->next != NULL ) {
			temp_next = temp_current->next;
		}
	}
	if (list == temp_current) {
		if (list->next != NULL ) {
			list = list->next;
		} else {
			list = NULL;
		}
	}
	free(temp_current);
}

完整例子

single_linked_list.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>

/**
 * 定义一个单链表的数据结构
 */
typedef struct single_linked_list_s {
	char * username;
	unsigned int age;
	struct single_linked_list_s * next;
} single_linked_list;

/**
 * 一定一个指针,存放单链表
 */
single_linked_list *list;

/**
 * 插入一条记录
 */
void insert(char *username, unsigned int age);

/**
 * 获取一条记录
 */
single_linked_list *get(char *username);

/**
 * 通过username去删除一个元素
 */
void delete(char *username);


single_linked_list.c

#include "single_linked_list.h"

void insert(char *username, unsigned int age) {
	single_linked_list *temp = (single_linked_list *) malloc(
			sizeof(single_linked_list));
	if (temp != NULL ) {
		temp->username = username;
		temp->age = age;
		temp->next = NULL;
		if (list == NULL ) {
			list = temp;
		} else {
			single_linked_list *temp_list = list;
			while (temp_list->next != NULL ) {
				temp_list = temp_list->next;
			}
			temp_list->next = temp;
		}
	}
}

single_linked_list *get(char *username) {
	if (list == NULL ) {
		return NULL ;
	}
	single_linked_list *temp = NULL;
	single_linked_list *temp_list = list;
	while (temp_list->next != NULL ) {
		if (strcmp(temp_list->username, username) == 0) {
			temp = temp_list;
			break;
		} else {
			temp_list = temp_list->next;
		}
	}
	if (temp_list->next == NULL ) {
		if (strcmp(temp_list->username, username) == 0) {
			temp = temp_list;
		}
	}
	return temp;
}

void delete(char *username) {
	if (list == NULL ) {
		return;
	}
	single_linked_list *temp_pre = NULL; //前一个元素
	single_linked_list *temp_next = NULL; //下一个元素
	single_linked_list *temp_current = list; //当前元素
	while (temp_current->next != NULL ) {
		if (strcmp(temp_current->username, username) == 0) {
			if (temp_pre != NULL ) {
				temp_pre->next = temp_next;
			}
			break;
		}
		temp_pre = temp_current;
		temp_current = temp_current->next;
		if (temp_current->next != NULL ) {
			temp_next = temp_current->next;
		}
	}
	if (list == temp_current) {
		if (list->next != NULL ) {
			list = list->next;
		} else {
			list = NULL;
		}
	}
	free(temp_current);
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值