printing a linked list

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 12
Reputation: ohnbabygal is an unknown quantity at this point 
Solved Threads: 0
ohnbabygal ohnbabygal is offline Offline
Newbie Poster

printing a linked list

 
1
  #1
Oct 8th, 2004
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 <fstream.h>
#include <iomanip.h>
#include <stdlib.h>

#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"<<endl;
else
{
cout<<"Begin data Print: "<<endl;
list.count=0;
moreData=getNext(list,0,pos);

while(moreData=true)
{
list.count++;
cout <<setw(3)<<list.pos->data<<' ';
moreData=getNext(list,1,pos);

}
cout<<"count is "<<list.count<<endl;
cout <<endl;
}
}

void append (LIST &list1, LIST &list2)

{
NODE *pLoc;
if(list1.count==0)
list1.head=list2.head;
else
{
pLoc=list1.head;
if(pLoc->link!=NULL)
pLoc=pLoc->link;
pLoc->link=list2.head;
}
list1.count=list1.count+list2.count;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: printing a linked list

 
0
  #2
Oct 8th, 2004
Let's look more closely at this:
  1. list.count=0;
  2. moreData=getNext(list,0,pos);
Notice how you set list.count to 0 and that the fromWhere argument is 0. Then in getNext:
  1. if(fromWhere==0)
  2. {
  3. if(list.count==0)
  4. success=false;
Both fromWhere and list.count are 0. Now back to printList:
  1. while(moreData==true)
  2. {
  3. 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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 12
Reputation: ohnbabygal is an unknown quantity at this point 
Solved Threads: 0
ohnbabygal ohnbabygal is offline Offline
Newbie Poster

Re: printing a linked list

 
0
  #3
Oct 8th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: printing a linked list

 
0
  #4
Oct 9th, 2004
Why not just change getNext like so:
  1. // Untested
  2. bool getNext ( LIST& list, int& dataOut )
  3. {
  4. if ( list.pos == NULL )
  5. return false;
  6.  
  7. dataOut = list.pos->data;
  8. list.pos = list.pos->link;
  9.  
  10. return true;
  11. }
Then you can write printList like so:
  1. // Untested
  2. void printList ( LIST& list )
  3. {
  4. int data;
  5.  
  6. list.pos = list.head;
  7.  
  8. while ( getNext ( list, data ) )
  9. cout<< data <<endl;
  10. }
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 12
Reputation: ohnbabygal is an unknown quantity at this point 
Solved Threads: 0
ohnbabygal ohnbabygal is offline Offline
Newbie Poster

Re: printing a linked list

 
0
  #5
Oct 9th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: printing a linked list

 
0
  #6
Oct 9th, 2004
>Thank you so much!
Anytime.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC