2015-06-20 80 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