开发者

Linked list - Almost done, but still minor problems somewhere. Help needed

开发者 https://www.devze.com 2023-02-14 03:19 出处:网络
void currentfor() { if(current == NULL) { cout << \"You don\'t have any members yet!\" << endl;
void currentfor() 

{

   if(current == NULL) 

{

      cout << "You don't have any members yet!" << endl;

   }
 else
 {

 if (current->next == NULL)

         cout << "This is the end of the list." << endl;
      else
         current = current->next;
   }

}

void currentbac() 
{

   if (current == first)开发者_运维问答

      cout << "This is the beginning of the list." << endl;

   else
 {

      list *previous;

      previous = first;

      while (previous->next != current)
 {

         previous = previous->next;

      }

      current = previous;

   }

}

I'm ALMOST there but there's still an issue.

How do I know where my current position is when I move my current position? Because whenever I use these two move functions it doesn't seem to be working. I try to add a node at the "current" position when I move but its always adding the item at the end after all the nodes.

PLUS in my previous code I'm not able to change the fact that the "<--current position" is pointing at all the nodes and not at one node.

I want to point to one node so its more clear that user knows that's where the current pointer is.

Hope people understood .-. I'm just not used to C++ but my professor wants me to learn on my own ( whatever that means) I've been trying to get references online its so hard to get linked list info for the project I'm doing.


"I try to add a node at the "current"position when i move but its always adding at the end after all the nodes."

The problem is with you add node code, not your moving code.

You have not posted your add node code, so it is very hard to say what the problem is. You will have to unlink the nodes before and after the new node and link them to the new node, and lso link the new node to the two old ones before and after - always remembering to handle the first and last node carefully. Have you done all that?


If you just need a linked list implementation for your project it's recommended to use std::list. Take a look at http://www.cplusplus.com/reference/stl/list/ . if you wish to learn c++ there is also a tutorial on that page.


If you want to be able to navigate backwards through a linked list, I'd suggest you implement it as a doubly linked list. That is, you have a next and prev pointer on each element. Will be much simpler to navigate that way.


Specify the structure of the node. (I mean the data you are holding in the list node. eg. Int or float or some object). As said by JohnFx best way to traverse backwards is by using a doubly link list. Specify the insertion code you are using and the your node sturture.

0

精彩评论

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