Hello, now, i am creating a program to create 3 lists. (List1, list2, list3)
my program will read from a txt file which contains letters.
for example:
my goal is to create 2 identical list, each node contain one letter. After that, i have to put each letter from my list2 to list3, but in reverse order.Code:a b c d
therefore, list 1 should contain:
abcd
list 2 should contains
abcd
and list 3 should contains
dcba
Here is my program, i think i have some logical error in my "insertFront" funcion..
Code:#include <iostream.h> #include <fstream.h> #include <stdlib.h> struct NodeType { char letter; NodeType* link; }; char DelBeginning(NodeType&); void insertFront(NodeType&, char); int main() { NodeType* head1, *current1, *newptr1, *head2, *current2, *newptr2; ifstream myFile; int count=0; char value, letter; myFile.open("ulist.txt"); if (myFile.fail()) { cout<<"Cannot open file"; exit(-1); } head1 = new NodeType; myFile>>head1->letter; head2 = new NodeType; head2->letter=head1->letter; myFile>>value; current1=head1; current2=head2; while (!myFile.eof()) { newptr1=new NodeType; newptr1->letter=value; newptr2=new NodeType; newptr2->letter=newptr1->letter; current1->link=newptr1; current1=newptr1; current2->link=newptr2; current2=newptr2; myFile>>value; count++; } count++; current1->link=NULL; current2->link=NULL; for (int i=0; i<count; i++) { letter=DelBeginning(head2); insertFront(head2,letter); } myFile.close(); return 0; } char DelBeginning(NodeType& head) //Remove an item from the front and returns it { char value; value=head->letter; NodeType* current=head->link; delete head; head=current; return (value); } void insertFront(NodeType& head, char letter) //Inserts the item at the front of list3 { NodeType* newptr, * head3; newptr=new NodeType; newptr->item=value; if (head==NULL) { newptr->link=NULL; head3=newptr; } else { newptr->link=head3; head3=newptr; } }



LinkBack URL
About LinkBacks


