| | |
linked lists show next
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2006
Posts: 16
Reputation:
Solved Threads: 0
Hey,
I have a singly linked list that has a show all function that looks like this
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!
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!
•
•
Join Date: Apr 2007
Posts: 103
Reputation:
Solved Threads: 17
C++ Syntax (Toggle Plain Text)
for(Node *temp = head; temp != NULL; temp = temp->next) { cout << temp->data; }
•
•
Join Date: May 2006
Posts: 16
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Apr 2007
Posts: 103
Reputation:
Solved Threads: 17
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...
[code]
struct Node
{
int element;
Node *next;
}
Node *head;
// some where else in code
Node *head = new Node;
Such as...
[code]
struct Node
{
int element;
Node *next;
}
Node *head;
// some where else in code
Node *head = new Node;
•
•
Join Date: May 2006
Posts: 16
Reputation:
Solved Threads: 0
Ok, I'm getting somwhere now!
How do I change this linked list to look more like what you’ve said?
How do I change this linked list to look more like what you’ve said?
C++ Syntax (Toggle Plain Text)
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); }
![]() |
Similar Threads
- Linked Lists in C (C)
- Singly-Linked Lists: Ultimate (C++)
- Bug when creating linked lists in Dev C++ (C++)
- Linked Lists (C)
- C/ Need Help with Linked Lists please (C)
- stack of linked lists (C++)
Other Threads in the C++ Forum
- Previous Thread: Multiple definition error
- Next Thread: cin skip ?
Views: 920 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





