题解 | #KiKi学结构体和指针#
KiKi学结构体和指针
https://www.nowcoder.com/practice/0ab593ca56b1476eb05b1ff848fd7fcc
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<malloc.h> typedef struct SlistNode { int data; struct SlistNode* next; }SL; int main() { int n = 0; scanf("%d", &n); SL* phead = (SL*)malloc(sizeof(SL) * 1); phead->next = NULL; SL* p = phead; int cnt = n; while (cnt--) { scanf("%d", &(p->data)); if (cnt != 0) { SL* newNode = (SL*)malloc(sizeof(SL) * 1); if (newNode != NULL) { newNode->next = NULL; p->next = newNode; } p = p->next; } else p->next = NULL; } int k = 0; scanf("%d", &k); p = phead; while (p != NULL) { if (p->data == k) { p->data = -1; n--; } p=p->next; } printf("%d \n", n); p = phead; while (p!=NULL)/////// { if(p->data!=-1) printf("%d ", p->data); p = p->next; } while (phead != NULL) { SL* pp = phead->next; free(phead); phead = pp; } return 0; }