Hi, I have two seperate programs below writing a short program for each. My programs run, but sometimes when I rearrange things in my program to try to get it to work, I will receive an error. I am tried to figure it out since last night, but I am unable to. Any help is appreciate. Thank you
I am currently having problems and need help with:
1) Printing the words in reverse order in my printList function
2) Printing the words in alphabetical order in my printList function
Example of reverse order:
Input File:
hi
hello
hey
bye
adios
Example Output
adios
bye
hey
hello
hi
Example of alphabetical order
Input File:
hi
hello
hey
bye
adios
Example Output
adios
bye
hello
hey
hi
Reverse order
#include <fstream>
using namespace std;
struct nodeType
{
string info;
nodeType *link;
};
void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first, nodeType*& last);
int main()
{
nodeType *first, *last;
string words;
ifstream inData;
ofstream outData;
createList(first,last);
printList(first,last);
system("PAUSE");
return 0;
}
void createList(nodeType*& first, nodeType*& last)
{
ifstream inData("input.txt");
string words;
int emptyList;
nodeType *newNode;
first = NULL;
last = NULL;
while(inData >> words)
{
newNode = new nodeType; // create new node
newNode->info = words;
newNode->link = NULL;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
}
}
void printList(nodeType*& first,nodeType*& last) // print words in reverse order
{
ofstream outData("output.txt");
nodeType *current=first;
nodeType *reverse = NULL;
last=current->link;
current->link = reverse;
reverse = current;
current = last;
while (current != NULL)
{
outData << current->info <<endl;
current = current->link;
}
}
Alphabetical Order
#include <fstream>
using namespace std;
struct nodeType
{
string info;
nodeType *link;
};
void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first, nodeType*& last);
int main()
{
nodeType *first, *last;
string words;
ifstream inData;
ofstream outData;
createList(first,last);
printList(first,last);
system("PAUSE");
return 0;
}
void createList(nodeType*& first, nodeType*& last)
{
ifstream inData("input.txt");
string words;
int emptyList;
nodeType *newNode;
first = NULL;
last = NULL;
while(inData >> words)
{
newNode = new nodeType; // create new node
newNode->info = words;
newNode->link = NULL;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
}
}
void printList(nodeType*& first,nodeType*& last) // print words in alphabetical order
{
ofstream outData("output.txt");
nodeType *current=first;
nodeType *alphab = NULL;
first=current->link;
current->link = alphab;
last = current;
alphab= last;
while (current != NULL)
{
outData << current->info <<endl;
current = current->link;
}
}