用C语言的代码写一个链表,它要拥有头插,尾插,按位置查找,按值查找,插入,删除,打印,求表长的功能
时间: 2024-05-03 09:17:34 浏览: 17
以下是基于C语言实现的链表代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 链表结点的结构体定义
typedef struct node {
int data;
struct node *next;
} Node;
// 链表结构体定义
typedef struct list {
Node *head; // 头结点
Node *tail; // 尾结点
int length; // 链表长度
} List;
// 创建一个新的结点
Node* create_node(int data) {
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
// 初始化链表
void init_list(List *list) {
list->head = NULL;
list->tail = NULL;
list->length = 0;
}
// 头插法插入结点
void insert_at_head(List *list, int data) {
Node *new_node = create_node(data);
if (list->head == NULL) {
list->head = new_node;
list->tail = new_node;
} else {
new_node->next = list->head;
list->head = new_node;
}
list->length++;
}
// 尾插法插入结点
void insert_at_tail(List *list, int data) {
Node *new_node = create_node(data);
if (list->tail == NULL) {
list->head = new_node;
list->tail = new_node;
} else {
list->tail->next = new_node;
list->tail = new_node;
}
list->length++;
}
// 按位置查找结点
Node* find_by_index(List *list, int index) {
if (index < 0 || index >= list->length) {
return NULL;
}
Node *cur = list->head;
for (int i = 0; i < index; i++) {
cur = cur->next;
}
return cur;
}
// 按值查找结点
Node* find_by_value(List *list, int value) {
Node *cur = list->head;
while (cur != NULL && cur->data != value) {
cur = cur->next;
}
return cur;
}
// 在指定位置插入结点
void insert_at_index(List *list, int index, int data) {
if (index < 0 || index > list->length) {
return;
}
if (index == 0) {
insert_at_head(list, data);
} else if (index == list->length) {
insert_at_tail(list, data);
} else {
Node *prev = find_by_index(list, index - 1);
Node *new_node = create_node(data);
new_node->next = prev->next;
prev->next = new_node;
list->length++;
}
}
// 删除指定位置的结点
void delete_by_index(List *list, int index) {
if (index < 0 || index >= list->length) {
return;
}
Node *del_node = NULL;
if (index == 0) {
del_node = list->head;
list->head = del_node->next;
if (list->length == 1) {
list->tail = NULL;
}
} else {
Node *prev = find_by_index(list, index - 1);
del_node = prev->next;
prev->next = del_node->next;
if (del_node == list->tail) {
list->tail = prev;
}
}
free(del_node);
list->length--;
}
// 打印链表
void print_list(List *list) {
Node *cur = list->head;
while (cur != NULL) {
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
// 求表长
int get_length(List *list) {
return list->length;
}
int main() {
List list;
init_list(&list);
insert_at_head(&list, 1);
insert_at_head(&list, 2);
insert_at_head(&list, 3);
insert_at_tail(&list, 4);
insert_at_index(&list, 2, 5);
printf("链表元素:");
print_list(&list);
printf("链表长度:%d\n", get_length(&list));
delete_by_index(&list, 3);
printf("链表元素:");
print_list(&list);
printf("链表长度:%d\n", get_length(&list));
Node *node1 = find_by_index(&list, 2);
if (node1 != NULL) {
printf("第%d个结点的值为:%d\n", 2, node1->data);
}
Node *node2 = find_by_value(&list, 5);
if (node2 != NULL) {
printf("值为%d的结点在第%d个位置\n", 5, node2->data);
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)