在另一个函数中调用一个函数

时间:2016-09-30 01:53:05

标签: c++ function linked-list

我正在创建一个带有插入函数的简单链表,该函数有4个条件:

  1. 添加到列表中间
  2. 如果列表不为空,则添加到头部

  3. 添加到空列表

  4. 添加到列表的末尾
  5. 我已经创建了一个addNode函数来处理最后两个条件,我试图在insertNode中实现它,但它一直在崩溃?有什么特别的原因,或者我只是遗漏了一些简单的东西?

    ADDNODE:

    void linkList::addNode(int dataAdd) // this only adds a node to the end
    {
        nodePtr n = new node; // what this does is make the node pointer 'n' point to this new node
        n->next = NULL;
        n->data = dataAdd;
    
        if (head != NULL)
        {
            follow = head;
            while (follow->next != NULL)
            {
                follow = follow->next;
            }
            follow->next = n;
        }
        else
        {
            head = n;
        }
    
    }
    

    insertNode:

    void linkList::insertNode(int dataInsert)
    {
        nodePtr n = new node;
        n->next = NULL;
        n->data = dataInsert;
    
        follow = head;
    
        while (follow != NULL)
        {
            trail = follow;
            follow = follow->next;
    
            if (head->data < dataInsert){
                n->next = head; //n's next will become the current head (n is now the first item in the linkList)
                head = n;       //n the head structure will now be n (n is the new head)
            }
    
            else if (follow->data < dataInsert)
            {
    
                trail->next = n;
                n->next = follow;
                break;
            }
            else
            {
                addNode(dataInsert);
            }
    
        }
    }
    

    我想在 insertNode

    中的 else 语句中调用addNode

0 个答案:

没有答案