I have a program that is supposed to read a text file called a3.txt and reads wh
ID: 3836293 • Letter: I
Question
I have a program that is supposed to read a text file called a3.txt and reads whats in it and uses the function that the word corresponds to in the program. The program builds a linked list. I have most of the functions down but the writeList and the writeNode in the list.h header listed below i'm drawing a blank on. The writeList will just print out what was put on the list based on what the user put in the a3.txt and the writeNode will write a specific node that is chosen. These are the two blank functions in the list.h below that i can't get.
Here's the a3.txt:
%insert Noah 99
%insert Emma 100
%insert Liam 75
%insert Olivia 82
%insert Mason 87
%insert Sophia 95
%insert Jacob 79
%insert Ava 97
%insert William 89
%insert Isabella 77
%insert Ethan 91
%insert Mia 94
%insert James 88
%insert Abigail 98
%write
%erase William
%erase Isabella
%erase Michael
%write
%search Noah
%search Emma
%search Michael
%sort
%write
%end
Here's the main program that reads that file:
#include
#include
#include
#include
#include
#include "node.h"
#include "llist.h"
using namespace std;
int main()
{
// stream to input instructions and information
ifstream instrFile;
// open file with list of people and their data
instrFile.open("a3.txt");
if (!instrFile)
{
cout << "Cannot open file 'a3.txt'" << endl;
}
string instruction, name, tempStr;
int score;
node *currNode;
while(!instrFile.eof())
{
getline(instrFile, instruction, ' ');
if (instruction == "%insert")
{
getline(instrFile, name, ' ');
getline(instrFile, tempStr, ' ');
const char* temp = tempStr.c_str();
score = atoi(temp);
cout << instruction << " " << name << " " << score << endl;
//your code
}
else if (instruction == "%erase")
{
getline(instrFile, name, ' ');
cout << instruction << " " << name << endl;
//your code
}
else if (instruction == "%search")
{
getline(instrFile, name, ' ');
cout << instruction << " " << name << endl;
//your code
}
else if (instruction == "%write")
{
instrFile.ignore(100, ' ');
cout << instruction << endl;
//your code
}
else if (instruction == "%sort")
{
instrFile.ignore(100, ' ');
cout << instruction << endl;
//your code
}
else if (instruction == "%end")
{
break;
}
}
int hold;
cin >> hold;
}
Here's the list.h header where the two functions are missing:
#ifndef LLIST_H_INCLUDED
#define LLIST_H_INCLUDED
#include
#include "node.h"
using namespace std;
class llist
{
public:
//default constructor
llist();
//destructor
~llist();
//Inserts node at before node at pointer currNode
node *insert(node *currNode, string name, int score);
//Uses insert function to add names and scores to the in alphabetical order
void insertInOrder(string name, int score);
//Search for a name and print it out
node *search(string name);
//Erase a name from the list
void erase(node *currNode);
//Write out the current list
void writeList();
//Write out a single node (name and score)
void writeNode(node *currNode);
//Swap two nodes in the list used by sort
void swapNode(node *nodeA, node *nodeB);
//Sort the by scores in ascending order
void sortAssend();
node *header;
private:
int listSize;
};
llist::llist()
{
header = new node;
header->prev = header;
header->next = header;
listSize = 0;
}
llist::~llist()
{
node *currNode, *tempNode;
currNode = header->next;
while(currNode != header)
{
tempNode = currNode;
currNode = currNode -> next;
delete tempNode;
}
delete header;
}
void llist::writeList()
{
cout << endl << "list" << endl;
node *currNode;
currNode = header->next;
while(currNode != header)
{
cout << setw(10) << currNode->name << " " << currNode->score <
currNode = currNode->next;
}
cout << endl;
}
node *llist::insert(node *currNode, string name, int score)
{
node *newNode, *prevNode;
prevNode = currNode->prev;
newNode = new node;
newNode->name = name;
newNode->score = score;
++listSize;
newNode->prev = currNode->prev;
newNode->next = prevNode->next;
prevNode->next = newNode;
currNode->prev = newNode;
return newNode;
}
void llist::insertInOrder(string name, int score)
{
node *currNode;
currNode = header->next;
while(currNode != header)
{
if(name < currNode->name)
{
insert(currNode, name, score);
break;
}
currNode= currNode-> next;
}
if (currNode == header)
{
insert(currNode, name, score);
}
}
node *llist::search(string name)
{
node *currNode;
currNode = header->next;
while(currNode != header)
{
if(currNode->name == name)
{
return currNode;
break;
}
currNode = currNode->next;
}
return NULL;
}
void llist::erase(node *currNode)
{
node *prevNode, *succNode;
prevNode = currNode->prev;
succNode = currNode->next;
prevNode->next = succNode;
succNode->prev = prevNode;
delete currNode;
--listSize;
}
void llist::writeList()
{
}
void llist::writeNode(node *currNode)
{
}
void llist::swapNode(node *nodeA, node *nodeB)
{
string tempName;
int tempScore;
tempName = nodeA->name;
tempScore = nodeA->score;
nodeA->name = nodeB->name;
nodeA->score = nodeB->score;
nodeB->name = tempName;
nodeB->score = tempScore;
}
void llist::sortAssend()
{
node *passNode, *smallNode, *jNode;
int pass, j;
passNode= header->next;
for(pass = 0; pass < listSize-1;pass++)
{
smallNode = passNode;
jNode = passNode->next;
for(j= pass+1;j {
if(jNode->score < smallNode->score)
{
smallNode = jNode;
}
jNode = jNode->next;
}
if(smallNode != passNode)
{
swapNode(passNode,smallNode);
//passNode = smallNode; FOR OTHER WAY OF SWAPPING
}
passNode= passNode->next;
}
}
#endif // LLIST_H_INCLUDED
Here's the simple node.h header:
#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
#include
using namespace std;
struct node
{
string name;
int score;
node *prev;
node *next;
};
#endif // NODE_H_INCLUDED
SAMPLE OUTPUT BASED ON THE a3.txt: ( This is what comes out after the first %write)
Noah 99
Emma 100
Liam 75
Olivia 82
Mason 87
Sophia 95
Jacob 79
Ava 97
William 89
Isabella 77
Ethan 91
Mia 94
James 88
Abigail 98
After the second %write:
Noah 99
Emma 100
Liam 75
Olivia 82
Mason 87
Sophia 95
Jacob 79
Ava 97
Ethan 91
Mia 94
James 88
Abigail 98
Explanation / Answer
void llist::writeList()
{
cout << endl << "list" << endl;
if(header != null) {
node *currNode;
currNode = header;
while(currNode->next != header)
{
cout << setw(10) << currNode->name << " " << currNode->score << endl;
currNode = currNode->next;
}
cout << endl;
}
}
void llist::writeNode(node *currNode)
{
cout << endl << "Node" << endl;
if(currNode != null) {
cout << setw(10) << currNode->name << " " << currNode->score << endl;
}
}
Here are the two functions which you asked for. Looks like your code has some part missing. Can you please check with my code. in case of any issue.. do let me know in comments. I will help you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.