分?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文章來源:http://www.zghlxwxcb.cn/news/detail-855563.html
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)!