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!?!?
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!?!?