[英]Simple Linked List Implementation in C++
我是第一個C ++課程的編程學生,最近我們介紹了鏈接列表,並且我們獲得了一個實現簡單編程的任務。 我編寫了pop_back()
函數之外的所有函數,該函數用於返回指向需要在Main()
刪除的Node
的指針。 在實際功能中不進行Node
刪除。 所以我的問題是:
你願意幫我指出我的pop_back()
函數的正確方向嗎? 另外,如果您發現其他任何我做錯的事,請告訴我。
此外,此鏈接列表只是用於字符串。 在這種情況下,一個購物清單,所以一個字符串表示項目數量(1,2),一個字符串表示項目類型。 (牛奶,雞蛋等)
下面我已經包含了我的List&Node類實現,因此您可以了解到目前為止我所做的工作。
Node.cpp
Node::Node(void)
{
descrip = " ";
quantity = " ";
previous = NULL;
next = NULL;
}
Node::Node(string q, string d)
{
descrip = d;
quantity = q;
previous = NULL;
next = NULL;
}
Node* Node::GetNext()
{
return next;
}
Node* Node::GetPrevious()
{
return previous;
}
void Node::SetNext(Node * setter)
{
next = setter;
}
void Node::SetPrevious(Node * setter)
{
previous = setter;
}
List.cpp
List::List(void)
{
first = NULL;
last = NULL;
numNodes = 0;
}
Node* List::GetFirst()
{
return first;
}
Node* List::GetLast()
{
return last;
}
void List::SetFirst(Node* setter)
{
first = setter;
}
void List::SetLast(Node* setter)
{
last = setter;
}
int List::GetNumNodes()
{
return numNodes;
}
void List::push_front(Node* item)
{
if (first == NULL)
{
first = item;
last = item;
}
else
{
Node* pFirst = first;
item->SetNext(pFirst);
first = item;
numNodes++;
}
}
void List::push_back(Node * item)
{
if (last == NULL)
{
first = item;
last = item;
}
else
{
last->SetNext(item);
last = item;
numNodes++;
}
}
Node* List::pop_front()
{
Node* temp = first;
first = first->GetNext();
if (first == NULL)
{
temp = first->GetNext();
first = p;
}
if (first == NULL)
{
last = NULL;
}
if (numNodes > 0)
{
numNodes--;
}
return temp;
}
Node* List::pop_back() // this whole function may be wrong, this is just my attempt at it
{
Node* temp;
temp = first;
while((temp->GetNext()) != NULL)
// im stuck here
}
一些指示:
0x1243bfa3
0x45afc56e
0xdeadbeef
更多指針:
您應該更喜歡在初始化列表中初始化類成員,而不是在構造函數的主體中。
在C ++中,與C89不同,我們聲明並定義一個沒有參數的函數,作為void f();
,不是void f(void);
。
在C ++中,我們通常將指針重置為0
,而不是NULL
。
請參閱下面的代碼我的意思。
好的C ++代碼將嘗試利用RAII 。 這意味着在大多數情況下避免使用原始指針。 在這種情況下,普通的舊std::auto_ptr<>
將完全替代原始的Node*
指針。 但是,我認為這里的部分練習是指針算術,所以我只是將其作為側面注釋。
如果您附加了類聲明,它對我們很有用。 我假設所有那些訪問器和mutator, GetFirst()
和SetFirst()
等都在那里,因為它們是公共的。 這是個壞主意。 首先,它們暴露私有指針,這會破壞訪問者的整個點。 其次,他們沒有做任何特別的事情,所以他們只是額外的代碼 - 這意味着額外的錯誤空間。 這讓我想到了下一點。
你的變異者是不正確的。 您盲目地為私有成員指針指定一個新值,而不刪除之前的內容。 那是內存泄漏。
列表為空時曾經嘗試過pop_front()
嗎?
最后,8是一個整數,是時候我們得到了手頭的問題。 pop_back()
。 我的問題是,如果您如此精心維護指向列表最后一個節點的指針,為什么要一直遍歷列表? 實際上,如果您不打擾維護指向列表末尾的指針,那么您必須遍歷到最后一個節點才能彈出它。 為此你正朝着正確的方向前進。 除了那個 ...
當您通過指針訪問成員時,如first->GetNext()
, 總是確保first
不是空指針 - 或者在函數的文檔注釋中聲明您認為指針不為空。
這些應該讓你開始。
代碼中的第1,2和3點:
Node::Node()
: descrip(" "), quantity(" "), previous(0), next(0)
{
}
所以,如果我理解這一點你只想運行你的鏈表,直到你到達鏈表中的最后一個節點並返回它的指針? 我很確定你有什么會做的除外
Node* List::pop_back() // this whole function may be wrong, this is just my attempt at it
{
Node* temp;
temp = first;
while(temp->GetNext() != NULL)
{
temp = temp->GetNext();
}
return temp;
}
因此,如果我正確讀取它,它將不斷循環,直到它到達后面的行中沒有的節點,然后返回它。
我喜歡以前的海報回答,但有一件事你可能要記住,如果你有一個空列表。 然后你的第一個指針將等於NULL,你將嘗試基本上調用NULL-> GetNext()和Seg Fault。 我認為你可以稍微編輯上面的代碼,仍然可以讓它像這樣工作:
Node* List::pop_back()
{
Node* temp;
temp = first;
while(temp != NULL && temp->GetNext() != NULL)
{
temp = temp->GetNext();
}
return temp;
}
如果列表中沒有任何內容並且仍能正常工作,則此函數將返回NULL。
如果你還發布了你的課程聲明,肯定會幫助我。 我不能保證下面的內容是正確的,但對我來說是有道理的
Node* List::pop_back()
{
Node *temp = NULL;
if(numNodes == 1)
{
temp = first;
// setting the list pointers to NULL
first = NULL;
// setting the list pointers to NULL
last = NULL;
//You should also probably remove the links from your node
//to the next and previous nodes but since you didn't specify
//this it is up to you
numNodes--;
}
else if(numNodes > 1) //more than one element
{
//the pointer you want to return
temp = last;
//For clarity I am creating another variable here
Node *newLast = temp->GetPrevious();
//Setting the new last node to point at nothing so now temp
//is "disconnected from the list"
newLast->next = NULL;
//the last pointer of the list is now pointing at the new last node
last = newLast;
//You should also probably remove the links from your node
//to the next and previous nodes but since you didn't specify this it is up to you
numNodes--; //decrement the counter
}
return temp;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.