【问题标题】:Overloading Constructor Not Working for Singly Linked List重载构造函数不适用于单链表
【发布时间】:2016-12-17 15:02:07
【问题描述】:

我有一个程序,我试图在其中创建一个单链表及其复制构造函数。程序编译成功;但是,在运行时,它会打印一半的输出,然后它会崩溃(对糟糕的技术词汇感到抱歉)而不打印剩余的输出。

我感觉我的 List 类中的复制构造函数有问题,因为程序在到达该点时崩溃了。

这是我的代码:

# include<iostream>
using namespace std;


class Node
{
    public:
        int value;
        Node* next;
};

class List
{
    public:

        List();
        List(const List &other){
            head = new Node;
            head = NULL;
            Node* temp7;
            temp7 = head;
            Node* temp6 = other.head;
            while (temp6 != NULL)
            {
                temp7 = new Node;
                temp7->value = temp6->value;
                temp7 = temp6;
                temp6 = temp6->next;
            }
        }


        void push_front(int value){
            temporary = new Node;
            temporary->value = value;
            temporary->next = NULL;
            temporary->next = head;
            head = temporary;
        }

        void insert_at(int index, int value){
            Node* temp4 = new Node;
            temp4->value = value;
            temp4->next = NULL;
            if (index == 1)
            {
                temp4->next = head;
                head = temp4;
                return;
            }
            Node* temp5 = head;
            for (int k = 0; k < index - 2; k++)
                temp5 = temp5->next;
            temp4->next = temp5->next;
            temp5->next = temp4;
        }
        void remove_at(int index){
            Node* temp2 = head;
            if (index == 1)
            {
                head = temp2->next;
                delete temp2;
                return;
            }
            for (int j = 0; j < index - 2; j++)
                temp2 = temp2->next;
            Node* temp3 = temp2->next;
            temp2->next = temp3->next;
            delete temp3;
        }

        string printList(void);
    private:
        Node* head; 
        Node* temporary;
};

List::List(){
    head = NULL;
}


string List::printList(void)
{
    Node* temp1 = head;
    int counting = 0;
    while (temp1 != NULL)
    {
        cout << "list[" << counting << "] == " << temp1->value << endl;
        temp1 = temp1->next;
        counting++;
    }
}


int main()
{
    List list1;

    list1.push_front(4);
    list1.push_front(3);
    list1.push_front(2);
    list1.push_front(1);


    cout << "list1" << endl;
    list1.printList();
    cout << endl;

    List list2(list1);

    cout << "list2" << endl;
    list2.printList();
    cout << endl;

    list1.insert_at(1, 6);

    list2.remove_at(2);

    cout << "list1" << endl;
    list1.printList();
    cout << endl;

    cout << "list2" << endl;
    list2.printList();
    cout << endl;


    return 0;
}

我无法查明程序中的错误来源。任何人都可以提出解决方案吗?

仅供参考,输出应该是这样的:(程序在崩溃之前只输出前5行)。

>list1
>
>list[0] == 1
>
>list[1] == 2
>
>list[2] == 3
>
>list[3] == 4
>
>list2
>
>list[0] == 1
>
>list[1] == 2
>
>list[2] == 3
>
>list[3] == 4
>
>list1
>
>list[0] == 1
>
>list[1] == 6
>
>list[2] == 2
>
>list[3] == 3
>
>list[4] == 4
>
>list2
>
>list[0] == 1
>
>list[1] == 2
>
>list[2] == 4

顺便说一句,这是我关于堆栈溢出的第一个问题,所以如果有什么错误或我应该做的事情,请不要犹豫,纠正我。

非常感谢大家的帮助:)

*** 我解决了这个问题。显然,我没有足够正确地实现复制构造函数。无论如何感谢您的所有意见和建议:-)

// 包含标准文件 #包括

    // Use standard namespace
    using namespace std;

    // Declare node class
    class Node
    {
        // All values are public; value of node, and next node pointer
    public:
        int value;
        Node* next;
    };

    // Declare singly linked list class
    class List
    {
    public:

        // Declare main constructor
        // Declare copy constructor
        List();
        List(const List &copying) : head(NULL)
        {
            // Use a node to move through the source linked list
            // Set the size of the new linked list
            // For every node in old list, copy it to a new node and link it to the new singly linked list
            Node* cur = copying.head;
            int size = copying.size();
            Node* end = NULL;
            for(int q = 0; q < size; q++)
            {
                Node* n = new Node;
                n->value = cur->value;
                if (head == NULL)
                {
                    head = n;
                    end = head;
                }
                else
                {
                    end->next = n;
                    end = n;
                }
                cur = cur->next;
            }
            end->next = NULL;
        }

        // Push front a new node
        // Add its value and set its next pointer to NULL
        void push_front(int value){
            temporary = new Node;
            temporary->value = value;
            temporary->next = NULL;
            temporary->next = head;
            head = temporary;
        }

        // Insert node between x and x+1
        // Get the new node's value
        // Create the new node by moving from the head->next method
        // Add the value and set up the node
        void insert_at(int index, int value){
            Node* temp4 = new Node;
            temp4->value = value;
            temp4->next = NULL;
            if (index == 1)
            {
                Node* temp9 = head->next;
                temp4->next = temp9;
                head->next = temp4;
                return;
            }
            Node* temp5 = head;
            for (int k = 0; k < index - 2; k++)
                temp5 = temp5->next;
            temp4->next = temp5->next;
            temp5->next = temp4;
        }

        // Remove node number [index]
        // Get the head
        // Iterate through the linked list
        // When at the node before the one that has to be deleted, set its value to the node after the next one
        // Delete the node that should be deleted
        void remove_at(int index){
            Node* temp2 = head;
            if (index == 1)
            {
                head = temp2->next;
                delete temp2;
                return;
            }
            for (int j = 0; j < index - 1; j++)
                temp2 = temp2->next;
            Node* temp3 = temp2->next;
            temp2->next = temp3->next;
            delete temp3;
        }

        // Simple function to pass the head of a singly linked list
        // Simple function to get the size of a function
        Node * PassHead(void);
        int size()const;
    private:
        Node* head;
        Node* temporary;
    };

    // Returns head
    Node * List::PassHead()
    {
        return head;
    }

    //  Constructor sets head to NULL
    List::List(){
        head = NULL;
    }

    // Gets the size of the singly linked list.
    // While the node_>next is not NULL, add 1 to counter
    // return counter
    int List::size()const {
        Node* temp1 = head;
        int counting = 0;
        while (temp1 != NULL)
        {
            counting++;
            temp1 = temp1->next;
        }
        return counting;
    }

    // Same function as the size() function, excetp this time, print the node value while iterating through list
    // Nothing returned
    void printList(List object)
    {
        Node* temp1 = object.PassHead();
        int counting = 0;
        while (temp1 != NULL)
        {
            cout << "list[" << counting << "] == " << temp1->value << endl;
            temp1 = temp1->next;
            counting++;
        }
    }

    // Declare main function here
    int main()
    {
        // Object of List
        List list1;

        // Push some values
        list1.push_front(4);
        list1.push_front(3);
        list1.push_front(2);
        list1.push_front(1);

        // Print the first list
        cout << "list1" << endl;
        printList(list1);
        cout << endl;

        // Copy constructor for second list
        List list2(list1);

        // Print second list
        cout << "list2" << endl;
        printList(list2);
        cout << endl;

        // Insert node in first list
        list1.insert_at(1, 6);

        // Remove node in second list
        list2.remove_at(2);

        // Print first list
        cout << "list1" << endl;
        printList(list1);
        cout << endl;

        // Print second list
        cout << "list2" << endl;
        printList(list2);
        cout << endl;


        // Program ran successfully
        return 1;
    }

【问题讨论】:

  • 您在复制构造函数中忘记了}
  • 您应该包含 complete 错误消息。它们通常包含一个行号,这对发现问题有很大帮助。
  • 谢谢@Rakete1111!我错过了那个障碍。但是,即使添加了它,它仍然不起作用。
  • 很抱歉@PeteBecker,就像我说的,这是我第一次来这个论坛。我修复了错误(它只是缺少“}”),并且编译成功。但是,当我运行它时,由于一些无法解释的错误(没有给出行号),它无法正常执行。我已经更新了上面的代码以反映缺少的右花括号。

标签: c++ constructor copy-constructor singly-linked-list


【解决方案1】:

由于此页面上的积分不足,我无法正常回答。但我发现了一些东西。

 List(const List &other){
            head = new Node;
            head = NULL;
            Node* temp7;
            temp7 = head;
            Node* temp6 = other.head;
            while (temp6 != NULL)
            {
                temp7 = new Node;
                temp7->value = temp6->value;
                temp7 = temp6;
                temp6 = temp6->next;
        }

右括号在哪里?下一个函数将在名为 List(const List &other) { ... 的函数中创建。 添加一个括号,然后重试。 希望有帮助;)

【讨论】:

  • 感谢@DotBlack,我修复了这个错误,代码可以正确编译,但它仍然崩溃,没有输出它应该输出的所有行。
  • 感谢您的提议,@DotBlack!真的很感激,虽然我没有 Skype。我设法查明了问题的主要根源(它在复制构造函数中)并更新了上面的代码。再次感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
相关资源
最近更新 更多