线性表的链式存储结构与操作的C++题目

这个题目,可以吧线性单链表的结构的功能循环,循环的功能有查找、获取、删除、长度、遍历。

让我们一起看看题目吧。

样例输入、输出:

这个用不了有参构造函数,那我就可忽略咯。

代码如下:

#include<iostream>
using namespace std;
struct Node{
    int data;
    Node *next;
};
class LinkList{
    public:
    LinkList(){
        first = new Node;
        first->next = NULL;
    }
    void Insert(int i,int x){
        Node *s = NULL;
        Node *p = first;
        int count = 0;
        while(p!=NULL&&count<i-1){
            p = p->next;
            count++; 
        }
        if(p == NULL){
            cout<<"位置不正确"<<endl;
            return ; 
        }
        else{
            s = new Node;
            s->data = x;
            s->next = p->next;
            p->next = s;
        }
    }
    void Search(int x){
        Node *p =first;
        int count = 0; 
        while(p != NULL ){
            if(p->data == x){
                cout<<count<<endl;
                return ;
            }
            count++;
            p = p->next; 
        }
        cout<<"None"<<endl;
    }
    void Get(int i){
        Node *p = first;
        int count = 0;
        while(p != NULL&& count < i){
            p = p->next;
            count++;
        }
        if(p == NULL){
            cout<<"位置不正确"<<endl;
            return ;
        }
        cout<<p->data<<endl; 
    }
    void Delete(int i){
        Node *p = first;
        int count = 0;
        Node *s = NULL;
        if(p == NULL){
            cout<<"表空"<<endl;
            return ;
        }
        while(p != NULL&& count < i-1){
            p = p->next;
            count++;
        }
        if(p == NULL){
            cout<<"位置不正确"<<endl;
            return ; 
        }
        s = p->next;
        cout<<s->data<<endl;
        p->next= s->next;
        delete s;
    } 
    void Length(){
        Node *p = first->next;
        int count = 0;
        while(p != NULL){
            count++;
            p = p->next;
        }
        cout<<count<<endl;
    }
    void PrintList(){
        Node *p = first->next;
        while(p != NULL){
            cout<<p->data<<endl; 
            p = p->next;
        }
    }
    private:
    Node *first; 
};
​
int main(){
    LinkList list;
    char m;
    cin>>m;
    int n;
    int x,y;
 
    while(1){
        if(m=='E'){
            break; 
        }
        if(m=='I'){
            cin>>n;
            for(int i = 0;i < n;i ++){
                cin>>x;
                cin>>y;
                list.Insert(x,y);
            };
        }
        if(m=='S'){
            cin>>n;
            list.Search(n);
        }
        if(m=='G'){
            cin>>n;
            list.Get(n);
        }
        if(m=='D'){
            cin>>n;
             list.Delete(n);        
        }
        if(m=='L'){
        list.Length();
        }
        if(m=='V'){
            list.PrintList();
        }
        cin>>m;
    }
    return 0;
}

最后的运行如下,输出的地方都用红框标记咯

这次分享到此就拜拜啦!!!

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值