c语言实现:链表创建、插入、删除、翻转

server/2024/9/25 19:10:20/
#include <stdio.h>
#include <stdlib.h>// 链表创建
typedef struct Node{int data;struct Node* next;
} Node;// 创建一个节点
Node* createNode(int data){Node* newNode = (Node* )malloc(sizeof(Node));newNode->data = data;newNode->next = NULL;return newNode;
}// 创建并插入一个节点
void insertNode(Node* head, int data){ // 要修改head得传入地址Node* insert = createNode(data);insert->next = head->next;head->next = insert;
}// 打印链表
void nodeListPrint(Node* head){int index = 0;head = head->next;while(head != NULL){printf("index: %d, head->data: %d\n", index, head->data);head = head->next;index ++;}
}// 根据data的值获取节点
Node* getNodeByData(Node* head, int data){while(head != NULL){if(head->data == data) return head;head = head->next;}printf("no wish node: %d\n", data);return NULL;
}// 根据data的值删除节点
void deleteNodeByData(Node* head, int data){Node* delete = getNodeByData(head, data);if(delete == NULL) return;while(head != NULL && head->next != delete) head = head->next;head->next = delete->next;delete->next = NULL;free(delete);
}// 链表翻转
void nodeListReverse(Node** head){Node* nodehead = *head; Node* oldhead = (*head)->next;Node* next = NULL;*head = NULL;while(oldhead != NULL){next = oldhead->next;oldhead->next = *head;*head = oldhead;oldhead = next;}nodehead->next = *head;*head = nodehead;
}int main(){Node* head = (Node* )malloc(sizeof(Node)); // 创建头节点for(int i = 1; i <= 10; i ++) insertNode(head, i);printf("1.完整链表如下:\n");nodeListPrint(head);printf("2.删除后的链表如下:\n");deleteNodeByData(head, 2);deleteNodeByData(head, 6);nodeListPrint(head);printf("3.链表翻转如下:\n");nodeListReverse(&head);nodeListPrint(head);printf("4.删除后的链表如下:\n");deleteNodeByData(head, 7);deleteNodeByData(head, 8);nodeListPrint(head);return 0;}

在这里插入图片描述

其中链表翻转也可以不用二阶指针,但是需要做返还在主函数要接收

// 链表翻转一阶指针
Node* nodeListReverse1(Node* head){Node* nodehead = head;Node* nhead = NULL;Node* next = NULL;head = head->next;while(head != NULL){next = head->next;head->next = nhead;nhead = head;head = next;}nodehead->next = nhead;nhead = nodehead;return nhead;
}

在这里插入图片描述
在这里插入图片描述
结果仍然是一样的

其他补充:

Makefile可以这样写:

runm: clean MAIN./MAINMAIN: main.ogcc -o MAIN main.omain.o: main.cgcc -c main.c -o main.oclean:rm -f *.o MAIN TESTrunt: clean TEST./TESTTEST: test.ogcc -o TEST test.otest.o: test.cgcc -c test.c -o test.o

优化后如下所示:

TRV1 = MAIN
TRV2 = TEST
TEP1 = main.o
TEP2 = test.o
ORG1 = main.c
ORG2 = test.crunm: clean $(TRV1)./$(TRV1)$(TRV1): $(TEP1)gcc -o $@ $<$(TEP1): $(ORG1)gcc -c $< -o $@clean:rm -f *.o MAIN TESTrunt: clean $(TRV2)./$(TRV2)$(TRV2): $(TEP2)gcc -o $@ $<$(TEP2): $(ORG2)gcc -c $< -o $@

http://www.ppmy.cn/server/121962.html

相关文章

前端开发之装饰器模式

介绍 装饰器模式 是在不修改对象内部结构的情况下&#xff0c;动态地给对象添加功能的一种设计模式。在软件开发中&#xff0c;有时候我们需要为已有对象添加一些额外的行为&#xff0c;但不希望修改该对象的代码&#xff0c;装饰器模式可以很好的满足这一需求。 在TypeScrip…

影响CDN的因素

数字化转型的趋势下&#xff0c;企业纷纷将业务平台转移到互联网上&#xff0c;但过去的网站配置明显不能满足现有的访问量&#xff0c;常常造成访问拥堵、页面加载慢等问题。CDN的出现&#xff0c;极大地缓解了网络拥堵的情况&#xff0c;但很多人却发现&#xff0c;自己的网站…

Python | Leetcode Python题解之第421题数组中两个数的最大异或值

题目&#xff1a; 题解&#xff1a; class Trie:def __init__(self):# 左子树指向表示 0 的子节点self.left None# 右子树指向表示 1 的子节点self.right Noneclass Solution:def findMaximumXOR(self, nums: List[int]) -> int:# 字典树的根节点root Trie()# 最高位的二…

鸿蒙OpenHarmony【小型系统基础内核(物理内存管理)】子系统开发

物理内存管理 基本概念 物理内存是计算机上最重要的资源之一&#xff0c;指的是实际的内存设备提供的、可以通过CPU总线直接进行寻址的内存空间&#xff0c;其主要作用是为操作系统及程序提供临时存储空间。LiteOS-A内核管理物理内存是通过分页实现的&#xff0c;除了内核堆占…

tauri开发软件中,使用tauri自带的api用浏览器打开指定的url链接

有能力的可以看官方文档&#xff1a;shell | Tauri Apps 就是使用这个api来打开指定的url链接&#xff0c;要在tauri.config.json中配置打开这个api&#xff1a; 然后在前端页面中导入使用&#xff1a; import { open } from tauri-apps/api/shell; // opens the given URL o…

apache paimon简介(官翻)

介绍 如下架构所示: 读/写操作: Paimon 支持多样化的数据读写方式,并支持 OLAP 查询。 读取: 支持从历史快照(批处理模式)中消费数据,从最新偏移量(流处理模式)中读取数据,或以混合方式读取增量快照。写入: 支持从数据库变更日志(CDC)进行流式同步,从离线数据中…

el-table多选,分页切换时,选中内容不变;清空多选的内容

el-table中添加:row-key“getRowKeys” 设置true【 :reserve-selection“true”】 :row-key"getRowKeys" <el-table-column type"selection" :reserve-selection"true" width"55" align"center" fixed"left" …

Python提供内置正则表达式库

正则表达式是一种强大的文本处理工具&#xff0c;可以匹配文本片段的模式 最简单的正则表达式就是普通的字符串&#xff0c;可以匹配自身 要注意的是&#xff0c;正则表达式并不是一个程序&#xff0c;它使用一种特定的语法模式来描述在搜索文本时要匹配的一个或多个字符串。正…