c ++链接列表在开始时插入错误节点

时间:2015-02-15 08:26:36

标签: c++ linked-list

我一直在使用链接列表编写程序,但由于某种原因,我的程序在程序开始时制作了一个错误的节点,而这个节点正在弄乱其他所有内容

此文件控制头文件中的函数

Node *createNode() {

    Node *newNode = new Node;

    cout << "Enter in Students last Name" << endl;
    cin >> newNode->lastName;
    cout << "Enter in Students first Name" << endl;
    cin >> newNode->firstName;
    cout << "Enter in Students id" << endl;
    cin >> newNode->idNumber;

    return newNode;
}

Node *insertNode(Node *ptr) {

    Node *newNode = createNode();

    if( ptr == NULL ) {
        ptr = newNode;
    } else if ( ptr->idNumber > newNode->idNumber ) {
        newNode->next = ptr;
        ptr = newNode;
    } else {
       Node *temp = ptr;
       while( temp->next != NULL && 
              temp->next->idNumber < newNode->idNumber )
           temp = temp->next;
       if( temp->next == NULL ) {
           temp->next = newNode;
       } else {
            newNode->next = temp->next;
            temp->next = newNode;
       }
    }

    return ptr;
}

Node *searchNode(Node *ptr, int id) {

    Node *temp;
    Node *found;
    bool isfound = false;
    temp = ptr;

    if(temp == NULL) {
        cout << "There are no students in the list" << endl;
        isfound = true;
    }
    while( isfound != true && temp->next != NULL ) {
        if( temp->idNumber == id ) {
            found = temp;
            isfound = true;
            cout << found->lastName << ", " << found->firstName 
                 << ": " << found->idNumber << endl;

        } else {
            temp = temp->next;
        }
    }

    return found;
}


void printList( Node *ptr ){

    Node *temp = ptr;

    while( temp != NULL ) {
        cout << temp->lastName << ", " 
             << temp->firstName<< ": " 
             << temp->idNumber << endl;
        temp = temp->next;
    }    
}

这是执行程序的主文件

int main() {

    Node *list = new Node();
    displayList(list);

    return 0;
}

void displayList( Node *node ) {

    int option = -1;
    cout << node->idNumber << endl;

    while( option != 5 ) {
        cout << endl;
        cout << "What do you want to do?" << endl;
        cout << "1: Add a student" << endl;
        cout << "2: Search for student" << endl;
        cout << "3: Delete Student" << endl;
        cout << "4: Print Student List" << endl;
        cout << "5: Exit" << endl;
        cout << "Enter a number for the choice: ";
        cin >> option;

        if( cin.fail() || option > 5 || option < 0 ) {
            cin.clear();
            cin.ignore();
            option = -1;
        }

        switch( option ) { 
            case 1:{
                insertNode(node);
            }
                break;
            case 2:{
                if(node != NULL){
                    cout<<"Enter the id you want to find: ";
                    int sid;
                    cin >> sid;
                    searchNode(node, sid);
                } else {
                    cout<<"No Ids in the list"<<endl;
                }
            }    
                break;
            case 4: printList(node);
                break;
            case 5: cout<<"GoodBye."<<endl ; 
                break;
            default: cout<<"you have entered a invalid character"<<endl;

        }
    }
}

这是在创建新节点之前创建的内容

What do you want to do?
1: Add a student
2: Search for student
3: Delete Student
4: Print Student List
5: Exit
Enter a number for the choice: 4
, : 0

1 个答案:

答案 0 :(得分:0)

Node *list = new Node();,因为您在main中创建了一个新的Node并将有效的Node地址传递给displayList( Node *node );然后再传递给printList( Node *ptr ),因此其打印零已初始化Node数据。

您可以在Node *list = new Node();

中将Node *list = NULL更改为main()

删除cout << node->idNumber << endl;中的displayList( Node *node );,否则您将获得分段错误或空解除引用。

Node *createNode(),添加newNode->next = 0;,您必须将新创建的next的{​​{1}}指针设置为Node

NULLNode *searchNode(Node *ptr, int id)更改为while( isfound != true && temp->next != NULL ),否则将无法搜索最后一个节点。

while( isfound != true && temp != NULL )的{​​{1}}的返回值存储在insertNode(node) node中,否则您将丢失displayList( Node *node );和{{}新添加的节点1}} node = insertNode(node)

的案例