I just learned about linked lists, so i decided to experiment a little. I'm trying to make a linked list that automatically keeps all of the data in consecutive order (like bubble sort built in :P).

Anyways, the problem is that while it adds data, the second thing added disappears, and IDK why :|

Here's the code:

sortlist.h

#ifndef SORTLIST_H_INCLUDED
#define SORTLIST_H_INCLUDED

template<typename T>
class sortlist {
    public:
        sortlist() { bottom = NULL; }
        sortlist(typename T dataToAdd);
        ~sortlist() {}
		
		void printall() { for(node* x = bottom; x->next != NULL;) { std::cout <<x->data<<"\n"; x = x->next; } }
        void add(typename T dataToAdd);

    private:
        struct node { typename T data;
                      node* next;
                    } *bottom;

};

template<typename T>
sortlist<typename T>::sortlist(typename T dataToAdd) {
    bottom = new node;
    bottom->data = dataToAdd;
    bottom->next = NULL;
}

template<typename T>
void sortlist<typename T>::add(typename T dataToAdd) {
	// if list is empty
	if(bottom == NULL) {
		bottom = new node;
		bottom->data = dataToAdd;
		bottom->next = NULL;
	}
	
	// iterate shall we?
	else {
		node* iter = bottom;	

		while((iter->data < dataToAdd) && (iter->next != NULL)) {
			iter = iter->next;	
		}

		node* newdata = new node;
		newdata->data = dataToAdd;
		newdata->next = iter->next;
		iter->next = newdata;	
	}
}


#endif // SORTLIST_H_INCLUDED

main.cpp

#include <iostream>
#include "sortlist.h"

using namespace std;

int main() {
	
	sortlist<int> ll;
	ll.add(34534);
	ll.add(789);
	ll.add(344);
	ll.add(344);
	ll.add(466);
	ll.add(654);

	ll.printall();

	std::cin.get();
	return 0;
}

anyone see what's wrong??? :?

Recommended Answers

All 3 Replies

In main function when the data '34534' is added, the pointer bottom points to '34534', when the second data '789' is inserted, the insertion takes place but your code is such that the pointer bottom will be still pointing to the data '34534'.

When you call the function ll.printall() the pointer will bottom will be pointing to '34534'.

Obviously your add function is wrong. How about you ditch the sorting
part of this and make a regular linked list, unless you haven't done that
already?

And your print function is wrong :

void printall() {
			for(node* x = bottom; x != NULL;) { 
				std::cout <<x->data<<"\n"; x = x->next; 
			} 
		}

The template was not working when the value of the item to be added is less than the value of the first item in the list. I corrected it like this:

template<typename T>
void sortlist<typename T>::add(typename T dataToAdd) {
	// if list is empty
    if(bottom == NULL) {
		bottom = new node;
		bottom->data = dataToAdd;
		bottom->next = NULL;
	}
	
	// iterate shall we?
	else {
		node* iter = bottom;	

		while((iter->data < dataToAdd) && (iter->next != NULL)) {
			iter = iter->next;	
		}
		node* newdata = new node;
		newdata->data = dataToAdd;
        if( iter == bottom)
        {
            newdata->next = bottom;
            bottom = newdata;
        }
        else
        {
		    newdata->next = iter->next;
		    iter->next = newdata;	
        }
    }
}

The second problem is printall() did not work correctly because it stopped before the last item in the list was printed. I don't like everything on one line as you had it because it makes it nearly impossible to step through the code with the debugger.

void printall() 
        
        { 
            for(node* x = bottom; x != NULL; x = x->next) 
            { 
                std::cout <<x->data<<"\n";
            } 
        }

[edit] Oops! what^^^ said too :) [/edit]

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.