Hi to all,
I have just started programing with C++ but I am having some problems with linked lists
I have this easy homework that I cant compile correctly.

We have this structure:

struct rekord 
{
	char key[10];
	int nr;
	rekord next;
};

Write a program that reads the elements of the list and makes a new sorted one by the key field. If they have the same value then sort by the nr field.

I don't want the full program, just a function that adds new elements to the list.

Thank you,
and a Happy 2009 to all :)

If it is a linked list, I think it should be
rekord* next;

Then you can have a separate node that designates the starting of linked list. say,

rekord* first = new rekord;;
first->next = 0;

Now a function to add a new node

void Add(char[] key, int nr)
{
// Traverse till the last node
rekord* node = first;
while (node->next != 0);
// We have found the last node. Just insert new node after this
rekord* newNode = new rekord;
// Initialize the correct members
strcpy(newNode->key, key);
newNode->nr = nr;
newNode->next = 0;
// Now set the newNode as the last node
node->next = newNode;
}

I'ven't used any C++ editor. So please excuse me if ther is any compilation error. But the logic is ok, I think.

http://cppkid.wordpress.com

This article has been dead for over six months. Start a new discussion instead.