线性表基本概念:
线性表是具有相同数据类型的 n(n≥0)个数据元素的有限序列。
线性表的实现:
顺序存储:
定义:它使用一组地址连续的存储单元依次存储线性表中的元素。在顺序表中,逻辑上相邻的元素其物理存储位置也相邻。
基本实现方法:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // 顺序表的最大容量
typedef struct {
int *data; // 存储数据的数组
int length; // 当前顺序表的长度
int capacity; // 顺序表的容量
} SeqList;
// 初始化顺序表
void initList(SeqList *L) {
L->data = (int *)malloc(MAX_SIZE * sizeof(int));
if (L->data == NULL) {
printf("内存分配失败!\n");
return;
}
L->length = 0;
L->capacity = MAX_SIZE;
}
// 插入元素到指定位置
int insertList(SeqList *L, int pos, int element) {
if (pos < 1 || pos > L->length + 1) {
printf("插入位置不合法!\n");
return 0;
}
if (L->length == L->capacity) {
printf("顺序表已满,无法插入!\n");
return 0;
}
for (int i = L->length; i >= pos; i--) {
L->data[i] = L->data[i - 1];
}
L->data[pos - 1] = element;
L->length++;
return 1;
}
// 删除指定位置的元素
int deleteList(SeqList *L, int pos) {
if (pos < 1 || pos > L->length) {
printf("删除位置不合法!\n");
return 0;
}
for (int i = pos; i < L->length; i++) {
L->data[i - 1] = L->data[i];
}
L->length--;
return 1;
}
// 查找指定元素的位置
int searchList(SeqList *L, int element) {
for (int i = 0; i < L->length; i++) {
if (L->data[i] == element) {
return i + 1;
}
}
return -1;
}
// 打印顺序表
void printList(SeqList *L) {
for (int i = 0; i < L->length; i++) {
printf("%d ", L->data[i]);
}
printf("\n");
}
int main() {
SeqList L;
initList(&L);
insertList(&L, 1, 10);
insertList(&L, 2, 20);
insertList(&L, 3, 30);
printf("初始顺序表: ");
printList(&L);
insertList(&L, 2, 15);
printf("插入元素后的顺序表: ");
printList(&L);
deleteList(&L, 2);
printf("删除元素后的顺序表: ");
printList(&L);
int element = 20;
int position = searchList(&L, element);
if (position!= -1) {
printf("%d 在顺序表中的位置是: %d\n", element, position);
} else {
printf("%d 不在顺序表中\n", element);
}
return 0;
}
顺序存储特点:
-
随机访问:可以在
O(1)
的时间复杂度内通过下标直接访问任意元素。 -
存储密度高:不需要额外的指针来表示元素之间的逻辑关系,因此存储密度高。
但也存在一些缺点:
-
插入和删除操作效率低:在进行插入和删除操作时,可能需要移动大量的元素,时间复杂度为
O(n)
。 -
存储空间大小固定:在初始化时需要预先分配固定大小的存储空间,如果空间不足需要重新分配更大的空间并进行数据迁移。
链式存储:
链式存储是一种数据存储方式,其中数据元素通过指针链接在一起,形成一个链表结构。
在链式存储中,每个数据元素所在的存储单元称为节点(Node)。节点通常由两部分组成:
-
数据域:用于存储数据元素的具体信息。
-
指针域:用于存储指向下一个节点的地址或指针。
通过这种指针的连接,使得链表中的节点可以按照一定的顺序依次访问。
单链表(Singly Linked List)是链表的一种常见形式。
单链表的特点包括:
-
节点的链接是单向的,即每个节点只能通过指针指向下一个节点,而不能指向前一个节点。
-
要访问链表中的某个节点,必须从表头节点开始,沿着指针依次遍历。
-
插入和删除操作相对简单,只需修改相关节点的指针即可,但在特定位置插入或删除时,需要先找到目标位置。
-
不能像顺序表那样进行随机访问,只能顺序访问。
单链表的基本操作实现:
#include <stdio.h>
#include <stdlib.h>
// 单链表节点结构体
typedef struct ListNode {
int data;
struct ListNode *next;
} ListNode;
// 创建新节点
ListNode* createNode(int data) {
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
if (newNode == NULL) {
printf("内存分配失败\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// 在表头插入节点
void insertAtHead(ListNode** head, int data) {
ListNode* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}
// 在指定位置插入节点
void insertAtIndex(ListNode* head, int index, int data) {
if (index < 0) {
printf("插入位置不合法\n");
return;
}
ListNode* newNode = createNode(data);
if (index == 0) {
newNode->next = head;
head = newNode;
return;
}
ListNode* curr = head;
int count = 0;
while (curr!= NULL && count < index - 1) {
curr = curr->next;
count++;
}
if (curr == NULL) {
printf("插入位置超出链表长度\n");
return;
}
newNode->next = curr->next;
curr->next = newNode;
}
// 删除指定节点
void deleteNode(ListNode** head, int data) {
ListNode* curr = *head;
ListNode* prev = NULL;
while (curr!= NULL) {
if (curr->data == data) {
if (prev == NULL) {
*head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
return;
}
prev = curr;
curr = curr->next;
}
printf("未找到要删除的数据\n");
}
// 打印链表
void printList(ListNode* head) {
ListNode* curr = head;
while (curr!= NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
// 释放链表内存
void freeList(ListNode* head) {
ListNode* curr = head;
ListNode* temp;
while (curr!= NULL) {
temp = curr;
curr = curr->next;
free(temp);
}
}
int main() {
ListNode* head = NULL;
insertAtHead(&head, 10);
insertAtHead(&head, 20);
insertAtIndex(head, 1, 15);
printf("链表: ");
printList(head);
deleteNode(&head, 20);
printf("删除元素后的链表: ");
printList(head);
freeList(head);
return 0;
}
双链表(Doubly Linked List)是链表的一种变体。
在双链表中,每个节点不仅有指向下一个节点的指针(称为“后继指针”),还有指向前一个节点的指针(称为“前驱指针”)。
双链表的特点包括:
-
可以双向遍历,既可以从表头向表尾遍历,也可以从表尾向表头遍历。
-
插入和删除操作在某些情况下比单链表更方便,因为不需要像单链表那样为了找到前一个节点而进行遍历。
-
由于每个节点需要额外存储一个指针,所以空间复杂度相对单链表略高
以下是用 C 语言实现双链表节点的结构和一些基本操作(创建、插入、删除、遍历等)的示例代码:
#include <stdio.h>
#include <stdlib.h>
// 双链表节点结构体
typedef struct DoublyListNode {
int data;
struct DoublyListNode *prev;
struct DoublyListNode *next;
} DoublyListNode;
// 创建新节点
DoublyListNode* createDoublyNode(int data) {
DoublyListNode* newNode = (DoublyListNode*)malloc(sizeof(DoublyListNode));
if (newNode == NULL) {
printf("内存分配失败\n");
return NULL;
}
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// 在表头插入节点
void insertAtHead(DoublyListNode** head, int data) {
DoublyListNode* newNode = createDoublyNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
// 在表尾插入节点
void insertAtTail(DoublyListNode** head, int data) {
DoublyListNode* newNode = createDoublyNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
DoublyListNode* curr = *head;
while (curr->next!= NULL) {
curr = curr->next;
}
curr->next = newNode;
newNode->prev = curr;
}
// 在指定位置插入节点
void insertAtIndex(DoublyListNode* head, int index, int data) {
if (index < 0) {
printf("插入位置不合法\n");
return;
}
DoublyListNode* newNode = createDoublyNode(data);
if (index == 0) {
insertAtHead(&head, data);
return;
}
DoublyListNode* curr = head;
int count = 0;
while (curr!= NULL && count < index) {
curr = curr->next;
count++;
}
if (curr == NULL) {
printf("插入位置超出链表长度\n");
return;
}
newNode->prev = curr->prev;
newNode->next = curr;
curr->prev->next = newNode;
curr->prev = newNode;
}
// 删除指定节点
void deleteNode(DoublyListNode** head, int data) {
DoublyListNode* curr = *head;
while (curr!= NULL) {
if (curr->data == data) {
if (curr->prev == NULL) {
*head = curr->next;
if (*head!= NULL) {
(*head)->prev = NULL;
}
} else if (curr->next == NULL) {
curr->prev->next = NULL;
} else {
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
}
free(curr);
return;
}
curr = curr->next;
}
printf("未找到要删除的数据\n");
}
// 正向遍历打印链表
void printForward(DoublyListNode* head) {
DoublyListNode* curr = head;
while (curr!= NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
// 反向遍历打印链表
void printBackward(DoublyListNode* head) {
DoublyListNode* curr = head;
while (curr->next!= NULL) {
curr = curr->next;
}
while (curr!= NULL) {
printf("%d ", curr->data);
curr = curr->prev;
}
printf("\n");
}
// 释放链表内存
void freeList(DoublyListNode* head) {
DoublyListNode* curr = head;
DoublyListNode* temp;
while (curr!= NULL) {
temp = curr;
curr = curr->next;
free(temp);
}
}
int main() {
DoublyListNode* head = NULL;
insertAtHead(&head, 10);
insertAtHead(&head, 20);
insertAtTail(&head, 30);
insertAtIndex(head, 1, 15);
printf("正向遍历链表: ");
printForward(head);
printf("反向遍历链表: ");
printBackward(head);
deleteNode(&head, 20);
printf("删除元素后的正向遍历链表: ");
printForward(head);
freeList(head);
return 0;
}
循环链表(Circular Linked List)是另一种特殊的链表结构。
它分为两种类型:
-
单循环链表:尾节点的指针不是指向
NULL
,而是指向头节点,从而形成一个环形结构。这样就可以从任何一个节点开始,顺着指针遍历整个链表。 -
双循环链表:不仅尾节点指向头节点,头节点也指向尾节点,同时每个节点的前驱指针和后继指针也形成了循环。
循环链表的特点包括:
-
可以实现从任何节点开始的遍历,无需特殊处理表头和表尾。
-
在某些应用中,如环形缓冲区、约瑟夫环问题等,能更方便地进行操作。
#include <stdio.h>
#include <stdlib.h>
// 单循环链表节点结构体
typedef struct CircularListNode {
int data;
struct CircularListNode *next;
} CircularListNode;
// 创建新节点
CircularListNode* createCircularNode(int data) {
CircularListNode* newNode = (CircularListNode*)malloc(sizeof(CircularListNode));
if (newNode == NULL) {
printf("内存分配失败\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// 在表头插入节点
void insertAtHead(CircularListNode** head, int data) {
CircularListNode* newNode = createCircularNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = *head;
return;
}
CircularListNode* curr = *head;
while (curr->next!= *head) {
curr = curr->next;
}
newNode->next = *head;
curr->next = newNode;
*head = newNode;
}
// 删除指定节点
void deleteNode(CircularListNode** head, int data) {
if (*head == NULL) {
printf("链表为空,无法删除\n");
return;
}
CircularListNode* curr = *head;
CircularListNode* prev = NULL;
do {
if (curr->data == data) {
if (curr == *head) {
*head = (*head)->next;
}
prev->next = curr->next;
free(curr);
return;
}
prev = curr;
curr = curr->next;
} while (curr!= *head);
printf("未找到要删除的数据\n");
}
// 打印链表
void printList(CircularListNode* head) {
if (head == NULL) {
printf("链表为空\n");
return;
}
CircularListNode* curr = head;
do {
printf("%d ", curr->data);
curr = curr->next;
} while (curr!= head);
printf("\n");
}
// 释放链表内存
void freeList(CircularListNode* head) {
if (head == NULL) {
return;
}
CircularListNode* curr = head;
CircularListNode* temp;
do {
temp = curr;
curr = curr->next;
free(temp);
} while (curr!= head);
}
int main() {
CircularListNode* head = NULL;
insertAtHead(&head, 10);
insertAtHead(&head, 20);
insertAtHead(&head, 30);
printf("循环链表: ");
printList(head);
deleteNode(&head, 20);
printf("删除元素后的循环链表: ");
printList(head);
freeList(head);
return 0;
}
静态链表(Static Linked List)是用数组来模拟链表的一种数据结构。
在静态链表中,数组的每个元素包含两个部分:数据域和游标(类似于指针)。游标用于指示下一个元素在数组中的位置。
静态链表的特点包括:
-
利用了数组的连续存储特性,同时模拟了链表的动态插入和删除操作。
-
不需要频繁地进行内存的分配和释放,适用于不支持指针操作或者内存管理较为复杂的场景。
-
但是,由于数组大小固定,可能存在空间浪费的情况。
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // 静态链表的最大长度
typedef struct {
int data;
int cur; // 游标,指向下一个节点在数组中的位置
} StaticListNode;
// 初始化静态链表
void initStaticList(StaticListNode list[], int *available) {
for (int i = 0; i < MAX_SIZE - 1; i++) {
list[i].cur = i + 1;
}
list[MAX_SIZE - 1].cur = 0; // 链表结束标志
*available = 1; // 空闲链表头指针
}
// 从备用空间获取一个节点
int mallocStaticList(StaticListNode list[], int *available) {
int i = *available;
if (*available) {
*available = list[*available].cur;
}
return i;
}
// 回收节点到备用空间
void freeStaticList(StaticListNode list[], int *available, int k) {
list[k].cur = *available;
*available = k;
}
// 插入元素到静态链表
void insertStaticList(StaticListNode list[], int *head, int element) {
int i = mallocStaticList(list, available); // 获取空闲节点
if (i) {
list[i].data = element;
list[i].cur = list[*head].cur; // 新节点指向原表头节点指向的节点
list[*head].cur = i; // 表头节点指向新节点
} else {
printf("存储空间不足,无法插入!\n");
}
}
// 打印静态链表
void printStaticList(StaticListNode list[], int head) {
int p = list[head].cur;
while (p) {
printf("%d ", list[p].data);
p = list[p].cur;
}
printf("\n");
}
int main() {
StaticListNode list[MAX_SIZE];
int available;
int head = 0; // 初始化表头
initStaticList(list, &available);
insertStaticList(list, &head, 10);
insertStaticList(list, &head, 20);
insertStaticList(list, &head, 30);
printf("静态链表: ");
printStaticList(list, head);
return 0;
}