用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 <iostream>

#include "student.h"

#include <string.h>

#include <stdlib.h>

 

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 <iostream>

#include "mylist.h"

#include <stdlib.h>

 

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 <iostream>

#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

 

 

 

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值