无法扩展我的链表

时间:2016-09-16 21:30:08

标签: c++ linked-list

以下是链接列表的代码片段。我无法添加号码。每当我尝试添加到我的列表中时,数字都会被替换,所以我的列表永远不会增长。你能告诉我这段代码有什么问题吗?如果你能评论我的编码方式,那将是非常有帮助的。

   using namespace std;

struct node
{
  int number;
  std::shared_ptr<node> next;
};


bool isEmpty(std::shared_ptr<node> &head)
{
 return (head == NULL);
}


void add(std::shared_ptr<node> &head, int number)
{
 std:: shared_ptr<node> temp;
 temp.reset(new node);

 //temp = head; 

 cout<<"\n Adddress of head: "<<head.get();
// cout<<"\nAddress of temp: "<<temp.get();
 if(isEmpty(head))
 {
   head.reset(new node);
   head->number = number;
   head->next = NULL;
   cout<<"\nAdded first element";  
  }

  else
  {
     cout<<"\nAdding element to exisiting list";
     while(head->next!= NULL)
       { 
        cout<<"\n traversing to next element----->"<<temp->number;
    head = head->next;
       }     

     shared_ptr<node> newNode; 
     newNode.reset(new node);
     newNode->number = number;
     newNode->next = NULL;
     head->next = newNode;
     cout<<"\n address of newNode: "<<newNode.get();

   //  head->next = temp;      
  }   
  //cout<<"\nExiting add";
}



int main()
{
  int number;
  std::shared_ptr<node> head(nullptr);  
   char choice;
  add(head, number);

 return 0;
}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

让我们来看看add函数。我已经调整了缩进以便于阅读。

void add(std::shared_ptr<node> &head, int number)
{
    std::shared_ptr<node> temp;
    temp.reset(new node);

你在使用temp做什么用途?我什么都看不到。

    //temp = head;

    cout << "\n Adddress of head: " << head.get();
// cout<<"\nAddress of temp: "<<temp.get();
    if (isEmpty(head))
    {
        head.reset(new node);
        head->number = number;
        head->next = NULL;
        cout << "\nAdded first element";
    }

行。那个案子看起来不错。

    else
    {
        cout << "\nAdding element to exisiting list";
        while (head->next != NULL)
        {
            cout << "\n traversing to next element----->" << temp->number;
            head = head->next;

Whups!刚刚移动了head。你刚丢失了第一个节点元素。没人指着它了。共享指针可以通过破坏它来防止泄漏。很好,但你仍然丢失了数据。幸运的是head指向前head->next,防止在前headnext进入其坟墓时遭到破坏。共享指针可以保存您的培根,但会增加额外的开销。

        }

        shared_ptr<node> newNode;
        newNode.reset(new node);
        newNode->number = number;
        newNode->next = NULL;
        head->next = newNode;
        cout << "\n address of newNode: " << newNode.get();

        //  head->next = temp;
    }
    //cout<<"\nExiting add";
}

在编码风格方面,我会使用Linked List类来使这更容易处理:

#include <iostream>
class LinkedList // Ahhhr. Here be the class
{
    struct node // node is a private member of the class hidden away from sight.
    {
        int number;
        node * next;
    };
    node * head; // no shared pointer. We'll handle the memory ourselves.

public:
    LinkedList(): head(nullptr) // construct and init LinkedList
    {

    }

    // we need a copy constructor to be Rule of Three compliant
    LinkedList(const LinkedList & src): head(nullptr) // copy constructor
    {
        node * cur = src.head;
        while (cur != nullptr)
        {
            add(cur->number);
            cur = cur->next;
        }
    }
    ~LinkedList() // free up the nodes
    {
        while (head->next != nullptr)
        {
            node *temp = head;
            head = head->next;
            delete temp;
        }
        delete head;

    }

    // Need assignment operator to be Rule of Three compliant
    // OK this looks a bit weird. src is passed by reference which will 
    // trigger the copy constructor above to do the copy for us. Then we 
    // steal the head from the copy and null it so when src goes out of 
    // scope the destructor doesn't kill all the nodes we just took.
    // This is called the Copy-and-Swap idiom.
    LinkedList & operator=(LinkedList src)
    {
        head = src.head;
        src.head = nullptr;
        return *this;
    }

    bool isEmpty() // essentially unchanged. head is now a class member. 
                   // No need for parameter
    {
        return (head == NULL);
    }

    void add(int number)
    {
        // removed dead code

        if (isEmpty())
        {
            head = new node;
            head->number = number;
            head->next = NULL;
        }

        else
        {
            node * cur = head; // updates a temporary, not the head.
            while (cur->next != NULL)
            {
                cur = cur->next;
            }

            cur->next = new node;
            cur->next->number = number;
            cur->next->next = NULL;
        }
    }
};

// and a quick tester
int main()
{
    LinkedList list;
    list.add(1);
    list.add(2);
    list.add(3);

    return 0;
}

这使链接列表可以轻松模板化,以后可以省去麻烦。

有关Rule of ThreeCopy-and-Swap

的更多信息