Từ Ngôn ngữ lập trình C-2-2-24(N11), Ngôn ngữ lập trình C-2-3-24(N01), Cấu trúc dữ liệu và thuật toán-2-2-25(N04.TH1)
Thông tin
include <stdio.h>
include <stdlib.h>
typedef struct Node { long long data; struct Node *next; } Node;
void themDau(Node **head, long long value) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = value; newNode->next = *head; *head = newNode; }
// kiểm tra x có nằm trong mảng b không int soTruoc(long long *b, int m, long long x) { for (int i = 0; i < m; i++) { if (b[i] == x) return 1; } return 0; }
// xoá phần tử LIỀN SAU node có giá trị thuộc mảng b void xoaGiuaLienSau(Node **head, long long *b, int m) { Node *cur = *head;
while (cur != NULL && cur->next != NULL) {
if (soTruoc(b, m, cur->data)) {
Node *tmp = cur->next;
cur->next = tmp->next;
free(tmp);
} else {
cur = cur->next;
}
}
}
void IN(Node *head) { while (head != NULL) { printf("%lld ", head->data); head = head->next; } }
int main() { int n, m; scanf("%d %d", &n, &m);
Node *head = NULL;
long long x;
// thêm n phần tử vào đầu
for (int i = 0; i < n; i++) {
scanf("%lld", &x);
themDau(&head, x);
}
// đọc m giá trị b
long long b[1005];
for (int i = 0; i < m; i++) {
scanf("%lld", &b[i]);
}
// xoá giữa
xoaGiuaLienSau(&head, b, m);
// in kết quả
IN(head);
return 0;
}