数据结构双向链表按位置删除
时间: 2023-09-04 10:10:55 浏览: 72
双向链表的删除操作需要分为两种情况,即删除头节点和删除非头节点。
1. 删除头节点:
首先需要判断链表是否为空,若为空,则无法进行删除操作;否则,将头节点指向下一个节点,同时将新头节点的前驱节点指针置为空。
```
void deleteNodeAtPos(Node **head, int pos)
{
if (*head == NULL) {
printf("Empty list\n");
return;
}
Node *temp = *head;
// 删除头节点
if (pos == 1) {
*head = temp->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
return;
}
```
2. 删除非头节点:
需要遍历链表找到指定位置的节点,然后修改它前驱节点的后继节点指针和它后继节点的前驱节点指针。
```
// 删除非头节点
int i;
for (i = 1; temp != NULL && i < pos; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Invalid position\n");
return;
}
temp->prev->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
}
```
完整的代码如下:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *prev;
struct node *next;
} Node;
void insertAtEnd(Node **head, int data)
{
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
Node *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
void printList(Node *head)
{
if (head == NULL) {
printf("Empty list\n");
return;
}
Node *temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void deleteNodeAtPos(Node **head, int pos)
{
if (*head == NULL) {
printf("Empty list\n");
return;
}
Node *temp = *head;
// 删除头节点
if (pos == 1) {
*head = temp->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
return;
}
// 删除非头节点
int i;
for (i = 1; temp != NULL && i < pos; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Invalid position\n");
return;
}
temp->prev->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
}
int main()
{
Node *head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
printf("Original list: ");
printList(head);
deleteNodeAtPos(&head, 1);
printf("List after deleting node at position 1: ");
printList(head);
deleteNodeAtPos(&head, 3);
printf("List after deleting node at position 3: ");
printList(head);
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)