335. 单向链表中的节点删除

Naive普通指针结构体单链表

时间限制:2000 ms

内存限制:256 MiB

题面

给定一个存储了若干个整数(个数范围:0 - 1000)的单向链表和一个整数 v,删除单向链表中所有值为 v 的节点。 【请提交以下的整个函数,不要提交其它部分】

NODE *EraseNodes(NODE *h,int v) {
    //TODO: your definition
}

输入格式

第 1 行:一个整数 TT (1T101 \le T \le 10) 为问题数。

对于每个问题,有 3 行数据:第 1 行是一个整数 n 表示要在款表中存储数的个数,第 2 行是 n 个整数,第 3 行是 v。

输出格式

对于每个问题,输出一行问题的编号(0 开始编号,格式:case #0: 等)。

然后对应每个问题在一行中输出经过删除操作后的数。

样例

输入

3
1
1
1
2
3 4
4
5
12 12 18 12 12
12

输出

case #0:

case #1:
3
case #2:
18

提示

程序:

// ********** Specification of struct Node **********
typedef struct Node {
    int value;
    struct Node *next;
} NODE;
/*/////////////////////////////////////////////////////*/
NODE *EraseNodes(NODE *h,int v) {
    //TODO: your definition
    //请提交该函数,不要提交其它部分
}
/*/////////////////////////////////////////////////////*/
/***************************************************************/
/*                                                             */
/*  DON'T MODIFY below code anyway!                            */
/*                                                             */
/***************************************************************/
#include <stdio.h>
#include <malloc.h>
//********** Specification of EraseNodes(head,v) **********
NODE *EraseNodes(NODE *h,int v);
/* PreCondition:
h is a head pointer of a linked-list, v is an integer
PostCondition:
return the head pointer of a linked-list that has not v in its nodes
*/
void solve() {
    int i,n,v;
    NODE* head=0,*p,*tail;
    scanf("%d",&n);
    for (i=0; i<n; i++) {
        p=(NODE*)malloc(sizeof(NODE));
        scanf("%d",&p->value);
        p->next=0;
        if (head==0)
            head=tail=p;
        else {
            tail->next=p;
            tail=p;
        }
    }
    scanf("%d",&v);
//********** EraseNodes is called here **********
    head=EraseNodes(head,v);
//****************************************
    while (head) {
        p=head;
        head=head->next;
        printf("%d",p->value);
        if (head)
            printf(" ");
        free(p);
    }
    printf("\n");
}
int main() {
    int i,t;
    scanf("%d",&t);
    for (i=0; i<t; i++) {
        printf("case #%d:\n",i);
        solve();
    }
    return 0;
}