2015-06-20 81 views
-2

首次使用本站和C++初學者。我有一個鏈接列表,並試圖將其轉換爲循環鏈表,但效果不佳。任何人都會給我2美分,以便我去哪裏錯了?謝謝。將鏈接列表轉換爲循環列表

編輯:問題是,在我嘗試將最後一個節點連接到第一個節點後,我再次顯示該列表,並且第一個節點似乎已被替換爲最後一個節點。 123456 名單後:623456

#include <iostream> 
#include "SuitorNode.h" 

using namespace std; 

void getNumSuitors(int& numberOfSuitors); 
void headInsert(SuitorNodePtr& head, int value); 

int main() 
{ 
    SuitorNodePtr head, temp, remove; 
    int numberOfSuitors; 

    getNumSuitors(numberOfSuitors); 


    head = new SuitorNode(numberOfSuitors); 

    //Creates list of nodeswith the desired number of suitors 
    for (int i = numberOfSuitors-1; i > 0; --i) 
    { 
     headInsert(head, i); 
    } 


    // Iterate through the list and display each value 
    temp = head; 
    while (temp != NULL) 
    { 
     cout << temp->getNum(); 
     temp = temp->getNext(); 

    } 

    cout << endl; 


    //get to last node, connect to first, delete head 
    temp = head; 

    while (temp->getNext() != NULL) 
    { 
     temp = temp->getNext(); 
    } 

    //Attempt to create circular list 

    temp->setNext(head->getNext()); 
    delete head; 



    for (int i = 0; i < numberOfSuitors; ++i) 
    { 
     cout << temp->getNum(); 
     temp = temp->getNext(); 
    } 

    cout << endl; 

    return 0; 
} 

void getNumSuitors(int& numberOfSuitors) 
{ 
    cout << "Please enter the number of suitors:"; 
    cin >> numberOfSuitors; 

    do 
    { 
     if (numberOfSuitors <= 0) 
     { 
      cout << "Invalid number of suitors. Requires more than 1 suitor\n"; 
      cout << "Please enter the number of suitors:"; 
      cin >> numberOfSuitors; 
     } 
     else if (numberOfSuitors == 1) 
     { 
      cout << "Trivial number of suitors. Requires more than 1 suitor\n"; 
      cout << "Please enter the number of suitors:"; 
      cin >> numberOfSuitors; 
     } 
     else 
     { 
      cout << "You entered " << numberOfSuitors << " suitors.\n"; 
     } 
    } while (numberOfSuitors <= 1); 
} 



void headInsert(SuitorNodePtr& head, int value) 
{ 
    SuitorNodePtr tempPtr; 
    tempPtr = new SuitorNode(value); 
    tempPtr->setNext(head); 
    head = tempPtr; 
} 


class SuitorNode 
{ 
public: 
    SuitorNode(); 
    ~SuitorNode(); 
    SuitorNode(int initialnum); 
    int getNum(); 
    SuitorNode* getNext(); 
    void setNext(SuitorNode *nextNode); 


private: 
    SuitorNode *next; 
    int num; 
}; 

typedef SuitorNode* SuitorNodePtr; 



SuitorNode::SuitorNode() : num(0), next(NULL) 
{ 
    //deliberately empty 
} 


SuitorNode::~SuitorNode() 
{ 
} 


SuitorNode::SuitorNode(int initialnum) : num(initialnum), next(NULL) 
{ 
    //deliberately empty 
} 

int SuitorNode::getNum() 
{ 
    return num; 
} 

SuitorNode* SuitorNode::getNext() 
{ 
    return next; 
} 

void SuitorNode::setNext(SuitorNode *nextNode) 
{ 
    next = nextNode; 
} 
+1

請[edit]添加一個特定的問題陳述 - 「它不起作用」可以假設,但* how *不起作用?什麼錯誤信息或不正確的行爲是特徵? –

回答

0

你找到的最後一個節點,但接盤的最後一個節點nextPtr到頭節點的nextPtr,而不是將其設置爲頭節點本身

名單。

我不確定你想要刪除頭節點,順便說一句。

+0

啊,這非常有意義,現在效果更好。謝謝。 – ThisGuy