線性表:
由一組具有相同數(shù)據(jù)類型的元素組成的有序序列。線性表中的元素之間存在一個前后關(guān)系,每個元素都有一個唯一的前驅(qū)元素(除了第一個元素)和一個唯一的后繼元素(除了最后一個元素)。
常見操作包括插入元素、刪除元素、查找元素、獲取元素等。
實現(xiàn)方式:
順序表(Sequential List):
順序表使用數(shù)組來實現(xiàn),元素在內(nèi)存中連續(xù)存儲。通過數(shù)組的索引可以直接訪問元素,因此隨機訪問的時間復雜度為O(1)。但是插入和刪除操作需要移動元素,時間復雜度為O(n),其中n是元素的數(shù)量。適合于頻繁進行隨機訪問的場景。
鏈表(Linked List):
鏈表使用節(jié)點(Node)來存儲元素,每個節(jié)點包含一個數(shù)據(jù)項和一個指向下一個節(jié)點的指針。鏈表的元素在內(nèi)存中可以是不連續(xù)的,通過指針可以實現(xiàn)元素之間的連接。鏈表的插入和刪除操作比較靈活,時間復雜度為O(1)。但是隨機訪問的時間復雜度為O(n),需要從頭節(jié)點開始順序查找。適合于頻繁進行插入和刪除操作的場景。
鏈表相關(guān)代碼(C語言):
以下是單鏈表在C語言中的基本操作示例:
```c
#include <stdio.h>
#include <stdlib.h>
// 定義鏈表節(jié)點結(jié)構(gòu)
struct Node {
int data; // 數(shù)據(jù)域
struct Node* next; // 指針域,指向下一個節(jié)點
};
// 初始化鏈表
void initList(struct Node** head) {
*head = NULL; // 將頭指針置為空,表示鏈表為空
}
// 在鏈表末尾添加節(jié)點
void appendNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// 在鏈表指定位置插入節(jié)點
void insertNode(struct Node** head, int data, int position) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (position == 0) {
newNode->next = *head;
*head = newNode;
} else {
struct Node* temp = *head;
int count = 0;
while (temp != NULL && count < position - 1) {
temp = temp->next;
count++;
}
if (temp == NULL) {
printf("Invalid position\n");
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
// 刪除鏈表指定位置的節(jié)點
void deleteNode(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (position == 0) {
*head = temp->next;
free(temp);
} else {
int count = 0;
struct Node* prev = NULL;
while (temp != NULL && count < position) {
prev = temp;
temp = temp->next;
count++;
}
if (temp == NULL) {
printf("Invalid position\n");
return;
}
prev->next = temp->next;
free(temp);
}
}
// 查找鏈表中指定元素的位置
int searchNode(struct Node* head, int data) {
struct Node* temp = head;
int position = 0;
while (temp != NULL) {
if (temp->data == data) {
return position;
}
temp = temp->next;
position++;
}
return -1; // 表示未找到
}
// 修改鏈表指定位置的節(jié)點值
void modifyNode(struct Node* head, int position, int newData) {
struct Node* temp = head;
int count = 0;
while (temp != NULL && count < position) {
temp = temp->next;
count++;
}
if (temp == NULL) {
printf("Invalid position\n");
return;
}
temp->data = newData;
}
// 打印鏈表
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// 釋放鏈表內(nèi)存
void freeList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
struct Node* nextNode = temp->next;
free(temp);
temp = nextNode;
}
}
int main() {
struct Node* head;
initList(&head);
// 在鏈表末尾添加節(jié)點
appendNode(&head, 10);
appendNode(&head, 20);
appendNode(&head, 30);
appendNode(&head, 40);
// 打印鏈表
printf("List: ");
printList(head); // 輸出: 10 20 30 40
// 在鏈表指定位置插入節(jié)點
insertNode(&head, 25, 2);
// 打印鏈表
printf("List after insertion: ");
printList(head); // 輸出: 10 20 25 30 40
// 刪除鏈表指定位置的節(jié)點
deleteNode(&head, 3);
// 打印鏈表
printf("List after deletion: ");
printList(head); // 輸出: 10 20 25 40
// 查找鏈表中指定元素的位置
int position = searchNode(head, 25);
printf("Position of 25: %d\n", position); // 輸出: 2
// 修改鏈表指定位置的節(jié)點值
modifyNode(head, 1, 15);
// 打印鏈表
printf("List after modification: ");
printList(head); // 輸出: 10 15 25 40
// 釋放鏈表內(nèi)存
freeList(head);
return 0;
}