zoukankan      html  css  js  c++  java
  • 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();
    }
    
  • 相关阅读:
    HTML5游戏参考资料
    Xcode 4.2 编译 ios5.1
    Const,Const函数,Const变量,函数后面的Const (zz)
    AndroidJNI 通过C++调用JAVA
    android adb shell 命令大全
    《Programming WPF》翻译 目录
    《Programming WPF》翻译 第3章 1.什么是控件
    CLR笔记目录
    《Programming WPF》翻译 第3章 3.内嵌控件
    《Programming WPF》翻译 第3章 2.处理输入
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384255.html
Copyright © 2011-2022 走看看