Hey,
I have a singly linked list that has a show all function that looks like this

virtual void Show()
   {
       myObject->Show(); myNext->Show();
   }

Where myObject is a class ordered by an int. This function prints out all information in the list.

I was wondering how I could change this at the start print out just the first object then on user input move on to the next object?

Thanks!

Sorry. I’ve got the print out the 1st object only bit to work! Just had to take out the myNext->Show() bit from the code above. Anyone have any idea about the showing the next object in the list?
Thanks

Loop through the linked list. Start at the head, and show it, then move to the next node and show it, and so on until the current node's next pointer is null.

I know I’m going to sound like a fool now but how do you do that?

Thanks! But the way I’ve set up my linked list I can’t convert a node to a head node and then change it back to a node!

I don’t suppose you know somewhere where that has an example of a good linked list to save me going all the way through google? Or even a tutorial that shows how to use the STL linked lists?

When you create a new list, you should make a Node *head. This will serve as the beginning of the list. No matter what you do with your list, always make sure the head pointer is pointing to the first index of the linked list.

Such as...

struct Node
{
   int element;
   Node *next;
}
Node *head;

// some where else in code

Node *head = new Node;

Edited 3 Years Ago by Dani: Formatting fixed

Ok, I'm getting somwhere now!
How do I change this linked list to look more like what you’ve said?

template <class T>
class Node
{
public:
    Node(){}
    virtual ~Node(){}
    virtual Node * Insert(T * theObject) =0;
    virtual void Show() = 0;
    virtual void ShowNext() = 0;
private:
};
 
 
template <class T>
class InternalNode: public Node<T>
{
public:
    InternalNode(T * theObject, Node<T> * next);
     ~InternalNode(){delete myNext; delete myObject;}
    virtual Node<T> * Insert(T *theObject);
    virtual void Show()
    {
        myObject->Show();system("PAUSE"); myNext->Show();
    }  
    
    virtual void ShowNext()
    {
     // myObject->Show();  show first
    }
private:
    T * myObject;        //the data
    Node<T> * myNext;    // the next bit of data in the list
};
 
//constructor
template <class T>
InternalNode<T>::InternalNode(T* theObject, Node<T> * next):
myObject(theObject), myNext(next)
{
}
 
 
 
 
//the main part of the list
// when u put new object in to the list
//it is passed to the node that figures out 
//where it goes
template <class T>
Node<T> * InternalNode<T>::Insert(T * theObject)
{
    //is the new data bigger or smaller
      int result = myObject->Compare(*theObject);
      
      switch(result)
      {
      // by convention if new data is same as old it goes 1st
      case KIsSame:      //fallthrough
      case KIsLarger:     //new data comes b4 old
            {
                 InternalNode<T> * ObjectNode =
                       new InternalNode<T>(theObject, this);
                 return ObjectNode;
            }
            // if it is bigger than the old pass it on
            //to the next node and let it handle it
      case KIsSmaller:
            myNext = myNext->Insert(theObject);
            return this;
      }
      return this;      //appease the compiler
}
 

//Tail node is just a sentinel
template <class T>
class TailNode : public Node<T>
{
public:
    TailNode(){}
    virtual ~TailNode(){}
    virtual Node<T> * Insert(T * theObject);
    virtual void Show(){}
    virtual void ShowNext(){}
private:
};
 
 
 
//if data comes to here it must be b4
//because this is the tail and nothing comes after it
template <class T>
Node<T> * TailNode<T>::Insert(T * theObject)
{
    InternalNode<T> * ObjectNode = new InternalNode<T>(theObject, this);
    return ObjectNode;
}
 

//Head node has no data, it just points to the
// begining of the list
template <class T>
class HeadNode : public Node<T>
{
public:
    HeadNode();
    virtual ~HeadNode(){delete myNext;}
    virtual Node<T> * Insert(T * theObject);
    virtual void Show(){myNext->Show();}
    virtual void ShowNext(){myNext->ShowNext();}
//private:
    Node<T> * myNext;
};
 
 
//as soon as head is
//created so is the tail
template <class T>
HeadNode<T>::HeadNode()
{
    myNext = new TailNode<T>;
}
 
 
//Nothing comes b4 the head so just
// pass the data to the next node
template <class T>
Node<T> * HeadNode<T>::Insert(T * theObject)
{
    myNext = myNext->Insert(theObject);
    return this;
}
//the linked list
template <class T>
class LinkedList
{
public:
    LinkedList();
    ~LinkedList(){delete myHead;}
    void Insert(T * theObject);
    void ShowAll(){myHead->Show();}
    //print out 1st node
    void ShowFirst(){myHead->ShowNext();}
  /*  void ShowNext()
        {
        for(HeadNode<T> *temp = myHead; temp != NULL; temp = temp->myNext)
                {
                           cout << temp->data;
                }
        }*/
private:
    HeadNode<T> * myHead;
    Node<T> * myNext;
};
 
 
 
//create the head node
//which creates the tail node
//so an empty list points to the head which
//points to the empty tail
template <class T>
LinkedList<T>::LinkedList()
{
    myHead = new HeadNode<T>;
}
//delegation
template <class T>
void LinkedList<T>::Insert(T * pObject)
{
    myHead->Insert(pObject);
}
This article has been dead for over six months. Start a new discussion instead.