最后两个tle是malloc的原因吗
查看原帖
最后两个tle是malloc的原因吗
147400
corgilld楼主2023/12/6 10:40
#include<stdio.h>
#include<malloc.h>
struct cell
{
    int x;
    struct cell *next;
};
void cr(struct cell *head,int y,int z)
{
    struct cell* p,*tmp;
    tmp=NULL;
    p=head;
    while(p->x!=y)
        p=p->next;
    tmp=(struct cell*)malloc(sizeof(struct cell));
    tmp->x=z;
    tmp->next=p->next;
    p->next=tmp;
}
void ask(struct cell* head,int y)
{
    struct cell* p;
    p=head;
    while(p->x!=y)
        p=p->next;
        if(p->next!=NULL)
            printf("%d\n",p->next->x);
        else printf("0");
}
void del(struct cell* head,int y)
{
    struct cell* p,*p0;
    p=head;
    while(p->x!=y)
    {
        p0=p;
        p=p->next;
    }
    p0=p;
    p=p->next;
    p=p->next;
    p0->next=p;
}
int main()
{
    struct cell *head;
    head=(struct cell*)malloc(sizeof(struct cell));
    head->x=1;
    head->next=NULL;
    int n,m,y,z;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&m);
        if(m==1)
        {
            scanf("%d %d",&y,&z);
            cr(head,y,z);
        }
        else if(m==2)
        {
            scanf("%d",&y);
            ask(head,y);
        }
        else if(m==3)
        {
            scanf("%d",&z);
            del(head,z);
        }
    }
}
2023/12/6 10:40
加载中...