数据结构--单向链表

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;
}
相关推荐
dongzhenmao4 小时前
P1484 种树,特殊情形下的 WQS 二分转化。
数据结构·c++·windows·线性代数·算法·数学建模·动态规划
thusloop6 小时前
380. O(1) 时间插入、删除和获取随机元素
数据结构·算法·leetcode
future14127 小时前
游戏开发日记
数据结构·学习·c#
wjcurry8 小时前
完全和零一背包
数据结构·算法·leetcode
qq_433554548 小时前
C++ 选择排序、冒泡排序、插入排序
数据结构
python_tty8 小时前
排序算法(一):冒泡排序
数据结构·算法·排序算法
学不动CV了9 小时前
ARM单片机OTA解析(二)
arm开发·数据结构·stm32·单片机·嵌入式硬件
kk在加油10 小时前
Redis基础数据结构
数据结构·数据库·redis
IT永勇10 小时前
数据结构-栈
c语言·数据结构·嵌入式开发
Aczone2810 小时前
嵌入式 数据结构学习 (六) 树、哈希表与内核链表
数据结构·学习·算法