Linked List... NEED HELP!

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
I'm working on one of my link list assignment, but I can't figure out what I did wrong.

The assignment is: The objective of this lab exercise is to create a linked list with the nodes assembled in order as they are generated

My strategy: First create a link list that holds all the addresses and values temporarily, then copy it to the real one. That way when I output the values in the list they'll be in the same order as they assembled.

Problem: The program copies the temp link list holder successfully, but when I try to output them it only outputs 2, and the 2 are the repetiton of the first node!

The code:
#include <iostream.h>

struct listNode
{
int data;
listNode *next;

listNode (); //default constructor
listNode (int, listNode *); // struct constructor
};

listNode::listNode ()
{
}

listNode::listNode (int tempData, listNode * tempNext)
{
data = tempData;
next = tempNext;
}

typedef listNode* listPtr;

struct listType
{
listPtr first;
listPtr last;
};


void createList (listPtr &, listType &);
void printList (listPtr);

main()
{
listPtr list;
listType L;
createList (list, L);
// printList (list);
}

void createList (listPtr &root, listType &L)
{
listPtr tempRoot; //a temporary holder for the nodes
tempRoot = NULL; //first node contains null
for (int k=1; k <= 20; k++)
{
tempRoot = new listNode (k, tempRoot);
}
for (int m = 1; m <= 20; m++)
//copy the nodes from the others, so the list is not in reverse order
{
root = new listNode (tempRoot->data, tempRoot);
tempRoot= tempRoot->next; //going to the next node
}
}

void printList (listPtr root)
{
while (root != NULL)
{
cout << root -> data << " ";
root = root -> next;
if(root==NULL)
cout << "NULL!";
}
}

what's wrong with it!?!?
 

GigaCluster

Golden Member
Aug 12, 2001
1,762
0
0
Your strategy is wrong... you do not need to create any temporary location.
What you should do is create the first node. Then when the user enters another number, assign to previous node's 'next' pointer the memory location of the current one, then assign the entered number to the current node's data variable.
And keep going like that.

Then all you have to do is print current's data variable, then assign to current the value of current->next.
 

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0


<< assign to previous node's 'next' pointer the memory location of the current one >>



Yes I know how to do that, the student outline gives a program that does it, but it puts everything in the reverse order. Meaning that when I output them I start with the last number the user input, I need to do something so that after user finishes entering all the number it'll output from the very beginning.

 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
You should be able to do this in one pass -- if the program you've been given creates a list in the reverse of the order that you want, you just need to switch what gets linked to what:
( old list ) -> ( new item )
becomes
( new item ) -> ( old list )



<< The assignment is: The objective of this lab exercise is to create a linked list with the nodes assembled in order as they are generated >>



Hmm, I'd wonder whether "assembled in order as they are generated" meant sorted ( 7,2,9,1 --> 1,2,7,9 ) but I assume the actual assignment description makes clear that all you need is a list in order of generation. Inserting in order is probably your next assignment :)