解题思路:

注意事项:

参考代码:

#include<bits/stdc++.h>

using namespace std;

typedef struct Node{

    int data;

    struct Node *next;

}node; 

void creat(node *head,int n){//尾 

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

    node *tail=(node *)malloc(sizeof(node));

    tail->next=head->next;

    cin>>tail->data;

    head->next=tail;

    }

}

void print(node *head){

    if(head->next==NULL){

    cout<<"Link list is empty"<<endl;

    return ;

    }

    node *p=(node *)malloc(sizeof(node));

    p=head->next;

    while(p){

    cout<<p->data<<' ';

    p=p->next;

    }

    cout<<endl;

}


void get(node *head,int n){

    node *p=(node *)malloc(sizeof(node));

    p=head;

    while(p&&n){

    n--;

    p=p->next;

    }

    if(n>0){

    cout<<"get fail"<<endl;

    return ;

    }

    cout<<p->data<<endl;

}


void insert(node *head,int n,int data){

    node *p=(node *)malloc(sizeof(node));

    p=head,n--;

    while(p->next&&n){

    n--;

    p=p->next;

    }

    if(n>0){

    cout<<"insert fail"<<endl;

    return ;

    }

    node *q=(node *)malloc(sizeof(node));

    q->data=data;

    q->next=p->next;

    p->next=q;

    cout<<"insert OK"<<endl;

}


void Delete(node *head,int n){

    if(head->next==NULL){

    cout<<"delete fail"<<endl;

    return ;

    }

    node *p=(node *)malloc(sizeof(node));

    p=head,n--;

    while(p->next&&n){

    n--;

    p=p->next;

    }

    if(n>0){

    cout<<"delete fail"<<endl;

    return ;

    }

    p->next=p->next->next;

    cout<<"delete OK"<<endl;

}


int main(){

    node *head=NULL;

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

    head->next=NULL;

    int n,b,c,d;

    cin>>n; 

    creat(head,n);

    string a;

    cin>>n;

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

    cin>>a;

    if(a=="show")

    print(head); 

    else if(a=="delete"){

    cin>>c;

    Delete(head,c);

    }

    else if(a=="get"){

    cin>>c;

    get(head,c);

    }

    else if(a=="insert"){

    cin>>c>>d;

    insert(head,c,d);

    }

}

    return 0;

}


点赞(0)
 

0.0分

0 人评分

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

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

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

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

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

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

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

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

评论列表 共有 1 条评论

陈冠希 2年前 回复TA
用==判断花费时间大,建议用strcmp