用C++实现链表存储学生基本信息

用C++实现链表存储学生信息,用尾插法实现插入:

为了代码规范,选择书写 多个.cpp  .h文件

 main.cpp  mylist.cpp  mylist.h  student.cpp  student.h

 

student.h

#ifndef _STUDENT_H_

#define _STUDENT_H_

 

class Student

{

private:

        char m_name[10];

        int m_age;

        char m_sex;

        Student *m_next;

 

public:

        Student(char *a = "xxx",int b = 20,char c = 'm');

        void print();

        void SetNext(Student *s);

        Student *GetNext();

};

 

#endif

 

 

Student.cpp

#include

#include "student.h"

#include

#include

 

using namespace std;

 

Student::Student(char *a,int b,char c)

{

        m_age = b;

        m_sex = c;

        strcpy(m_name,a);

        m_next = NULL;

}

 

void Student::print()

{

        cout << "m_name :" << m_name << endl;

        cout << "m_age :" << m_age << endl;

        cout << "m_sex :" << m_sex << endl;

}

 

void Student::SetNext(Student *s)  //设置下一个结点

{

        m_next = s;

}

 

Student* Student::GetNext() //得到下一个结点

{

        return m_next;

}

 

 mylist.h

#ifndef _MYLIST_H_

#define _MYLIST_H_

 

#include "student.h"

 

class Mylist

{

private:

        Student *m_first;

public:

        void push_back(Student *s);

        void traveser();

        void remove(Student *s);

        Mylist();

};

 

#endif

 

 

 

 

 

 

 mylist.cpp

#include

#include "mylist.h"

#include

 

using namespace std;

 

Mylist::Mylist()

{

        m_first = new Student;

        if(NULL == m_first)

        {

                cout << "new failure" << endl;

        }

}

 

void Mylist::push_back(Student *s)  //尾插法

{

        Student *p = m_first;

 

        while(p->GetNext() != NULL)

        {

                p = p->GetNext();

        }

        p->SetNext(s);

}

 

void Mylist::traveser() //遍历

{

        Student *p = m_first;

 

        while(p->GetNext() != NULL)

        {

                p = p->GetNext();

                p->print();

        }

}

 

void Mylist::remove(Student *s)

{

        Student *p = m_first;

 

        while(p->GetNext() != s)

        {

                p = p->GetNext();

                if(p->GetNext() == NULL)

                {

                        cout << "error" << endl;

                }

        }

        //Student *tmp;

        p->SetNext(p->GetNext()->GetNext());

        //p->GetNext() = tmp;

 

}

 

 

 main.cpp

#include

#include "mylist.h"

 

using namespace std;

 

int main()

{

 

        Mylist mylist;

 

        Student *s1 = new Student("aa",120,'m');

        Student *s2 = new Student("bb",220,'f');

        Student *s3 = new Student("cc",320,'m');

        Student *s4 = new Student("dd",420,'f');

        Student *s5 = new Student("ee",520,'m');

 

        mylist.push_back(s1);

        mylist.push_back(s2);

        mylist.push_back(s3);

        mylist.push_back(s4);

        mylist.push_back(s5);

 

        mylist.traveser();

 

        mylist.remove(s2);

        mylist.traveser();

return 0;

}

 

运行结果:

m_name :aa

m_age :120

m_sex :m

m_name :bb

m_age :220

m_sex :f

m_name :cc

m_age :320

m_sex :m

m_name :dd

m_age :420

m_sex :f

m_name :ee

m_age :520

m_sex :m

m_name :aa

m_age :120

m_sex :m

m_name :cc

m_age :320

m_sex :m

m_name :dd

m_age :420

m_sex :f

m_name :ee

m_age :520

m_sex :m

 

 

 

你可能感兴趣的:(用C++实现链表存储学生基本信息)