This is my first time with link lists. I have gotten it to pull the first bit of information into the first node, and it prints out fine after that into the console window.
When I try to add additional lists and link them, and then try to print them out, there is a blank line appearing at the top of the console window, then it prints the last data I entered. I'm not sure whats going on. My print function looks fine, as I know my first part works at least to get the first bit of data in. Could someone please point out where it is going wrong? The book we are using is really convoluted and the lecture the professor gave was really vague and had nothing similar to what we were seeing in the book, at least for a first go around with this. Code at the bottom.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream> //for ifstream and ofstream
using namespace std;
ifstream dataIn;
ofstream dataOut;
struct LList
{
string itemName;
int itemNumber;
int itemQuantity;
float itemPrice;
int itemSafeStock;
char itemFlag;
LList *linky;
};
LList *head;
LList *newNode()
{
LList *temp;
temp = new LList();
temp->itemName = "";
temp->itemNumber = 0;
temp->itemQuantity = 0;
temp->itemPrice = 0;
temp->itemSafeStock = 0;
temp->itemFlag = ' ';
temp->linky = NULL;
return temp;
}
void insert(LList *cp);
void printLL(LList *cp);
int main(int argc, char *argv[])
{
dataIn.open("invt.txt");
insert(head);
printLL(head);
system("PAUSE");
return EXIT_SUCCESS;
}
void insert(LList *cp){
for(int i = 0; i < 13; i++){ //I know I need to check for eof, this is just for testing
LList *prev, *t;
prev = NULL;
//dataIn.open("invt.txt");
char x = ' ';
string tmpName;
int tmpNumber;
dataIn.get(x);
for(int i = 0; i < 20; i++){
tmpName = tmpName + x;
dataIn.get(x);
}
dataIn >> tmpNumber;
if(head == NULL){//checks to see if the list is empty, if it is, creates first node
head = newNode();
//pulls information from .txt into the node
head->itemName = tmpName;
head->itemNumber = tmpNumber;
dataIn >> head->itemQuantity;
dataIn >> head->itemPrice;
dataIn >> head->itemSafeStock;
if(head->itemQuantity < head->itemSafeStock)
head->itemFlag = '*';
head->linky = NULL;
}
////////////////////////////////////////////
else {
while(cp != NULL && cp->itemNumber < tmpNumber){ //for ordered list by part number?
prev = cp;
cp = cp->linky;
}
if(prev != NULL){
t = newNode();
t->itemName = tmpName;
t->itemNumber = tmpNumber;
dataIn >> t->itemQuantity;
dataIn >> t->itemPrice;
dataIn >> t->itemSafeStock;
if(t->itemQuantity > t->itemSafeStock)
t->itemFlag = '*';
prev->linky = t;
t->linky = cp;
}
else {
t=newNode();
t->itemName = tmpName;
t->itemNumber = tmpNumber;
dataIn >> t->itemQuantity;
dataIn >> t->itemPrice;
dataIn >> t->itemSafeStock;
if(t->itemQuantity > t->itemSafeStock)
t->itemFlag = '*';
head = t;
}
}
}
}
void printLL(LList *cp){
while(cp != NULL){
cout << cp->itemName << " " << cp->itemNumber << " " << cp->itemQuantity << " " <<
cp->itemPrice << " "<< cp->itemSafeStock << " "<< cp->itemFlag << endl << endl;
cp = cp-> linky;
}
}