数据结构与算法之链表 链表简单题 基础操作考察

数据结构与算法阿木 发布于 4 天前 1 次阅读


链表简单题:基础操作考察

链表是一种常见的数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。链表在计算机科学中有着广泛的应用,如实现栈、队列、哈希表等数据结构。本文将围绕链表的基础操作,探讨一些常见的链表简单题。

1. 链表概述

1.1 链表的定义

链表是一种线性表,它的每个节点包含两部分:数据域和指针域。数据域存储数据元素,指针域存储指向下一个节点的指针。链表的特点是每个节点在内存中可以独立存储,因此插入和删除操作比较灵活。

1.2 链表的分类

根据节点中指针的数量,链表可以分为单链表、双链表和循环链表。

- 单链表:每个节点只有一个指向下一个节点的指针。

- 双链表:每个节点有两个指针,一个指向前一个节点,一个指向下一个节点。

- 循环链表:最后一个节点的指针指向第一个节点,形成一个环。

2. 链表的基础操作

2.1 创建链表

创建链表通常需要定义一个节点结构体,然后根据需要创建节点并连接起来。

c

typedef struct Node {


int data;


struct Node next;


} Node;

Node createList(int n) {


Node head = NULL;


Node tail = NULL;


for (int i = 0; i < n; ++i) {


Node newNode = (Node)malloc(sizeof(Node));


newNode->data = i;


newNode->next = NULL;


if (head == NULL) {


head = newNode;


tail = newNode;


} else {


tail->next = newNode;


tail = newNode;


}


}


return head;


}


2.2 插入节点

插入节点是链表操作中比较常见的操作,可以分为在链表头部、尾部和指定位置插入。

2.2.1 在链表头部插入

c

void insertAtHead(Node head, int data) {


Node newNode = (Node)malloc(sizeof(Node));


newNode->data = data;


newNode->next = head;


head = newNode;


}


2.2.2 在链表尾部插入

c

void insertAtTail(Node head, int data) {


Node newNode = (Node)malloc(sizeof(Node));


newNode->data = data;


newNode->next = NULL;


if (head == NULL) {


head = newNode;


} else {


Node tail = head;


while (tail->next != NULL) {


tail = tail->next;


}


tail->next = newNode;


}


}


2.2.3 在指定位置插入

c

void insertAtPosition(Node head, int position, int data) {


if (position < 0) return;


Node newNode = (Node)malloc(sizeof(Node));


newNode->data = data;


if (position == 0) {


newNode->next = head;


head = newNode;


} else {


Node current = head;


for (int i = 0; current != NULL && i < position - 1; i++) {


current = current->next;


}


if (current == NULL) return;


newNode->next = current->next;


current->next = newNode;


}


}


2.3 删除节点

删除节点同样可以分为删除头部、尾部和指定位置。

2.3.1 删除头部

c

void deleteAtHead(Node head) {


if (head == NULL) return;


Node temp = head;


head = (head)->next;


free(temp);


}


2.3.2 删除尾部

c

void deleteAtTail(Node head) {


if (head == NULL) return;


if ((head)->next == NULL) {


Node temp = head;


head = NULL;


free(temp);


} else {


Node current = head;


while (current->next->next != NULL) {


current = current->next;


}


Node temp = current->next;


current->next = NULL;


free(temp);


}


}


2.3.3 删除指定位置

c

void deleteAtPosition(Node head, int position) {


if (position < 0 || head == NULL) return;


if (position == 0) {


Node temp = head;


head = (head)->next;


free(temp);


} else {


Node current = head;


for (int i = 0; current != NULL && i < position - 1; i++) {


current = current->next;


}


if (current == NULL || current->next == NULL) return;


Node temp = current->next;


current->next = temp->next;


free(temp);


}


}


2.4 查找节点

查找节点可以通过遍历链表来实现。

c

Node findNode(Node head, int data) {


Node current = head;


while (current != NULL) {


if (current->data == data) {


return current;


}


current = current->next;


}


return NULL;


}


2.5 打印链表

打印链表可以通过遍历链表并输出每个节点的数据来实现。

c

void printList(Node head) {


Node current = head;


while (current != NULL) {


printf("%d ", current->data);


current = current->next;


}


printf("");


}


3. 总结

本文介绍了链表的基础操作,包括创建、插入、删除、查找和打印。通过这些操作,我们可以实现对链表的基本操作。在实际应用中,链表是一种非常灵活的数据结构,可以用于实现各种复杂的数据结构。希望本文能帮助读者更好地理解链表的基础操作。