国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Remove the specified nodes in the linked list with dummy header

這篇具有很好參考價值的文章主要介紹了Remove the specified nodes in the linked list with dummy header。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

分?jǐn)?shù) 20

作者 伍建全

單位 重慶科技大學(xué)

Please create a function with the prototype void removeNode(List L, int key). This function deletes all nodes from the linked list L 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.

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 removeNode(List L, int key);

The parameter L is a pointer to the dummy header. This function deletes all nodes from the linked list L 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.

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 removeNode(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函數(shù)由題目提供,不需要在本題的答案中實現(xiàn)
List createList();
//Function show outputs the data field of each node in the linked list L.
// show函數(shù)由題目提供,不需要在本題的答案中實現(xiàn)
void show(List L);
// destroy函數(shù)由題目提供,不需要在本題的答案中實現(xiàn)
void destroy(List L);

int main(void)
{
    List L = createList();
    show(L);
    int key;
    scanf("%d", &key);
    removeNode(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行是一系列正整數(shù),輸入-1表示輸入結(jié)束。第2行有1個整數(shù),表示需要從鏈表中刪除的數(shù)。)

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 40  40 60 -1
40

Sample Output :

60 40 40 50 40 30 20 10 
60 50 30 20 10 

代碼長度限制

16 KB

時間限制

400 ms

內(nèi)存限制

64 MB

C程序如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-855563.html

// 定義一個函數(shù),用于從鏈表中刪除具有特定鍵值的節(jié)點  
void removeNode(List L, int key){  
    // 定義兩個指向節(jié)點的指針,p和q  
    node *p, *q;  
    // 將p指向鏈表的頭節(jié)點  
    p = L;  
    // 將q指向頭節(jié)點的下一個節(jié)點,即鏈表的第一個實際數(shù)據(jù)節(jié)點  
    q = p->next;  
    // 當(dāng)q不為NULL時,即鏈表中還有節(jié)點時,循環(huán)繼續(xù)  
    while(q != NULL){  
        // 如果q所指向的節(jié)點的數(shù)據(jù)等于要刪除的鍵值  
        if(q->data == key){  
            // 將p的next指針指向q的下一個節(jié)點,從而跳過q節(jié)點  
            p->next = q->next;  
            // 釋放q節(jié)點所占用的內(nèi)存  
            free(q);  
            // 將q重新指向p的下一個節(jié)點,繼續(xù)檢查下一個節(jié)點  
            q = p->next;  
        }  
        // 如果q所指向的節(jié)點的數(shù)據(jù)不等于要刪除的鍵值  
        else{  
            // 將p移動到q的位置,即p指向當(dāng)前檢查的節(jié)點  
            p = q;  
            // 將q移動到下一個節(jié)點,準(zhǔn)備檢查下一個節(jié)點  
            q = q->next;  
        }  
    }  
}

到了這里,關(guān)于Remove the specified nodes in the linked list with dummy header的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 算法: 移除單鏈表的倒數(shù)第N個節(jié)點 19. Remove Nth Node From End of List

    算法: 移除單鏈表的倒數(shù)第N個節(jié)點 19. Remove Nth Node From End of List

    Given the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: Example 2: Example 3: Constraints: The number of nodes in the list is sz. 1 = sz = 30 0 = Node.val = 100 1 = n = sz Follow up: Could you do this in one pass? 該方法采用了兩次遍歷鏈表的方法。首先,它計算鏈表的總長度,然后通

    2024年02月13日
    瀏覽(21)
  • Android:Namespace not specified. Please specify a namespace in the module‘s build.gradle file like

    問題: 接上文: 【解決方案記錄】Could not find com.android.tools.build:gradle:8.0. 在修改完對應(yīng)的文件之后,并將compileSdk 版本號改為33后, 重新導(dǎo)入gradle項目,彈出錯誤: 在多次修改項目目錄下的build.gradle文件無效后,我通過參考鏈接中的描述修改了app目錄下的build.gradle文件 在

    2024年02月10日
    瀏覽(17)
  • 導(dǎo)入module報錯Namespace not specified. Please specify a namespace in the module‘s build.gradle file like

    解決辦法:打開導(dǎo)入的module的build:gradle dependencies { 。。。 } 把 namespace \\\'com.example.XXX’和 applicationId \\\"com.example.XXX\\\"改成 被導(dǎo)入的project的 namespace \\\'com.example.XXX’和 applicationId “com.example.XXX” 一般也會報版本不對,也把 compileSdk 33 targetSdk 33 minSdk 16也改成 被導(dǎo)入的project的 同款 -

    2024年02月11日
    瀏覽(28)
  • 【redis已解決】Warning: no config file specified, using the default config. In order to specify a config

    【redis已解決】Warning: no config file specified, using the default config. In order to specify a config

    Warning: no config file specified, using the default config. In order to specify a config file use /redis-6.2/redis-server /path/to/redis.conf 點擊這個報錯 表示沒有指定配置文件,使用默認(rèn)配置。要指定配置文件,請使用redis-server /path/to/redis.conf 啟動成功 (如果你怕麻煩就使用批處理) redis-server.exe redis

    2024年02月16日
    瀏覽(20)
  • Linux服務(wù)器報錯解決The git executable must be specified in one of the following ways: - be included in

    在利用深度學(xué)習(xí)服務(wù)器,利用Xshell進(jìn)入端口,想要運行深度學(xué)習(xí)項目時碰到了以下錯誤: Traceback (most recent call last): ? File \\\"/opt/conda/envs/[yolov5_SE]/lib/python3.9/site-packages/git/__init__.py\\\", line 166, in module ? ? refresh() ? File \\\"/opt/conda/envs/[yolov5_SE]/lib/python3.9/site-packages/git/__init__.py\\\", line

    2024年02月02日
    瀏覽(28)
  • Leetcode 1367. Linked List in Binary Tree (二叉樹好題)

    Linked List in Binary Tree Medium Given a binary tree root and a linked list with head as the first node. Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False. In this context downward path means a path that starts at some node and goes downwards. Exampl

    2024年01月25日
    瀏覽(22)
  • git pull Your configuration specifies to merge with the ref ‘refs/heads/xxxx‘ from the remote, but n

    Your configuration specifies to merge with the ref ‘refs/heads/xxxx’ from the remote, but no such ref was fetched.) 1.主要是因為網(wǎng)頁上倉庫fork別人的,更新了就沒了分支,但是本地還有 2.需要切換到丟失的分支下,需要先解鎖: 3.然后在執(zhí)行g(shù)it pull會有提示: 但是這樣問題還不能根本性的解決:

    2024年02月04日
    瀏覽(47)
  • 【mysql】[ERROR] --initialize specified but the data directory has files in it. Aborting.

    【mysql】[ERROR] --initialize specified but the data directory has files in it. Aborting.

    執(zhí)行 mysqld --initialize [ERROR] --initialize specified but the data directory has files in it. Aborting. ? [錯誤]--指定了初始化,但數(shù)據(jù)目錄中有文件。正在中止 清除掉 數(shù)據(jù)文件。 對應(yīng)的目錄 : /usr/local/mysql/data 查看現(xiàn)在目錄內(nèi)容: 把這些都刪除掉。 然后重新執(zhí)行: mysqld --initialize 可以正常執(zhí)

    2024年01月16日
    瀏覽(24)
  • MySQL啟動時出現(xiàn)initialize specified but the data directory has files in it. Aborting問題

    你是否遇見以下問題在MySQL重啟時出現(xiàn)問題,報出了以下的錯誤,現(xiàn)在我將告訴你遇見以下錯誤怎么處理。 當(dāng)出現(xiàn)了上面的錯誤,我們可以看出它提供了兩條命令“systemctl status mysqld.service”和“journalctl -xe”來幫助我們來查看錯誤的詳情。 使用“systemctl status mysqld.service”命

    2024年02月16日
    瀏覽(24)
  • The minCompileSdk (33) specified in a dependency‘s AAR metadata (META-INF/com/android/build/gradle/a

    The minCompileSdk (33) specified in a dependency‘s AAR metadata (META-INF/com/android/build/gradle/a

    android studio 編譯的報錯提示: 報錯信息中很關(guān)鍵的三個: 先看一下第一個報錯信息: 意思是 CompileSdk 最小必須是 33 ,并且 recyclerview版本1.3.0-beta02 ,為什么和我的 CompileSdk 29 和 recyclerview版本1.1.0 不一樣呢? 我的 recyclerview版本1.1.0 怎么查看:打開看看布局中 androidx.recycler

    2024年02月16日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包