链表的创建及基本操作(增、删、改、查)

一、链表概述

  • 链表是一种物理存储结构上非连续,非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
  • 与数组区别:
    链表是链式的存储结构,数组是顺序的存储结构。
    链表通过指针来连接元素与元素,数组则是把所有元素按次序依次存储。
    优缺点如图:
    数组与链表的优缺点

二、创建链表前的准备工作

1.结点构成

  • 数据域:存放用户需要的实际数据
  • 指针域:存放下一个结点的地址

2.malloc函数

  • 函数原型:void *malloc(unsigned int size);
  • 功能:在内存的动态存储区申请一个长度为size字节的连续存储空间,并返回该存储空间的起始地址。如果没有足够的内存空间可分配,则函数的返回值为空指针NULL。
  • 说明:这个函数返回的是个void类型指针,所以在使用时应注意强制类型转换成需要的指针类型。

3.free函数

  • 函数原型:void free(void *p);
  • 功能:将指针变量p指向的存储空间释放,交换给系统。

三、创建链表

1.尾插法

  • 特点:输入顺序(存储顺序)与输出顺序相同
  • 图解:尾插法
  • 核心代码
end->next = p;     //尾节点连接新节点
end = p;           //尾节点向后移动成为新的尾节点

2.头插法

  • 特点:输入顺序(存储顺序)与输出顺序相反
  • 图解:
    头插法
  • 核心代码:
p->next = head->next;    //新节点指向头节点的下一位
head->next = p;         //头节点连接新节点

3.完整代码

struct node *creat() {
   
	//定义头指针head,尾指针end,和指向新节点的指针p
	struct node *head= NULL, *end = NULL, *p = NULL;    //初始化
	head = (struct node *)malloc(sizeof(strcut node));  //动态申请内存
	end = head;
	head->next = NULL
  • 30
    点赞
  • 96
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
链表是一种常见的数据结构,用于存储和操作数据元素。下面是关于C语言链表的一些基本操作: 1. 添加节点(加): - 创建一个新节点。 - 将新节点的数据赋值。 - 将新节点的next指针指向当前链表的头节点(或者指向NULL,如果链表为空)。 - 更新链表的头节点指针,使其指向新节点。 2. 除节点(除): - 找到要除的节点以及它的前一个节点。 - 将前一个节点的next指针指向要除节点的下一个节点。 - 释放要除节点的内存。 3. 修节点(变): - 遍历链表,找到要修的节点。 - 修该节点的数据。 4. 找节点(询): - 从链表的头节点开始遍历,直到找到包含所需数据的节点。 - 如果找到了,返回该节点;如果没有找到,返回NULL或者其他特定值。 下面是一个简单的示例代码,演示了链表操作: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void insertNode(struct Node** head, int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = *head; *head = newNode; } void deleteNode(struct Node** head, int data) { struct Node* currentNode = *head; struct Node* previousNode = NULL; if (currentNode != NULL && currentNode->data == data) { *head = currentNode->next; free(currentNode); return; } while (currentNode != NULL && currentNode->data != data) { previousNode = currentNode; currentNode = currentNode->next; } if (currentNode == NULL) { printf("Node not found.\n"); return; } previousNode->next = currentNode->next; free(currentNode); } void modifyNode(struct Node* head, int data, int newData) { while (head != NULL) { if (head->data == data) { head->data = newData; return; } head = head->next; } printf("Node not found.\n"); } struct Node* searchNode(struct Node* head, int data) { while (head != NULL) { if (head->data == data) { return head; } head = head->next; } return NULL; } void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\n"); } int main() { struct Node* head = NULL; insertNode(&head, 3); insertNode(&head, 5); insertNode(&head, 7); printf("Initial list: "); printList(head); deleteNode(&head, 5); printf("Updated list after deletion: "); printList(head); modifyNode(head, 7, 9); printf("Updated list after modification: "); printList(head); struct Node* searchedNode = searchNode(head, 9); if (searchedNode != NULL) { printf("Searched node found: %d\n", searchedNode->data); } else { printf("Searched node not found.\n"); } return 0; } ``` 这个示例代码展示了链表操作,你可以根据需要进行修和扩展。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值