I’ve been banging my head against the wall for about 3 hours now trying to come up with the fix for this but I can’t figure it out. My test program is written as such…
int main()
{
SimpList<int> intList; // (empty) list of integers
cout << "Let's build a sorted list of integers." << endl;
cout << endl << "Uninitialized List: ";
intList.print();
cout << endl << "Length: " << intList.size() << endl;
int intData[] = { 5, 3, -2, 7, 9, -8, 1, -4 };
for (int i=0; i<8; i++)
intList.insert( intData[i] );
cout << endl << "After inserting 8 integers: ";
intList.print();
cout << endl << "Length: " << intList.size() << endl;
system("PAUSE");
return 0;
}
So the Link List is getting initialized from an array and a for loop. My class code for the node and list is here…
template < typename T > // Forward declaration of the SimpList class
class SimpList;
template < typename T >
class Node // Node class for the SimpList class
{
private:
// Constructors
Node () { next = 0; } // default constructor
// Complete the definition inline
Node ( const T &initItem, Node<T> *ptr ) { }
// Data members
T item; // Node data item
Node *next; // Pointer to the next node
friend class SimpList<T>;
};
template < typename T >
class SimpList
{
public:
// Constructor (add your code inline)
SimpList ()
{
head = &PHONY;
length = 0;
}
// List manipulation operations
void insert ( const T &newitem ); // insert a data item
bool remove ( T &item ); // remove data item
bool find ( T &item ) const; // find data item
void clear (); // empty the list
bool isEmpty () const {
if (length == 0)
return true;
else
return false;
}
// length accessor method
int size () const {
return length;
}
// print the list items
void print () const;
private: // data members
Node<T> PHONY; // empty node that anchors the list
Node<T> *head; // pointer to the beginning of the list
int length; // length of list
};
And then the insert and print functions are as follows…
template < typename T >
void SimpList<T>::print() const
{
if (length == 0)
{
cout << "List is empty." << endl;
return;
}
Node<T> *ptr = head->next;
while (ptr != NULL)
{
cout << ptr->item << " ";
ptr = ptr->next;
}
cout << endl;
}
template < typename T >
void SimpList<T>::insert(const T& newitem) {
Node<T> *currN = head->next;
Node<T> *prevN = 0;
Node<T> *tmp = new Node<T>();
tmp->item = newitem;
if(head->next == NULL ) {
head->next = tmp;
}
else {
prevN = head;
while(currN != NULL && currN->item < newitem) {
prevN = currN;
currN = currN->next;
}
prevN->next = tmp;
}
length++;
print();
}
I inserted the last “print()” into the insert function as a way of debugging what was happening and the output is quite perplexing as it gives me
5
3
-2
-2 7
-2 7 9
-8
-8 1
-8 -4
But I want the output to be sorted smallest to largest (-8 -4 -2 1 3 5 7 9)
edit: solved…forget to update tmp->next to currN. DERP.
Couple of things you need to fix:
PHONY->nextis not null , so your initialization will fail :Node constructor should initialize next to 0 instead of
this, hence you will never hit NULL condition for end of your list.in insert in else statement you should start search from head->next, currently you start search from
currn=headwhich isPHONYwhich is not correct.You also need to set
tmp->nextappropriately ,consider this case when you are inserting in middle of the list.