私信TA

用户名:uq_60654334766

访问量:299

签 名:

等  级
排  名 623
经  验 4022
参赛次数 0
文章发表 6
年  龄 0
在职情况 在职
学  校
专  业

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


typedef struct _fushu{

int value;

struct _fushu *next;

}fushu; 


void add(fushu **phead);

void print(fushu **phead);

void del_one(fushu **phead,int x,int *count);

void get_one(fushu **phead,int x);

void insert_one(fushu **phead,int x,int y,int *count);


int main()

{

         fushu *head = NULL;//核心表头 

        

         int n;

         scanf("%d",&n);

         int count = n;

         while(n--)

         add(&head);//术式反转 

        

         int b;

         scanf("%d",&b);

         char cmd[7];

         while(b--)

         { 

              scanf("%s",cmd);

             if(!strcmp(cmd,"show"))

             {

                  print(&head);

             }

             if(!strcmp(cmd,"delete"))

             {

                 int where;

                 scanf("%d",&where);

                 if(where>count || where<1)

                 printf("delete fail\n");

                 else

                 del_one(&head,where,&count);

             }

             if(!strcmp(cmd,"get"))

             {

                 int where;

                 scanf("%d",&where);

                 if(where>count || where<1)

                 printf("get fail\n");

                 else 

                 get_one(&head,where);

             }

             if(!strcmp(cmd,"insert"))

             {

                 int where,how;

                 scanf("%d %d",&where,&how);

                 if((where>count || where<1) && !(count==0&&where==1))

                 printf("insert fail\n");

                 else

                 insert_one(&head,where,how,&count);

             }

         }

         return 0;

}


void add(fushu **phead)

{

     int what;

     scanf("%d",&what);

     fushu *a = (fushu*)malloc(1*sizeof(fushu));

     a->value = what;

     a->next = NULL;

     if(!*phead)  

     *phead = a;

     else

     {

         a->next = *phead;

         *phead = a;

     }

}


void print(fushu **phead)

{

     if(!*phead) printf("Link list is empty");

     else

     {

         fushu *p = *phead;

         for(;p;p=p->next)

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

     }

     printf("\n");

}


void del_one(fushu **phead,int x,int *count)

{

     fushu *p = *phead;

     if(x==1)

     {

         *phead = p->next;

         free(p);

     }

     else

     {

         for(int i=1;i<x-1;i++)

         p=p->next;

         fushu *pp = p->next;

         p->next = pp->next;

         free(pp);

     }

     printf("delete OK\n");

     (*count)--;

}


void get_one(fushu **phead,int x)

{

     fushu *p = *phead;

     for(int i=1;i<x;i++)

     p=p->next;

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

}

void insert_one(fushu **phead,int x,int y,int *count)

{

     fushu *a = (fushu*)malloc(1*sizeof(fushu));

     a->value = y;

     a->next = NULL;

     fushu *p = *phead;

     if(!p) *phead = a;

     else

    {

         if(x==1) 

         {

             a->next = *phead;

             *phead = a;

         }

         else

         {

             for(int i=1;i<x-1;i++)

             p=p->next;

             a->next = p->next;

             p->next = a;

         }

     }

     printf("insert OK\n");

     (*count)++;

}


 

0.0分

0 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区