簡體   English   中英

我的一份列表副本的尺寸在我的紙疊中打印不正確

[英]The size of one of my list copies is printing out incorrectly in my stack

首先,此列表的最大大小為30,每個創建的列表中的項目數存儲在num_items中,通過我在其他地方使用的push和pop方法增加和減少該數量,但是我想知道是否需要跟蹤這里也有num_items。 我將顯示期望的輸出以及得到的輸出:

在此處輸入圖片說明

現在,我將顯示復制我的堆棧的代碼:

void operator=(const Stack& s)
    {
        if (s.top == NULL)
            top = NULL;
        else
        {
            top = new Node;
            top->data = s.top->data;
            Node* newP = top;

                for(Node* curr = s.top->link; curr != NULL; curr = curr->link)
                {
                    if(num_items != MAX_SIZE)
                    {
                    newP->link = new Node;
                    newP = newP->link;
                    newP->data = curr->data;
                    }
                }
        }
    }

提供輸出的代碼是:

Stack<int> s2(s1); // s2 declared as a copy of s1
    cout << "*declare s2 as a copy of s1 (stack s2(s1))\ns2=" << s2 << endl;
    cout << "s2.Size()=" << s2.Size() << endl;
    cout << "s2.IsEmpty()=" << ((s2.IsEmpty()) ? "T" : "F") << endl;
    cout << "s2.IsFull()=" << ((s2.IsFull()) ? "T" : "F") << endl;
    cout << "s2.Peek()=" << s2.Peek() << endl;
    cout << endl;

編輯:

初始化后num_items = 0; 在我將在下面顯示的代碼中

        void operator=(const Stack& s)
    {
        if (s.top == NULL)
            top = NULL;
        else
        {
            top = new Node;
            top->data = s.top->data;
            Node* newP = top;

                for(Node* curr = s.top->link; curr != NULL; curr = curr->link)
                {
                    num_items = 0;
                    if(num_items != MAX_SIZE)
                    {
                    newP->link = new Node;
                    newP = newP->link;
                    newP->data = curr->data;
                    num_items++;
                    }
                }
        }
    }

我為我的尺寸得到的輸出結果為1,我將再次在圖像中顯示整個輸出:

在此處輸入圖片說明

第二次編輯:

我現在將代碼修改為以下內容:

void operator=(const Stack& s)
    {
        if (s.top == NULL)
            top = NULL;
        else
        {
            top = new Node;
            top->data = s.top->data;
            Node* newP = top;
                num_items = 0;
                for(Node* curr = s.top->link; curr = NULL; curr = curr->link)

                {

                    if(num_items != MAX_SIZE)
                    cout<< num_items;
                    {
                    newP->link = new Node;
                    newP = newP->link;
                    newP->data = curr->data;
                    ++num_items;
                    }
                }
        }
    }

盡管我的大小僅由9而不是10決定,但我認為這是因為我的循環實際上跳過了0或“ NULL”,但是必須有一種使其停止的方法。

最好復制單個列表,同時維護一個Node **存儲,該存儲指向需要設置為下一個Node *的變量:

void operator=( const Stack& rhs ){ // or return Stack&
  // call this->clear() to avoid memory leak
  if( rhs.top == NULL ){ top = NULL; return /* *this */; }
  Node** store = &top;
  for( Node* curr = rhs.top; curr != NULL; curr = curr->link ){
    Node* newNode = new Node;
    num_items++;
    newNode->data = curr->data;
    *store = newNode;
    store = &newNode->link;
  }
  return /* *this */;
}

除非小心刪除所有現有條目,否則此賦值運算符將產生內存泄漏。 也許已經有一個clear()方法?

以后:可以使用這些構造函數:

Stack() : num_items(0), top(NULL) {}
Stack( const Stack& other ) {
  *this = other;
}

應該在指示的地方使用這種清晰的方法:

void clear(){
  Node* curr = top;
  while( curr != NULL ){
    Node* next = curr->link;
    delete curr;
    curr = next;
  }
}
Stack<int> s2(s1);

這將調用默認的復制構造函數,而不是operator =。 但是,由於復制邏輯是在operator =中實現的,因此節點永遠不會復制到s2中。

您需要編寫一個復制構造函數,並將邏輯從operator =移入其中:

Stack(const Stack & other)
{
    // Copy other stack here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM