Description

Please write a program that first creates a link list containing a string, then asks the user to input a string s, deletes all nodes containing s in the link list, and prints the filtered results.

請撰寫一個程式,先建立一個包含字串的鏈結串列,接著讓使用者輸入一個字串 s ,把鏈結串列內包含 s 的節點都刪除,並印出篩除後的結果。

For example: abc->abb->bbc->cab->bca, s = "bc", the result after deletion is abb->cab.

例如:abc->abb->bbc->cab->bca,s = "bc",刪除後的結果為 abb->cab。

Input

The first line contains multiple 字串 separated by spaces, terminated by 0, indicating the initial state of the linked list.

第一行輸入包含多個字串,以 0 作為結束,代表一開始 Linked List 的狀態。

From the second line, there are multiple lines of input. Each line contains a string "s", representing the string to be found and deleted. Input continues until EOF.

第二行開始有多行輸入,每行有一個字串 s,代表要尋找並刪除的字串。直到 EOF 為止。

All the mentioned strings have a length not exceeding 10 characters, and they only contain lowercase letters.

以上所有字串的長度都不超過 10 個字元,且只包含小寫字母。

Output

Please print the result after each deletion, regardless of whether a node has been deleted or not. If list is empty, please print "Empty".

不論有沒有刪除到節點,都請印出每次刪除後的結果。如果串列為空,則印出 Empty。

Sample Input 1

abc abb bbc cab bca 0
bc
def
bb
a

Sample Output 1

abb->cab
abb->cab
cab
Empty

Hint

複習:strstr(char* s, char* t):回傳 t 中找到s 位置的指標,如果沒找到則回傳NULL

strcmp(char* s, char* t):如果 s < t 回傳負值,s > t 回傳正值,s = t 回傳 0

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

typedef struct node {
    char data[11]; // Maximum string length is 10 characters
    struct node* next;
} Node;

// Function to remove nodes containing the specified substring
Node* RemoveNodesWithSubstring(Node* head, char* s) {
    if (head == NULL)
        return NULL;

    Node* current = head;
    Node* prev = NULL;

    while (current != NULL) {
        // Check if the data contains the substring
        if (strstr(current->data, s) != NULL) {
            // If it contains, remove the node
            Node* temp = current;
            if (prev == NULL) {
                head = current->next;
            } else {
                prev->next = current->next;
            }
            current = current->next;
            free(temp);
        } else {
            // Move to the next node
            prev = current;
            current = current->next;
        }
    }

    return head;
}

// Function to print the linked list
void PrintList(Node* head) {
    if (head == NULL) {
        printf("Empty\\n");
        return;
    }
    
    while (head != NULL) {
        printf("%s", head->data);
        if (head->next != NULL) {
            printf("->");
        }
        head = head->next;
    }
    printf("\\n");
}

int main() {
    Node* head = NULL;
    char str[11];

    // Creating the linked list from input
    while (scanf("%s", str) != EOF) {
        if (strcmp(str, "0") == 0)
            break;
        Node* new_node = (Node*)malloc(sizeof(Node));
        strcpy(new_node->data, str);
        new_node->next = NULL;
        if (head == NULL)
            head = new_node;
        else {
            Node* current = head;
            while (current->next != NULL) {
                current = current->next;
            }
            current->next = new_node;
        }
    }

    // Inputting strings to search and delete from the list
    while (scanf("%s", str) != EOF) {
        head = RemoveNodesWithSubstring(head, str);
        PrintList(head);
    }

    // Freeing allocated memory
    Node* temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }

    return 0;
}