本篇博客主要讲解了链表的各项基本操作。
一、链表的初始化
1.链表的概念
链表是一种物理存储上非连续,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种线性存储结构。
链表由一系列节点(链表中每一个元素称为节点)组成,节点在运行时动态生成 (malloc),每个节点包括两个部分:
一个是存储数据元素的数据域
另一个是存储下一个节点地址的指针域
typedef struct Node {
int data;
struct Node* next;
} Node;
2.创建链表
首先创建一个节点,并且这个节点包含下一节点的地址,而下一节点又包含下下节点的地址,以此类推。
Node* createList(int arr[], int n) {
Node* head = NULL;
Node* tail = NULL;
for (int i = 0; i < n; ++i) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = arr[i];
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
二、链表操作
1.增加节点
将链表末尾的空指针置为新节点的地址,并将新节点的指针域置空。
输入首节点,节点数,和新data。
Node* insert(Node* head, int position, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (position == 1) {
newNode->next = head;
head = newNode;
} else {
Node* p = head;
int count = 1;
while (count < position - 1 && p != NULL) {
p = p->next;
count++;
}
if (p == NULL) {
printf("插入位置无效!\n");
} else {
newNode->next = p->next;
p->next = newNode;
}
}
return head;
}
2.删除节点
以删除特定节点为例
输入首节点和删除的节点
Node* delete(Node* head, int position) {
if (head == NULL) {
printf("链表为空!\n");
return head;
}
if (position == 1) {
Node* temp = head;
head = head->next;
free(temp);
} else {
Node* p = head;
int count = 1;
while (count < position - 1 && p != NULL) {
p = p->next;
count++;
}
if (p == NULL || p->next == NULL) {
printf("删除位置无效!\n");
} else {
Node* temp = p->next;
p->next = temp->next;
free(temp);
}
}
return head;
}
3.查找节点
输入首节点和要查找的元素
Node* find(Node* head, int value) {
Node* p = head;
while (p != NULL) {
if (p->data == value) {
return p;
}
p = p->next;
}
return NULL;
}
4.更改节点
通过查找节点找到要更改的节点,然后这一节点的元素置换为新。
Node*change(Node*head,int value)
{
Node*p=find(head,value);
int new;
scanf("%d",&new);
p->data=new;
}
5.链表的排序
以元素大小为例排序
Node* Sort(Node* head)
{
if (head == NULL || head->next == NULL) {
return head;
}
Node* dummy = (Node*)malloc(sizeof(Node)); // 创建一个虚拟头节点
dummy->next = head;
Node* cur = head->next; // 当前节点
Node* pre = head; // 前一个节点
while (cur != NULL) {
if (cur->data < pre->data) {
Node* p = dummy;
// 找到合适的位置插入当前节点
while (p->next->data < cur->data) {
p = p->next;
}
// 将当前节点插入到合适的位置
pre->next = cur->next;
cur->next = p->next;
p->next = cur;
cur = pre->next;
} else {
pre = pre->next;
cur = cur->next;
}
}
head = dummy->next;
free(dummy);
return head;
}
6.链表的逆置
Node* reverse(Node* head) {
if (head == NULL || head->next == NULL) {
return head;
}
Node* prev = NULL; // 前一个节点
Node* cur = head; // 当前节点
while (cur != NULL) {
Node* nextNode = cur->next; // 下一个节点
cur->next = prev; // 指向前一个节点
prev = cur; // 更新前一个节点
cur = nextNode; // 更新当前节点
}
return prev;
}
7.链表的打印
void printList(Node* head) {
Node* cur = head;
while (cur != NULL) {
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
三、链表的释放
重新定义一个指针q,保存p指向节点的地址,然后p后移保存下一个节点的地址,然后释放q对应的节点,以此类推,直到p为NULL为止。
void freeList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
struct Node* next = temp->next;
free(temp);
temp = next;
}
head = NULL;
}
3512

被折叠的 条评论
为什么被折叠?



