数据结构--单向链表

1.按位置查找返回元素的值

复制代码
// 按位置查找元素
int query_num(node_p P, int pos) {
	if (P == NULL) {
		return 0;
	}
 
	if (pos <= 0 || pos > P->len) {
		printf("所选插入位置不准确\n");
		return 0;
	}
 
	int i;
	node_p H = P;
	for (i = 0; i < pos; i++, H = H->next);
 
	return H->data;
 
}

2.按值修改(多个一样的值改第一个)

复制代码
// 按值修改
void update_value(node_p P, int target, int value) {
	if (P == NULL) {
		return;
	}
	node_p H = P->next;
	int i;
	for (i = 0; i < P->len; i++, H = H->next) {
		if (target == H->data) {
			H->data = value;
			break;
		}
	}
 
	if (i == P->len) {
		printf("没有要修改的值\n");
	}
}

3.单向链表的逆置

复制代码
//13、逆置
void reverse_link(node_p H)
{
	if(H==NULL){return;}
	if(empty_link(H)){return;}
	if(H->next->next==NULL){return;}
	//提前将第二个结点开始链表保存下来
	node_p p = H->next->next;
	node_p q;
	//让原来的第一个结点变成现在的最后一个结点
	H->next->next = NULL;
	//循环头插
	//只要p的指向的结点不为空说明要头插
	while(p!=NULL)
	{
		//先保留下一个要头插结点的首地址
		q = p->next;
		//头插
		p->next = H->next;
		H->next = p;
		p = q;  //让p指向下一个要头插的结点
	}
}

4.尝试实现单向循环链表

a.特点:尾结点指向头结点

b.创建

c.头插、尾插、任意位置插入

d.头删、尾删、任意位置删除

头文件

复制代码
#ifndef __LOOP_H__
#define __LOOP_H__
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
	union
	{
		int len;
		int data;
	};
	struct node *next;
}node,*node_p;
//1、申请单向循环链表
node_p create_loop();
//2、申请结点
node_p create_node(int value);
//3、判空
int empty_loop(node_p H);
//4、头插
void insert_head(node_p H,int value);
//5、尾插
void insert_tail(node_p H,int value);
//6、头删
void dele_head(node_p H);
//7、尾删
void dele_tail(node_p H);
//8、输出
void show_loop(node_p H);
node_p delete(node_p H);
//10、删除头结点后的单向循环链表的输出
void show_no_head(node_p H);




#endif

功能文件

复制代码
#include "loop.h"
//1、申请单向循环链表
node_p create_loop()
{
	node_p H = (node_p)malloc(sizeof(node));
	if(H==NULL)
	{
		printf("申请空间失败\n");
		return NULL;
	}
	H->len=0;
	H->next = H;
	return H;
}
//2、申请结点
node_p create_node(int value)
{
	node_p new = (node_p)malloc(sizeof(node));
	new->next = NULL;
	new->data = value;
	return new;
}
//3、判空
int empty_loop(node_p H)
{
	if(H==NULL){return -1;}
	return H->next==H?1:0;
}
//4、头插
void insert_head(node_p H,int value)
{
	if(H==NULL){return;}
	node_p new = create_node(value);
	//新结点的后继指向头结点的后继
	new->next = H->next;
	H->next = new;
	H->len++;
}
//5、尾插
void insert_tail(node_p H,int value)
{
	if(H==NULL){return;}
	node_p p=H; //p用于遍历链表,找到最后一个结点
	while(p->next!=H)
	{
		p=p->next;
	}
	node_p new = create_node(value);
	//退出循环后p指向最后一个结点
	p->next = new;
	new->next=H;
	//new->next = p->next;
	//p->next = new;
	H->len++;
}
//6、头删
void dele_head(node_p H)
{
	if(H==NULL){return;}
	if(empty_loop(H)){return;}
	//保存要删除结点的首地址
	node_p p=H->next;
	//让头结点指向原来的第二个结点
	H->next=p->next;
	free(p);
	H->len--;
	return;
}
//7、尾删
void dele_tail(node_p H)
{
	if(H==NULL){return;}
	if(empty_loop(H)) {return;}
	node_p p=H;
	//尾删要找倒数第二个结点
	while(p->next->next!=H)
	{
		p=p->next;
	}
	//循环退出时p指向倒数第二个节点
	node_p q=p->next;
	p->next=H;
	free(q);
	//free(p->next);  //先释放最后一个结点
	//p->next = H;
	H->len--;
}
//8、输出
void show_loop(node_p H)
{
	if(H==NULL){return;}
	if(empty_loop(H)){return;}
	node_p p = H->next;
	while(p!=H)
	{
		printf("%d->",p->data);
		p = p->next;
	}
	printf("H\n");
}
//9、删除单向循环链表的头结点
//删除头结点后需要返回给主调函数处新的链表的头
node_p delete(node_p H)
{
	if(H==NULL){return NULL;}
	if(empty_loop(H)){return NULL;}
	//保存第一个结点的首地址
	node_p p = H->next;
	//找到最后一个结点
	node_p tail = H->next;
	while(tail->next!=H)
	{
		tail=tail->next;
	}
	//让最后一个结点的后继节点变成原来的第一个结点
	tail->next = p;
	//释放头结点
	free(H);
	return p;
}
//10、删除头结点后的单向循环链表的输出
void show_no_head(node_p H)
{
	if(H==NULL){return;}
	//不需要再判空
	//因为没有头结点了传过来的结点只要不是空说明链表中就有元素
	node_p p = H;
	do
	{
		printf("%d->",p->data);
		p = p->next;
	}while(p!=H);
	printf("H\n");
	//需要第一次循环时不判断条件
	//只能使用do··while
}

主函数文件

复制代码
#include "loop.h"
int main()
{
	node_p H = create_loop();
	insert_head(H,67);
	insert_head(H,90);
	insert_head(H,34);
	insert_head(H,8);
	show_loop(H);
	printf("H->next=%p\n",H->next);
	H = delete(H);  //删除头结点后链表已经没有头结点了
	//H需要重新被赋值
	show_no_head(H);
	return 0;
}
相关推荐
zylyyyyyy1 小时前
Polar (极化)码的译码Ⅱ--BP 译码算法
数据结构·算法
liujing102329292 小时前
Day05_数据结构(二叉树&快速排序&插入排序&二分查找)
数据结构
YuTaoShao2 小时前
Java八股文——数据结构「排序算法篇」
java·数据结构·算法·面试·排序算法·八股文
随缘而动,随遇而安9 小时前
第八十二篇 大数据开发基础:树形数据结构深度解析与实战指南(附创新生活案例)
大数据·开发语言·数据结构
药95511 小时前
数据结构 4 (栈和队列)
java·开发语言·数据结构
2401_8582861112 小时前
CD45.【C++ Dev】STL库的list的使用
开发语言·数据结构·c++·list
int型码农12 小时前
数据结构第八章(五)-外部排序和败者树
c语言·数据结构·算法·排序算法
好易学·数据结构12 小时前
可视化图解算法52:数据流中的中位数
数据结构·算法·leetcode·面试·力扣·笔试·牛客
点云SLAM14 小时前
PyTorch 中Tensor常用数据结构(int, list, numpy array等)互相转换和实战示例
数据结构·人工智能·pytorch·算法·list·numpy·tensor
苦学LCP的小猪16 小时前
LeeCode94二叉树的中序遍历
数据结构·python·算法·leetcode