顺序表
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct array_list {
int data[MAX_SIZE];
int size;
};
struct array_list list;
// 初始化顺序表
void init_array_list() {
list.size = 0;
}
// 在顺序表末尾插入新元素
void insert_array_list(int value) {
if (list.size < MAX_SIZE) {
list.data[list.size] = value;
list.size++;
} else {
printf("Error: array list is full\n");
}
}
// 在顺序表中查找特定元素
int search_array_list(int value) {
for (int i = 0; i < list.size; i++) {
if (list.data[i] == value) {
return i;
}
}
return -1;
}
// 在顺序表中删除特定元素
void delete_array_list(int value) {
int index = search_array_list(value);
if (index != -1) {
for (int i = index; i < list.size - 1; i++) {
list.data[i] = list.data[i + 1];
}
list.size--;
}
}
// 打印顺序表中所有元素
void print_array_list() {
printf("List: ");
for (int i = 0; i < list.size; i++) {
printf("%d ", list.data[i]);
}
printf("\n");
}
int main() {
init_array_list();
insert_array_list(1);
insert_array_list(2);
insert_array_list(3);
insert_array_list(4);
print_array_list();
delete_array_list(2);
print_array_list();
int index = search_array_list(3);
if (index != -1) {
printf("Found element 3 at index %d\n", index);
} else {
printf("Element 3 not found\n");
}
return 0;
}
单链表
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* head = NULL;
// 在链表头插入新节点
void insert_node(int data) {
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = head;
head = new_node;
}
// 在链表中查找特定节点
struct node* search_node(int data) {
struct node* current = head;
while (current != NULL) {
if (current->data == data) {
return current;
}
current = current->next;
}
return NULL;
}
// 在链表中删除特定节点
void delete_node(int data) {
struct node* current = head;
struct node* prev = NULL;
while (current != NULL) {
if (current->data == data) {
if (prev == NULL) {
head = current->next;
} else {
prev->next = current->next;
}
free(current);
return;
}
prev = current;
current = current->next;
}
}
// 打印链表中所有节点的数据
void print_list() {
struct node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// 释放链表中所有节点的内存
void free_list() {
struct node* current = head;
while (current != NULL) {
struct node* temp = current;
current = current->next;
free(temp);
}
head = NULL;
}
int main() {
insert_node(1);
insert_node(2);
insert_node(3);
insert_node(4);
printf("Initial list:\n");
print_list();
delete_node(2);
printf("List after deleting 2:\n");
print_list();
struct node* node_3 = search_node(3);
if (node_3 != NULL) {
printf("Found node with data 3\n");
} else {
printf("Node with data 3 not found\n");
}
free_list();
return 0;
}
双向链表
#include <stdio.h>
#include <stdlib.h>
// 双向链表结构体
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
// 在双向链表头部插入节点
void insertAtHead(Node** head, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->prev = NULL;
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
}
*head = newNode;
}
// 在双向链表尾部插入节点
void insertAtTail(Node** head, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
Node* tail = *head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = newNode;
newNode->prev = tail;
}
// 从双向链表中删除指定节点
void deleteNode(Node** head, Node* nodeToDelete) {
if (*head == NULL || nodeToDelete == NULL) {
return;
}
if (*head == nodeToDelete) {
*head = nodeToDelete->next;
}
if (nodeToDelete->next != NULL) {
nodeToDelete->next->prev = nodeToDelete->prev;
}
if (nodeToDelete->prev != NULL) {
nodeToDelete->prev->next = nodeToDelete->next;
}
free(nodeToDelete);
}
// 输出双向链表
void printList(Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
// 在头部插入节点
insertAtHead(&head, 3);
insertAtHead(&head, 2);
insertAtHead(&head, 1);
printf("双向链表头部插入节点: ");
printList(head);
// 在尾部插入节点
insertAtTail(&head, 4);
insertAtTail(&head, 5);
printf("双向链表尾部插入节点: ");
printList(head);
// 删除指定节点
deleteNode(&head, head->next->next);
printf("删除节点后的双向链表: ");
printList(head);
return 0;
}