四亚甲基二砜四胺


私信TA

用户名:uq_24663325205

访问量:9

签 名:

等  级
排  名 4793
经  验 1577
参赛次数 0
文章发表 1
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:

注意事项:记得每次输出后要换行

参考代码:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct str

{

    int data;

    struct str *next;

}Node;

typedef struct str * node;

node creat(int n)

{

    node head=(node)malloc(sizeof(Node));

    head->next=NULL;

    for(int i=n;i>0;--i){

        node tmp=(node)malloc(sizeof(Node));

        scanf("%d",&tmp->data);

        tmp->next=head->next;

        head->next=tmp;

    }

    return head;

}

void show(node l)

{

    l=l->next;

    node p=l;

    if(p==NULL){

        printf("Link list is empty\n");

        return ;

    }

    else{

        while(p){

            printf("%d ",p->data);

            p=p->next;

        }

        putchar('\n');

    }

}

void insert(int n,int m,node l)

{

    node p=l;

    while(--n){

        p=p->next;

        if(p==NULL){

            printf("insert fail\n");

            return ;

        }

    }

    node tmp=(node)malloc(sizeof(Node));

    tmp->data=m;

    tmp->next=p->next;

    p->next=tmp;

    printf("insert OK\n");

}


void del(int n,node l)

{

    node p=l;

    while(--n){

        p=p->next;

        if(p==NULL){

            printf("delete fail\n");

            return ;

        }

    }

    node q=p->next;

    p->next=q->next;

    q->next=NULL;

    free(q);

    printf("delete OK\n");

}

void get(int n,node l)

{

    node p=l->next;

    while(--n){

        p=p->next;

        if(p==NULL)

            break;

    }

    if(p==NULL){

        printf("get fail\n");

        return ;

    }

    printf("%d\n",p->data);

}

int main()

{

    int n;

    scanf("%d",&n);

    node w=creat(n);

    char arr[10];

    int a,b;

    int m;

    scanf("%d",&m);

    while(m--){

        scanf("%s",arr);

        if(!strcmp(arr,"show")){

            show(w);

        }

        else if(!strcmp(arr,"insert")){

            scanf("%d%d",&a,&b);

            insert(a,b,w);

        }

        else if(!strcmp(arr,"delete")){

            scanf("%d",&a);

            del(a,w);

        }

        else if(!strcmp(arr,"get")){

            scanf("%d",&a);

            get(a,w);

        }

    }

    return 0;

}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区