Ok, I am about 20+ hours into this utterly confusing idea of link lists. The classes I have are listed below. I cannot for the life of me understand how I would write a reverse function. My office floor is cluttered with printouts and scribbles from trying to make this work. If someone could provide me with a tip on how I might attack this it would be greatly appreciated.

class Node
{
public:
   Node(string s); 
private:
   string data;
   Node* previous;
   Node* next;
friend class List;
friend class Iterator;
};

class List
{
public:
   List();
   void push_back(string data);
   void insert(Iterator iter, string s);
   Iterator erase(Iterator iter);
   Iterator begin();
   Iterator end();
   void remove(Iterator iter, string value, List a);
   void unique(Iterator iter, string value, List a);
   void reverse(Iterator iter, List a);
private:
   Node* first;
   Node* last;
friend class Iterator;
};

class Iterator
{
public:
   Iterator();
   string get() const;
   void next();
   void previous();
   bool equals(Iterator b) const;
private:
   Node* position;
   List* container;
friend class List;
};

Hi Grusky, welcome to DaniWeb!

First, maybe studying the name will be a good place to start. "Linked list" indicates that there are some kind of pieces (elements in the list) that are "linked" together. There is a pretty good description here: http://en.wikipedia.org/wiki/Linked_list. I'm not an expert, but without a doubly-linked list, you'll have to go through the whole list and store each element somewhere before you can reverse the list.

Can you explain what you have tried to do so far for the reverse() function?

Good luck!

Dave

Yea, I have looked into copying but have no idea how to copy the string data. The pointers are no big deal but I don't seem to have access to the data. I was hoping I could just use a loop and stroll through the list swapping the pointers.

Hi Grusky, welcome to DaniWeb!

First, maybe studying the name will be a good place to start. "Linked list" indicates that there are some kind of pieces (elements in the list) that are "linked" together. There is a pretty good description here: http://en.wikipedia.org/wiki/Linked_list. I'm not an expert, but without a doubly-linked list, you'll have to go through the whole list and store each element somewhere before you can reverse the list.

Can you explain what you have tried to do so far for the reverse() function?

Good luck!

Dave

well you have a doubly linked list so in reverse start at the last node and then traverse your way backwards by calling previous.

If you are programming in C++ you should not be writing your own linked-list code.
There are a number of built-in or free container classes available for that purpose.

Writing your own linked list (except to learn why you should not do it),
is a waste of time since the off-the-shelf free versions are much more flexible and error-free than you could easily write yourself. Any hiring manager would expect you to know that.

Consider using an stl::list (or std::list), it is build into most compilers.
Do a web search for stl list to get all the details.

Narue has a good linked list tutorial on her website here http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_linklist.aspx

If you're trying to wrap your head around the concept of linked lists, start out with something simple first - a singly-linked list which represents a stack is by far the easiest to write (because its simpler to build a list 'in reverse' than it is to build one forwards). Once you're comfortable with a singly-linked list, you can modify it to link in both directions (Doubly linked lists leave you with alot more to think about with regards to 'dangling' pointers)

UncleLeroy,

This is certainly an assignment (I hope!). If I'm wrong, then DEFINITELY use std::list!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.