Delete the specified node in the linked list with dummy header

分数 20

作者 伍建全

单位 重庆科技大学

Please create a function with the prototype void deleteNode(List L, int key) . This function should delete the first node from the linked list L with dummy header, where the data field is equal to key . If there are no nodes in the list where the data field is equal to key , the function should do nothing. If there are multiple nodes in the list where the data field is equal to key, only the first one should be deleted.

Structure description:

The node structure is shown below:

typedef struct ListNode {
    int data;
    struct ListNode *next;
} node;

typedef node* position;
typedef position List;

Function definition:

void deleteNode(List L, int key);

The parameter L is a pointer to the dummy header. This function should delete the first node from the linked list L with dummy header, where the data field is equal to key . If there are no nodes in the list where the data field is equal to key , the function should do nothing. If there are multiple nodes in the list where the data field is equal to key, only the first one should be deleted.

Test program example:

#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode {
    int data;
    struct ListNode *next;
}node;

typedef node* position;
typedef position List;

void deleteNode(List L, int key);

// The questioner has implemented the createList function.
// This function reads a series of positive integers separated by spaces
// and inserts them into a linked list using the head insertion method.
// Entering -1 indicates the end of input.
// creatgeList函数由题目提供,不需要在本题的答案中实现
List createList();
//Function show outputs the data field of each node in the linked list L.
// show函数由题目提供,不需要在本题的答案中实现
void show(List L);
// destroy函数由题目提供,不需要在本题的答案中实现
void destroy(List L);

int main(void)
{
    List L = createList();
    show(L);
    int key;
    scanf("%d", &key);
    deleteNode(L, key);
    show(L);
    destroy(L);
    return 0;
}

Input Specification:

There are two lines of input. The first line is a series of positive integers, and entering -1 indicates the end of the input. The second line contains one integer, which represents the number that needs to be deleted from the linked list.(输入有两行。第1行是一系列正整数,输入-1表示输入结束。第2行有1个整数,表示需要从链表中删除的数。)

Output Specification:

There are two lines of output. The first line is the linked list before the element is deleted; the second line is the linked list after the deletion.

Sample Input :

10 20 30 40 50 -1
40

Sample Output :

50 40 30 20 10 
50 30 20 10 

参考答案

编辑

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

C程序如下:

cs 复制代码
void deleteNode(List L, int key){  
    // 定义两个指向节点的指针p和q  
    node* p;  
    node* q;  
  
    // 将p指针指向链表的头节点  
    p = L;  
  
    // 将q指针指向头节点的下一个节点,即链表的第一个数据节点  
    q = p->next;  
  
    // 遍历链表,直到到达链表的末尾  
    while(p->next != NULL){  
        // 检查当前q指针所指向的节点的数据域是否等于key  
        if(q->data == key){  
            // 如果找到值为key的节点,则修改p指针的next指向q的下一个节点  
            // 这样相当于跳过了q指向的节点,实现了删除操作  
            p->next = q->next;  
  
            // 释放q指向的节点的内存  
            free(q);  
  
            // 删除成功,退出函数  
            return;  
        }  
  
        // 将p指针移动到q指向的节点,以便下一次迭代时检查q的下一个节点  
        p = q;  
  
        // 将q指针移动到它的下一个节点,继续遍历链表  
        q = q->next;  
    }  
  
}
相关推荐
ChoSeitaku9 分钟前
链表循环及差集相关算法题|判断循环双链表是否对称|两循环单链表合并成循环链表|使双向循环链表有序|单循环链表改双向循环链表|两链表的差集(C)
c语言·算法·链表
DdddJMs__13514 分钟前
C语言 | Leetcode C语言题解之第557题反转字符串中的单词III
c语言·leetcode·题解
Fuxiao___18 分钟前
不使用递归的决策树生成算法
算法
我爱工作&工作love我23 分钟前
1435:【例题3】曲线 一本通 代替三分
c++·算法
娃娃丢没有坏心思1 小时前
C++20 概念与约束(2)—— 初识概念与约束
c语言·c++·现代c++
白-胖-子1 小时前
【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-统计数字
开发语言·c++·算法·蓝桥杯·等考·13级
workflower1 小时前
数据结构练习题和答案
数据结构·算法·链表·线性回归
好睡凯1 小时前
c++写一个死锁并且自己解锁
开发语言·c++·算法
Sunyanhui11 小时前
力扣 二叉树的直径-543
算法·leetcode·职场和发展
一个不喜欢and不会代码的码农1 小时前
力扣105:从先序和中序序列构造二叉树
数据结构·算法·leetcode