单链表的基本操作C语言
时间: 2024-06-15 09:09:22 浏览: 12
单链表是一种常见的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。以下是单链表的基本操作:
1. 初始化链表:创建一个空链表,将头指针指向NULL。
2. 插入节点:在链表的指定位置插入一个新节点。插入操作需要考虑头节点、尾节点和中间节点的情况。
3. 删除节点:删除链表中指定位置的节点。删除操作需要考虑头节点、尾节点和中间节点的情况。
4. 查找节点:根据给定的值,在链表中查找对应的节点。可以遍历整个链表,逐个比较节点的值。
5. 遍历链表:按顺序访问链表中的每个节点,可以使用循环或递归实现。
6. 反转链表:将链表中的节点顺序反转,即原来的尾节点变为头节点,原来的头节点变为尾节点。
7. 合并链表:将两个有序链表合并成一个有序链表,可以使用递归或迭代实现。
8. 计算链表长度:统计链表中节点的个数,可以遍历整个链表并计数。
9. 判断链表是否为空:检查链表是否为空,即头指针是否为NULL。
10. 销毁链表:释放链表占用的内存空间,将所有节点都删除。
相关问题
单链表基本操作c语言
单链表是一种常见的数据结构,用于存储一系列的元素。在C语言中,可以通过定义一个结构体来表示单链表的节点,每个节点包含一个数据域和一个指针域,指向下一个节点。
首先,我们需要定义单链表的节点结构体:
```c
struct ListNode {
int data; // 数据域
struct ListNode* next; // 指针域,指向下一个节点
};
```
接下来,我们可以实现一些基本操作,比如创建单链表、插入节点、删除节点和遍历单链表等。
创建单链表的函数如下所示,可以根据给定的数组创建一个单链表:
```c
struct ListNode* createLinkedList(int arr[], int size) {
struct ListNode* head = NULL; // 头节点指针
struct ListNode* tail = NULL; // 尾节点指针
for (int i = 0; i < size; i++) {
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->data = arr[i];
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
```
插入节点的函数如下所示,可以在指定位置插入一个新节点:
```c
void insertNode(struct ListNode** head, int data, int position) {
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->data = data;
newNode->next = NULL;
// 插入到链表头部
if (position == 0) {
newNode->next = *head;
*head = newNode;
return;
}
struct ListNode* curr = *head;
struct ListNode* prev = NULL;
int count = 0;
// 找到插入位置的前一个节点
while (curr != NULL && count < position) {
prev = curr;
curr = curr->next;
count++;
}
// 插入到链表中间或尾部
if (curr != NULL) {
newNode->next = curr;
}
prev->next = newNode;
}
```
删除节点的函数如下所示,可以删除指定位置的节点:
```c
void deleteNode(struct ListNode** head, int position) {
if (*head == NULL) {
return;
}
struct ListNode* curr = *head;
struct ListNode* prev = NULL;
int count = 0;
// 找到要删除的节点
while (curr != NULL && count < position) {
prev = curr;
curr = curr->next;
count++;
}
// 删除头节点
if (prev == NULL) {
*head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
}
```
遍历单链表的函数如下所示,可以将单链表中的元素依次输出:
```c
void traverseLinkedList(struct ListNode* head) {
struct ListNode* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
}
```
以上是单链表的基本操作实现,你可以根据需要调用这些函数进行单链表的操作。
单链表基本操作C语言代码
当然,我可以为您提供单链表基本操作的C语言代码。请看下面的代码:
```
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *create_list(int n) {
struct node *head = NULL, *p = NULL, *prev = NULL;
int i = 0;
for (i = 0; i < n; i++) {
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node %d: ", i+1);
scanf("%d", &p->data);
if (head == NULL) {
head = p;
}
else {
prev->next = p;
}
prev = p;
}
if (head != NULL) {
prev->next = NULL;
}
return head;
}
void display(struct node *head) {
struct node *p = head;
while (p != NULL) {
printf("%d -> ", p->data);
p = p->next;
}
printf("NULL\n");
}
void search(struct node *head, int key) {
struct node *p = head;
int i = 0;
while (p != NULL && p->data != key) {
i++;
p = p->next;
}
if (p == NULL) {
printf("%d not found in the list.\n", key);
}
else {
printf("%d found at position %d.\n", key, i+1);
}
}
void insert_beg(struct node **head, int data) {
struct node *p = (struct node*)malloc(sizeof(struct node));
p->data = data;
p->next = *head;
*head = p;
}
void insert_end(struct node **head, int data) {
struct node *p = (struct node*)malloc(sizeof(struct node));
struct node *last = *head;
p->data = data;
p->next = NULL;
if (*head == NULL) {
*head = p;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = p;
}
void insert_after(struct node *head, int key, int data) {
struct node *p = head;
while (p != NULL && p->data != key) {
p = p->next;
}
if (p == NULL) {
printf("%d not found in the list.\n", key);
return;
}
struct node *new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
new_node->next = p->next;
p->next = new_node;
}
void delete_beg(struct node **head) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct node *p = *head;
*head = (*head)->next;
printf("Deleted node with data = %d.\n", p->data);
free(p);
}
void delete_end(struct node **head) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct node *p = *head;
struct node *prev = NULL;
while (p->next != NULL) {
prev = p;
p = p->next;
}
if (prev == NULL) {
*head = NULL;
}
else {
prev->next = NULL;
}
printf("Deleted node with data = %d.\n", p->data);
free(p);
}
void delete_node(struct node **head, int key) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct node *p = *head;
struct node *prev = NULL;
while (p != NULL && p->data != key) {
prev = p;
p = p->next;
}
if (p == NULL) {
printf("%d not found in the list.\n", key);
return;
}
if (prev == NULL) {
*head = p->next;
}
else {
prev->next = p->next;
}
printf("Deleted node with data = %d.\n", p->data);
free(p);
}
int main() {
struct node *head = NULL;
int n = 0, key = 0, data = 0, choice = 0;
while (1) {
printf("1. Create a list.\n");
printf("2. Display the list.\n");
printf("3. Search an element.\n");
printf("4. Insert at the beginning.\n");
printf("5. Insert at the end.\n");
printf("6. Insert after a node.\n");
printf("7. Delete from the beginning.\n");
printf("8. Delete from the end.\n");
printf("9. Delete a node.\n");
printf("10. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the number of nodes: ");
scanf("%d", &n);
head = create_list(n);
break;
case 2:
display(head);
break;
case 3:
printf("Enter the element to be searched: ");
scanf("%d", &key);
search(head, key);
break;
case 4:
printf("Enter the data to be inserted at the beginning: ");
scanf("%d", &data);
insert_beg(&head, data);
break;
case 5:
printf("Enter the data to be inserted at the end: ");
scanf("%d", &data);
insert_end(&head, data);
break;
case 6:
printf("Enter the data of the node after which you want to insert: ");
scanf("%d", &key);
printf("Enter the data to be inserted: ");
scanf("%d", &data);
insert_after(head, key, data);
break;
case 7:
delete_beg(&head);
break;
case 8:
delete_end(&head);
break;
case 9:
printf("Enter the data of the node to be deleted: ");
scanf("%d", &key);
delete_node(&head, key);
break;
case 10:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
```
希望这段代码可以帮到您!