开发者

Help with displaying list of contents C++ Linked lists!

开发者 https://www.devze.com 2023-02-14 03:14 出处:网络
void display() { list *newlist; newlist = first; cout << endl; do { if (newlist == NULL) cout << \"List is empty\" << endl;
void display()


  {  

     list *newlist;

     newlist = first;

     cout << endl;

     do
      {
       if (newlist == NULL)
       cout << "List is empty" << endl;
       else
       {
          cout << "Name is: " << newlist->name << " ";
          cout << "Age is: " << newlist->age << " ";
          cout <<开发者_高级运维 "Height is: " << newlist->height;
          if (newlist==current)
          cout<<" <-- Current position ";
          cout<< endl;
          newlist = newlist->next;
       }
      }
    while(newlist!=NULL);
    cout << "End of List" << endl;
  }

I corrected my whole code with the help from here about the current initialization i love this site ! Just this one problem I want to show the Cursor at the current node only. But this code doesn't even show the "<--current position" written. When I cut out the "if(newlist==current)" the "<--current position" appeared for all the nodes. So Im a bit confused as to where I should be placing this... Also I have to insert a node between other nodes. Can someone show me a code on how to show the "current position" wherever I move current or wherever the current is pointing at ?


That code should work fine, if indeed current points to one of the nodes in the list.

You should be able to find out by ineserting something like:

cout << newlist << " " << current << endl;

before:

newlist = newlist->next;


Mahis, in your previous post I corrected your source code. Please read the comments in the code I provided. This will answer your question. See here.

You may have initialized your current to NULL, but when you add an element, you also need to set current to the correct value. For example, when you insert the first value in your linked list, current = first. The problem now is that you are comparing newlist to NULL, which will never evaluate to true. And so it will never print your current position.

0

精彩评论

暂无评论...
验证码 换一张
取 消