c++ - Linked lists and pointers -


#include "personlist.h" #include <iostream> #include <string> using namespace std;  personlist::personlist() {     head = null; //head personrec* }  struct personrec {     string aname;     int abribe;     personrec* link; };  void personlist::addtolist() {     //string a;     //int b;     personrec* p;     personrec **currptr = &head;     p = new personrec;     cout << "\nenter person's name: ";     cin >> p->aname;     cout<< "\nenter person's contribution: ";     cin >> p->abribe;      if (head == null)     {         cout<<1<<endl;         head=p;     }     else if(head!=null)     {          bool x = true;          while (x != false)          {              *currptr = (*currptr)->link;              if (currptr == null)              {                  currptr = &p;                  x = false;              }          }     } } 

this supposed linked list in user inputs name , bribe amount , added list highest amount of bribe being placed first.

in particular stage trying figure out how input people list more once, without bribes coming equation. because of inclusion of pointers, not at, having troubles here.

after first node in list inputted successfully, program freezes after input second node code @ "else if(head!=null)" flawed. i'm not sure if it's syntax or don't concept entirely.

also, not allowed use link previous node part of personrec structure, , might have helped me.

how can fix problem?

you getting close, code not adding new second item list. problem reaches end of list , final assignment:

currptr=&p; 

simply assigns address of new list element local variable currptr (that not result in being added list). following suggestions towoard getting working (this kind of seems homework, don't want give code):

  • initialize p->link null after creating object. not appear there constructor initialize (the mix of struct , class seems bit odd in situation).
  • remove 1 level of indirection (single asterisk) declaration of currptr.
  • in while loop before assigning next (link) address currptr, check if null. once assign it, no longer have address of node needs updated.

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -