| | |
printing a linked list
![]() |
•
•
Join Date: Sep 2004
Posts: 12
Reputation:
Solved Threads: 0
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;
}
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;
}
Let's look more closely at this:
Notice how you set list.count to 0 and that the fromWhere argument is 0. Then in getNext:
Both fromWhere and list.count are 0. Now back to printList:
moreData is not true because you set list.count and fromWhere to 0. So the loop is skipped and the list is not printed.
C++ Syntax (Toggle Plain Text)
list.count=0; moreData=getNext(list,0,pos);
C++ Syntax (Toggle Plain Text)
if(fromWhere==0) { if(list.count==0) success=false;
C++ Syntax (Toggle Plain Text)
while(moreData==true) { list.count++;
I'm here to prove you wrong.
•
•
Join Date: Sep 2004
Posts: 12
Reputation:
Solved Threads: 0
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
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
Why not just change getNext like so:
Then you can write printList like so:
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.
C++ Syntax (Toggle Plain Text)
// Untested bool getNext ( LIST& list, int& dataOut ) { if ( list.pos == NULL ) return false; dataOut = list.pos->data; list.pos = list.pos->link; return true; }
C++ Syntax (Toggle Plain Text)
// Untested void printList ( LIST& list ) { int data; list.pos = list.head; while ( getNext ( list, data ) ) cout<< data <<endl; }
I'm here to prove you wrong.
![]() |
Similar Threads
- how to create linked list? (C)
- Linked List: Returning value and inserting node (C++)
- Linked List: Search and Modify Help (C++)
- need help with Linked list (C++)
- Memory refrence error when printing linked list (C)
Other Threads in the C++ Forum
- Previous Thread: a little help
- Next Thread: C++ for Dummies Programming Book
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code codesamplerunwhilecommands coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph guess gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker listing loop looping loops map math matrix memory multiple news node output pointer port problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






