Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to make a program that uses the functions from my llist.h file and uses t

ID: 3855837 • Letter: I

Question

I need to make a program that uses the functions from my llist.h file and uses them in the cases in main. Such as when it looks for the case %insert it uses that function from the llist.h file to insert that into the list and so on. All of the functions are built already but i need to use them in the main function and thats where i am unsure of what to do and how to do it.

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:

#ifndef LLIST_H_INCLUDED
#define LLIST_H_INCLUDED

#include <string>
#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 <<endl;

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::writeNode(node *currNode)
{
cout << endl << "Node" << endl;
if(currNode != NULL) {
cout << setw(10) << currNode->name << " " << currNode->score << endl;
}
}


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<listSize;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

#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;

// Set "curNode" value to NULL

*currNode = NULL;

// Create Object for llist class in list.h header file

llist obj;


   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

currNode = obj.insert(currNode, name, score);
}
else if (instruction == "%erase")
{
getline(instrFile, name, ' ');
cout << instruction << " " << name << endl;
//your code

// Search for element in list, ''search" returns the element position then erase it

currNode = obj.search(name);

obj.erase(currNode);
}
else if (instruction == "%search")
{
getline(instrFile, name, ' ');
cout << instruction << " " << name << endl;
//your code

// Search the list using the name given in the list if present do something else do another thing

boolean present = list.search(name);

// present is true if given name is presetn in list else false

}
else if (instruction == "%write")
{
instrFile.ignore(100, ' ');
cout << instruction << endl;
//your code

// Simply call writeList, it will print all the elements in the list

list.writeList();
}
else if (instruction == "%sort")
{
instrFile.ignore(100, ' ');
cout << instruction << endl;
//your code

//call sortAssend to sort in ascending order.

list.sortAssend();
}
else if (instruction == "%end")
{
break;
}
}


int hold;
cin >> hold;
}