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.
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;
typedef NodeType* Nodeptr;
struct NodeType
{
char letter;
Nodeptr link;
};
char DelBeginning(Nodeptr&);
void insertFront(Nodeptr&, char);
Nodeptr head3;
int main()
{
Nodeptr 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(Nodeptr& head) //Remove an item from the front of list 2 and returns it to the main function
{
char value;
value=head->letter;
Nodeptr current=head->link;
delete head;
head=current;
return (value);
}
void insertFront(Nodeptr& head, char letter) //Inserts the item at the front of list3
{
Nodeptr newptr;
newptr=new NodeType;
newptr->letter=letter;
if (head==NULL)
{
newptr->link=NULL;
head3=newptr;
}
else
{
newptr->link=head3;
head3=newptr;
}
}
I think i will got a NULL in the beginning instead at the end.