题解 | #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;
}

全部评论

相关推荐

点赞 评论 收藏
分享
牛客389580366号:读书的意义在于提升自己和他人吧,“阶级意识”是读书过程中的产出,“跨越阶级”是通过读书获得的能力
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务