Converting a linked list to a circular list -


first time using site , beginner in c++. have linked list built , trying convert circular linked list it's not going well. give me 2 cents going wrong? thanks.

edit: problem is, after attempt connect last node first, display list again , first node seems have been replaced last node.

list before: 123456 list after: 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 desired number of suitors     (int = numberofsuitors-1; > 0; --i)     {         headinsert(head, i);     }       // iterate through list , display each value     temp = head;     while (temp != null)     {         cout << temp->getnum();         temp = temp->getnext();      }      cout <<  endl;       //get last node, connect first, delete head     temp = head;      while (temp->getnext() != null)     {         temp = temp->getnext();     }      //attempt create circular list      temp->setnext(head->getnext());     delete head;        (int = 0; < numberofsuitors; ++i)     {         cout << temp->getnum();         temp = temp->getnext();     }      cout << endl;      return 0; }  void getnumsuitors(int& numberofsuitors) {     cout << "please enter number of suitors:";     cin >> numberofsuitors;          {         if (numberofsuitors <= 0)         {             cout << "invalid number of suitors. requires more 1 suitor\n";             cout << "please enter number of suitors:";             cin >> numberofsuitors;         }         else if (numberofsuitors == 1)         {             cout << "trivial number of suitors. requires more 1 suitor\n";             cout << "please enter 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; } 

you find last node, set last node nextptr head node's nextptr, instead of setting head node itself.

i'm not sure want deleting head node, btw.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -