• c++ 链表类创建 demo


    1_student_link_list.cpp

    #include<iostream>
    using namespace std;
    
    struct student{
        int id;string name;int age;
        student()=default;
        student(int i,string n,int a)
        :id(i),name(n),age(a)
        {}
        student(student &s){
            id=s.id;name=s.name;age=s.age;
        }
        ~student(){}
        student * next=nullptr;
    };
    
    student * head=nullptr;
    
    int create_stu_list()
    {
        student* p=nullptr;
        char ch;
        while(1)
        {
            p = new student();
            cin>>p->id>>p->name>>p->age;
    
            p->next = head;
            head = p;
    
            cout<<"是否继续?[Y/n]";
            cin>>ch;
            if(ch == 'n' || ch== 'N')
                break;
        }
        return 0;
    }
    
    int show_stu_list()
    {
        student *p = head;
        while(p)
        {
            cout<<p->id<<" "<<p->name<<" "<<p->age<<endl;
            p = p->next;
        }
        return 0;
    }
    
    int main()
    {
        create_stu_list();
        show_stu_list();
    }
    

    2_student_list_class.cpp

    #include<iostream>
    using namespace std;
    
    struct student{
        int id;string name;int age;
        student()=default;
        student(int i,string n,int a)
        :id(i),name(n),age(a)
        {}
        student(student &s){
            id=s.id;name=s.name;age=s.age;
        }
        ~student(){}
        student * next=nullptr;
    };
    
    struct stu_list{
        private:
        student * head=nullptr;
        public:
        int create_stu_list()
        {
            student* p=nullptr;
            char ch;
            while(1)
            {
                p = new student();
                cin>>p->id>>p->name>>p->age;
                if(cin.fail())break;
    
                p->next = head;
                head = p;
    
    //          cout<<"是否继续?[Y/n]";
    //          cin>>ch;
    //          if(ch == 'n' || ch== 'N')
    //              break;
            }
            return 0;
        }
    
        int show_stu_list()
        {
        student *p = head;
        while(p)
        {
            cout<<p->id<<" "<<p->name<<" "<<p->age<<endl;
            p = p->next;
        }
        return 0;
    }
    
    };
    
    
    int main()
    {
        stu_list sl;
        sl.create_stu_list();
        sl.show_stu_list();
    
    //  stu_list sl1;
    //  sl1.create_stu_list();
    //  sl1.show_stu_list();
    
    //  create_stu_list();
    //  show_stu_list();
    }

    3_work.cpp

    #include<iostream>
    using namespace std;
    
    struct student{
        int id;string name;int age;
        student()=default;
        student(int i,string n,int a)
        :id(i),name(n),age(a)
        {}
        student(student &s){
            id=s.id;name=s.name;age=s.age;
        }
        ~student(){}
        student * next=nullptr;
    };
    
    struct stu_list{
        private:
        student * head=nullptr;
        public:
        int create_stu_list()
        {
            student* p=nullptr;
            char ch;
            while(1)
            {
                p = new student();
                cin>>p->id>>p->name>>p->age;
                if(cin.fail())break;
    
                p->next = head;
                head = p;
    
    //          cout<<"是否继续?[Y/n]";
    //          cin>>ch;
    //          if(ch == 'n' || ch== 'N')
    //              break;
            }
            return 0;
        }
    
        int show_stu_list()
        {
        student *p = head;
        while(p)
        {
            cout<<p->id<<" "<<p->name<<" "<<p->age<<endl;
            p = p->next;
        }
        return 0;
    }
    //return value: 0/OK,-1/failed;
    int insert(student &s)
    {
        student *p =new student(s);
        if(p == nullptr){
            perror("new p");
            return -1;
        }
        p->next = head;
        head = p;
        return 0;
    }
    int remove(int id)
    {
        student* p=head,*q=head;
        while(p)
        {
            if(p->id == id)
                break;
            q =p;
            p=p->next;
        }
        if(p){
            if(p == head){
                head = head->next;
            }else
                q->next = p->next;
            delete p;
            return 0;
        }else
            return -1;  
    }
    };
    
    
    int main()
    {
        stu_list sl;
        student s(1005,"zhaoqi",20);
        sl.create_stu_list();
        sl.insert(s);
        sl.show_stu_list();
        cout<<"delete 1003:--------------"<<endl;
        sl.remove(1005);
        sl.remove(1004);
        sl.show_stu_list();
    
    //  stu_list sl1;
    //  sl1.create_stu_list();
    //  sl1.show_stu_list();
    
    //  create_stu_list();
    //  show_stu_list();
    }
    

    4_work.cpp

    #include<iostream>
    using namespace std;
    
    struct student{
        int id;string name;int age;
        student()=default;
        student(int i,string n,int a)
        :id(i),name(n),age(a)
        {}
        student(student &s){
            id=s.id;name=s.name;age=s.age;
        }
        ~student(){}
        student * next=nullptr;
    };
    
    struct stu_list{
        private:
        student * head=nullptr;
        public:
        int create_stu_list()
        {
            student* p=nullptr;
            char ch;
            while(1)
            {
                p = new student();
                cin>>p->id>>p->name>>p->age;
                if(cin.fail())break;
    
                p->next = head;
                head = p;
    
    //          cout<<"是否继续?[Y/n]";
    //          cin>>ch;
    //          if(ch == 'n' || ch== 'N')
    //              break;
            }
            return 0;
        }
    
        int show_stu_list()
        {
        student *p = head;
        while(p)
        {
            cout<<p->id<<" "<<p->name<<" "<<p->age<<endl;
            p = p->next;
        }
        return 0;
    }
    //return value: 0/OK,-1/failed;
    int insert(student &s)
    {
        student *p =new student(s);
        if(p == nullptr){
            perror("new p");
            return -1;
        }
        p->next = head;
        head = p;
        return 0;
    }
    int remove(int id)
    {
        student* p=head,*q=head;
        while(p)
        {
            if(p->id == id)
                break;
            q =p;
            p=p->next;
        }
        if(p){
            if(p == head){
                head = head->next;
            }else
                q->next = p->next;
            delete p;
            return 0;
        }else
            return -1;  
    }
    
    int reserve_stu_list()
    {
        student * new_head=nullptr,*p;
        if(head == nullptr)return -1;
        while(head)
        {
            p = head;
            head=head->next;
    
            p->next = new_head;
            new_head = p;
        }
        head = new_head;
        return 0;
    }
    
    int release_stu_list()
    {
        student *p=head,*q=head;
        while(p)
        {
            q = p->next;
            delete p;
            p = q;
        }
        head= nullptr;
    }
    
    };
    
    
    int main()
    {
        stu_list sl;
        sl.create_stu_list();
    //  cout<<"reserve list:-----------"<<endl;
    //  sl.reserve_stu_list();
        sl.release_stu_list();
        sl.show_stu_list();
    }
    
  • 相关阅读:
    为 ADO 程序员设计的 ADO.NET (转)
    MSN 历史纪录分页显示(XML + XSL + Javascript)
    python连接postgresql数据库
    centOS安装scikitlearn
    14. 页高速缓存 20100228 21:10 322人阅读 评论(0) 收藏
    18. 程序的执行 20100302 12:56 131人阅读 评论(0) 收藏
    ttt 20110118 11:49 71人阅读 评论(0) 收藏
    15. 访问文件 20100228 22:55 129人阅读 评论(0) 收藏
    17. 进程间通信 20100302 12:49 191人阅读 评论(0) 收藏
    19(终). 系统启动 20100302 13:00 191人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384255.html
Copyright © 2020-2023  润新知