解题思路:

注意事项:

参考代码:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>


typedef struct node

{

    int num;

    struct node *next;

}listnode;


listnode *creatnode(int amount,int *q);

void shownode(listnode *head);

void getnode(listnode *head);

void deletenode(listnode *head);

void insertnode(listnode *head);

void freenode(listnode *head);


int main()

{

int n,m,*p;

scanf("%d",&n);

p=(int *)malloc(sizeof(int)*n);

for(int i=0;i<n;i++)

{

    scanf("%d",&p[n-1-i]);

}

listnode *firstnode=creatnode(n,p);

free(p);

scanf("%d",&m);

while(m--)

{

    char str[10]={0};

    scanf("%s",str);

    

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

    {

        shownode(firstnode);

    }

    else if(!strcmp(str,"get"))

    {

        getnode(firstnode);

    }

    else if(!strcmp(str,"delete"))

    {

        deletenode(firstnode);

    }

    else if(!strcmp(str,"insert"))

    {

        insertnode(firstnode);

    }

    else

    {

        continue;

    }

}

freenode(firstnode);

return 0;

}


listnode *creatnode(int amount,int *q)

{

    listnode *head,*middle,*temp;

    head=(listnode *)malloc(sizeof(listnode));

    if(head==NULL)

    {

        perror("malloc");

        exit(0);

    }

    head->next=NULL;

    temp=head;

    for(int i=0;i<amount;i++)

    {

        middle=(listnode *)malloc(sizeof(listnode));

        if(middle==NULL)

        {

            perror("malloc");

            exit(0);

        }

        middle->num=q[i];

        temp->next=middle;

        temp=middle;

    }

    temp->next=NULL;

    return head;

}


void shownode(listnode *head)

{

    listnode *temp=head;

    listnode *i;

    for(i=temp;i->next!=NULL;i=i->next)

    {

        printf("%d ",i->next->num);

    }

    if(i==temp)

    {

        printf("Link list is empty");

    }

    printf("\n");

}


void getnode(listnode *head)

{

    int sz,count=0;

    listnode *temp=head;

    scanf("%d",&sz);

    for(listnode *i=temp;i->next!=NULL;i=i->next)

    {

        count++;

        if(count==sz)

        {

            printf("%d",i->next->num);

        }

    }

    if(sz<1 || count<sz)

    {

        printf("get fail");

    }

    printf("\n");

}


void deletenode(listnode *head)

{

    listnode *temp=head;

    int sz,count=0;

    int value=count;

    scanf("%d",&sz);

    for(listnode *i=temp;i->next!=NULL;i=i->next)

    {

        value++;

    }

    for(listnode *i=temp;i->next!=NULL;i=i->next)

    {

        count++;

        if(count==sz)

        {

            listnode *temp1=i->next;

            i->next=temp1->next;

            free(temp1);

            printf("delete OK");

            break;

        }

    }

    if(sz<1 || value<sz)

    {

        printf("delete fail");

    }

    printf("\n");

}



void insertnode(listnode *head)

{

    listnode *temp=head;

    int a,e,count=0;

    int value=count;

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

    for(listnode *i=temp;i->next!=NULL;i=i->next)

    {

        value++;

    }

    if(a>=1 && (a-value)<=1)  //插入位置的输入数据需满足此条件

    {

        if(value!=0)  //链表不仅只有头结点的情况

        {

            if(a>1)  //两个普通节点之间插入的情况

            {

                for(listnode *j=temp;j->next!=NULL;j=j->next)

                {

                    count++;

                    if(count==(a-1))

                    {

                        listnode *temp1=j->next;

                        listnode *middle;

                        middle=(listnode *)malloc(sizeof(listnode));

                        middle->num=e;

                        middle->next=temp1->next;

                        temp1->next=middle;

                        printf("insert OK");

                        break;

                    }

                }

            }

            else  //头结点与第一个普通节点之间插入的情况

            {

                listnode *middle;

                middle=(listnode *)malloc(sizeof(listnode));

                middle->num=e;

                middle->next=temp->next;

                temp->next=middle;

                printf("insert OK");

            }

        }

        else  //链表只存在头结点的情况

        {

            listnode *middle;

            middle=(listnode *)malloc(sizeof(listnode));

            middle->num=e;

            temp->next=middle;

            printf("insert OK");

        }

    }

    else //输入数据不满足条件时

    {

        printf("insert fail");

    }

    printf("\n");

}



void freenode(listnode *head)

{

    listnode *curr=head;

    while(curr)

    {

        listnode *temp=curr->next;

        free(curr);

        curr=temp;

    }

}


点赞(0)
 

0.0分

0 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论