954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

printing a linked list

hi
im havin a problem w/ printing a linked list.
my program reads two input files and makes two linked lists out of them, and apend the lists and print it out.
my input files look like this:
1 2 3 4 5 (input1)
11 22 33 44 (input2)
here's my program. i think everythin is working well except printing or may be getNext function. Please help me out. thanks.

#include
#include
#include

#define FILE_1 "input1.txt"
#define FILE_2 "input2.txt"

struct NODE
{
int data;
NODE *link;
};

struct LIST
{
NODE *head;
NODE *pos;
int count;
};


void build (LIST &list, char *filename);
//void printList (LIST list);
void append (LIST &list1, LIST &list2);
bool searchList (LIST list, NODE *&pPre,
NODE *&pLoc, int target);
bool getNext(LIST &list, int fromWhere, int &dataOut);
bool insertNode (LIST &list, NODE *&pPre, int num);

int main (void)
{

LIST list1;
LIST list2;

build (list1 , FILE_1);
// printList (list1);
build (list2 , FILE_2);
// printList (list2);

append (list1 , list2);

// printList (list1);

cout << "\n\nTotal number of nodes = "<< list1.count << endl;
return 0;
}

void build (LIST &list, char *filename)
{

ifstream infile;
NODE *pPre;
NODE *pLoc;
int num;
bool found;


infile.open(filename, ios::in);
if (!infile)
{
cerr << "\n\aCannot open " << filename << endl;
abort();
}

list.head = NULL;
list.count = 0;

while (infile>>num)
{
found = searchList(list, pPre, pLoc, num);
if (found)
cout << "\n\aError : Duplicate key " << endl;
else
insertNode (list, pPre, num);
}
infile.close();
return;
}

bool insertNode (LIST &list, NODE *&pPre, int num)
{
NODE *pNew;

if (! (pNew = new NODE))
return false;

pNew->data = num;
pNew->link = NULL;

if (pPre == NULL)
{
pNew->link = list.head;
list.head = pNew;
}
else
{
pNew->link = pPre->link;
pPre->link = pNew;
}

list.count++;
return true;

}

bool searchList(LIST list, NODE *&pPre, NODE *&pLoc, int target)

{
pPre = NULL;
pLoc = list.head;

while (pLoc && target > pLoc->data)
{
pPre = pLoc;
pLoc = pLoc->link;
}

return (pLoc ? target == pLoc->data : NULL);
}

bool getNext(LIST &list, int fromWhere, int &dataOut)

{
bool success;
if(fromWhere==0)
{
if(list.count==0)
success=false;
else
{
list.pos=list.head;
dataOut=list.pos->data;
success=true;
}
}
else
{
if(list.pos->link==NULL)
success=false;
else
{
list.pos=list.pos->link;
dataOut=list.pos->data;
success=true;
}
}
return success;
}

void printList (LIST list)

{
bool moreData; NODE *pos;

if(list.count==0)
cout<<"Sorry, nothing in the list"<data<<' ';
moreData=getNext(list,1,pos);

}
cout<<"count is "<link!=NULL)
pLoc=pLoc->link;
pLoc->link=list2.head;
}
list1.count=list1.count+list2.count;
}

ohnbabygal
Newbie Poster
12 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

Let's look more closely at this:

list.count=0;
moreData=getNext(list,0,pos);

Notice how you set list.count to 0 and that the fromWhere argument is 0. Then in getNext:

if(fromWhere==0)
{
  if(list.count==0)
    success=false;

Both fromWhere and list.count are 0. Now back to printList:

while(moreData==true) 
{
  list.count++;

moreData is not true because you set list.count and fromWhere to 0. So the loop is skipped and the list is not printed.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thanks for ur help...
even though i tried to omit the line that set count=0, it stil doesnt work.
the error i got when i compile it is (cant convert 3 from NODE * to Int & ) or something like this . and the error is when i set getNext funtion to moredata. . so i belive that the prob is when i call getNext funtion in printlist funtion, (getNext(list,0,pos);) i hoestly dont know which parameter to pass for the last parameter (which is pos, wat i thought, and i believe its not right). i know i need to put a pointer(instead of 'pos') that will point to the data from the linked list to be printed. i just dont know how to do it :(
please please help me out........
the problem is only in the print function.
please help me..
thank you so much :)

ohnbabygal
Newbie Poster
12 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

Why not just change getNext like so:

// Untested
bool getNext ( LIST& list, int& dataOut )
{
  if ( list.pos == NULL )
    return false;

  dataOut = list.pos->data;
  list.pos = list.pos->link;

  return true;
}

Then you can write printList like so:

// Untested
void printList ( LIST& list )
{
  int data;

  list.pos = list.head;

  while ( getNext ( list, data ) )
    cout<< data <<endl;
}

The biggest confusion is how getNext does so much. It would be better to separate this function into getData and getNext so that the user of the list knows what to expect. And the concept of a list having its own internal iterator causes more problems than if you gave the user control of iterators.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

It works!!!! :) Thank you so much! u are right, the way i wrote was so complicated. u just clear it up and now i know wat was wrong. thank you so much again. i really appreciate it. :) u r quite smart.

ohnbabygal
Newbie Poster
12 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

>Thank you so much!
Anytime. :)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You