c++链表在开始处插入一个错误的节点

c++ Linked list inserts a false node at start

本文关键字:一个 错误 节点 链表 开始 插入 c++      更新时间:2023-10-16

我一直在使用链表编写程序,但由于某种原因,我的程序在程序开始时创建了一个错误的节点,这扰乱了的其他一切

该文件控制头文件中的功能

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

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

您可以在main() 中将Node *list = new Node();更改为Node *list = NULL

删除displayList( Node *node );中的cout << node->idNumber << endl;,否则将出现分段错误或空取消引用。

Node *createNode()中,添加newNode->next = 0;时,必须将新建的Nodenext指针设置为NULL

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

insertNode(node)node的返回值存储在displayList( Node *node );中作为node = insertNode(node),否则您将失去insertNodeifelse if情况下新增的节点

相关文章: