Welcome Guest.

1Answers

I hope God can help me analyze the code below.

Asked by: Dorothy Phillips 234 views IT August 12, 2018

bool CStudent::insert(const student &astu) {

    student *newnode,*p = head;

    if(p->m_next == NULL) {

        p->m_next = new student(astu);

        p->m_next->m_next = NULL;

        return true;

    }

    while(p->m_next) {

        if(p->m_next->m_id == astu.m_id) {

            cout << "重复的学号,插入失败!\n";

            return false;

        }

        if(p->m_next->m_id > astu.m_id) {

            newnode = new student(astu);

            newnode->m_next = p->m_next;

            p->m_next = newnode;

            return true;

        }

        p = p->m_next;

    }

    p->m_next = new student(astu);

    p->m_next->m_next = NULL;

    return true;

}

 

1 Answers

  1. +7Votes  

    This code is an insertion method for the student class of CStudent. The implementation of this method mainly uses the structure of student, so it is ok to focus on this structure. In addition to storing data, this structure focuses on storing a pointer variable m_next. The content of the variable points to the next student instance object. This indicates that the student structure is prepared to organize data by means of a singly linked list. No other pointers are seen in the segment code, so only the one-way linked list can be guessed. The implementation of other specific statements, you have to look specifically, according to my meaning to understand.

    Rebecca Harrison- August 12, 2018 |