Question

In: Computer Science

PROBLEM STATEMENT: Using the list container from the STL, write a program that will read the...

PROBLEM STATEMENT:
Using the list container from the STL, write a program that will read the information from a file into a list
and then display the list to the screen. Add your name. Sort the list by id. Print the list again
CODE:
Use the provided disneyin2.txt file.
Do not hard code the file name; get file name from user.
Use the struct below
struct student
{
char firstnm[20],
lastnm[20];
int id,
grade;
};
You are to create a list of data type student : std::list<student> l;
The driver is to demonstrate that the above mentioned operations are performed.
Overload the << and >> operators so you may easily input and output a student.

disneyin2.txt

Hewey Duck 123 90
Daffy Duck 342 92
Wiley Coyote 432 89
Goofy Dog 654 95
Daisy Duck 145 92
Sylvester PuddyCat 775 86
Tweety Bird 221 87
Mickey Mouse 666 66

list.t

#ifndef LIST_T_
#define LIST_T_


  
template <class BaseData>
List <BaseData>::List()
{
previous = 0;
currentNode = 0;
head = 0;
numNodes = 0;
currentPos = 0;
}

template <class BaseData>
List <BaseData>::List(List<BaseData> &init)
{
if (this == &init) return;
ListNode *newList, *current, *newNode;
current = init.head;
newList = 0;
head = 0;
while (current)
{
newNode = new ListNode;
newNode->listData = current->listData;
newNode->link = 0;

if (newList)
{
newList->link = newNode;
newList = newList->link;
}

else newList = newNode;

if (current == init.head)
head = newNode;
current = current->link;
}

numNodes = init.numNodes;
currentPos = 0;
previous = 0;
currentNode = 0;
}

template <class BaseData>
void List <BaseData>::insertBefore(const BaseData &item)
{
ListNode *p;
p = new ListNode;
p->listData = item;
if (numNodes)
{
if (head == currentNode) head = p;
p->link = currentNode;
if (previous) previous ->link = p;
++numNodes;
currentNode = p;
}
else
{
head = p;
p->link = 0;
previous = 0;
++numNodes;
currentNode = p;
}
}

template <class BaseData>
BaseData * List<BaseData>::examine()
{
BaseData *temp;
if (currentNode)
{
temp = new BaseData;
*temp = currentNode->listData;
return (temp);
}
else
return 0;
}

template <class BaseData>
List <BaseData>::~List()
{
destroy();
}

template <class BaseData>
void List<BaseData>::destroy()
{
ListNode *temp;
currentNode = head;
while (currentNode)
{
temp = currentNode;
currentNode = currentNode->link;
delete temp;

}

previous = 0;
currentNode = 0;
head = 0;
numNodes = 0;
currentPos = 0;
}

template <class BaseData>
void List <BaseData>::first()
{
if (numNodes)
{
previous = 0;
currentNode = head;
currentPos = 1;
}
else
currentPos = 0;
}

template <class BaseData>
void List <BaseData>::last()
{
while (currentNode->link)
{
previous = currentNode;
currentNode = currentNode->link;
}
currentPos = numNodes;
}


template <class BaseData>
void List<BaseData>::makeCurrent (int position)
{
if (( position < 1) || (position > numNodes))
cout << "invalid position: "<< endl;
else
{
first();
for (int i = 1; i < position; i++)
{
previous = currentNode;
currentNode = currentNode->link;
}
currentPos = position;
}
}

template <class BaseData>
void List<BaseData>::prev()
{
int tempCurrPos = currentPos;
if (currentPos > 1)
{
ListNode *temp = previous;
first();
if (currentNode == temp)
{
previous = 0;
currentNode = temp;
}
else
{
while (currentNode->link != temp)
currentNode = currentNode->link;
previous = currentNode;
currentNode = temp;
}
currentPos = tempCurrPos -1;
}
else
{
cout << "walking over front of list";
currentPos = 0;
}

}

template <class BaseData>
void List<BaseData>::next()
{
if (currentNode->link)
{
previous = currentNode;
currentNode = currentNode->link;
currentPos++;
}
else
{
cout << "walking over end of list";
currentPos = 0;
}
}

template <class BaseData>
int List<BaseData>::current()
{
return (currentPos);
}

template <class BaseData>
int List<BaseData>::count()
{
return (numNodes);
}

template <class BaseData>
void List<BaseData>::insertAfter(const BaseData &item)
{
ListNode *p;
p = new ListNode;
p->listData = item;
if (numNodes)
{
p->link = currentNode->link;
currentNode->link = p;
++numNodes;
previous = currentNode;
currentNode = p;
currentPos++;
}
else
{
head = p;
p->link = 0;
previous = 0;
++numNodes;
currentNode = p;
currentPos++;
}
}

template <class BaseData>
void List<BaseData>::remove()
{
ListNode *p, *temp;
p = currentNode;
if (numNodes)   //there are nodes
{if (previous)   //this is not the first node in the list
{   //any other node in list but first
previous->link = currentNode->link;
if (currentNode->link != 0)
currentNode = currentNode->link;
else   //deleting last node in list
{
currentPos--;
currentNode = previous;
temp = head;
if (temp == currentNode)
previous = 0;
else
{
while (temp->link != currentNode && temp)
temp = temp->link;
previous = temp;
}
}
delete p;
--numNodes;
}
else
{   //delete first node in list
head = head->link;
delete p;
currentNode = head;
--numNodes;

//if first and last node in list
if (!numNodes) currentPos = 0;
}
}
else cout << "empty list" << endl;
}


template <class BaseData>
void List<BaseData>::replace(BaseData &item)
{
if (currentNode)
currentNode->listData = item;
}


template <class BaseData>
List<BaseData>& List<BaseData>:: operator = (List<BaseData> &init)
{
if (this == &init) return *this;

ListNode *temp, *newList, *current, *newNode;
currentNode = head;
while (currentNode) //delete existing left side list
{
temp = currentNode;
currentNode = currentNode->link;
delete temp;
}

current = init.head;
newList = 0;
while (current) //copy list
{ newNode = new ListNode;
newNode->listData = current->listData;
newNode->link = 0;
if (newList)
{
newList->link = newNode;
newList = newList->link;
}
else newList = newNode;
if (current == init.head)
head = newNode;
current = current->link;
}

numNodes = init.numNodes;
currentPos = 0;
previous = 0;
currentNode = 0;
return *this;
}


#endif

list.h

#ifndef LIST_H_
#define LIST_H_

#include <iostream>
#include <string>
using std::cout;
using std::endl;
using namespace std;

template <class BaseData>
class List
{
protected:
struct ListNode
{
public:
BaseData listData;
ListNode* link;
};

public:

List();
List(List& init);
~List();
void first();
void last();
void makeCurrent(int position);
void prev();
void next();
int current();
int count();
void insertBefore(const BaseData& item);
void insertAfter(const BaseData& item);
void remove();
void replace(BaseData& item);
BaseData* examine();
List<BaseData>& operator = (List<BaseData>& source);
void destroy();

protected:

ListNode* head, * currentNode, * previous;
int numNodes;
int currentPos;

};

#include "list.t"
#endif

Solutions

Expert Solution

lab5_client_driver.cpp:

#include <iostream>
#include <fstream>
#include <list>
#include <string>

using namespace std;

struct student
{
   char firstnm[20], lastnm[20];
   int id, grade;

   bool operator < (const student& rhs)
   {
       return id < rhs.id;
   }
};

int main(){
   ifstream inFile;

   string fileName;

   cout << "Please enter your file name:";
   cin >> fileName;

   inFile.open(fileName.c_str());

   if (!inFile){
       cout << "cannot open the input file" << endl;
       return 1;
   }

   student data;
   std::list<student>l;

   inFile >> data.firstnm >> data.lastnm >> data.id >> data.grade;
  
   while (inFile){

       l.push_back(data);
      
       inFile >> data.firstnm >> data.lastnm >> data.id >> data.grade;
   }
   inFile.close();

   std::list<student>::iterator i;
   for (i = l.begin(); i != l.end(); ++i)
       cout << (*i).firstnm << " " << (*i).lastnm << " " << (*i).id << " " << (*i).grade << endl;
  
   cout << "enter a first name: " << endl;  
   cin >> data.firstnm;
   cout << "enter a last name: " << endl;
   cin >> data.lastnm;
   cout << "enter an id number: " << endl;
   cin >> data.id;
   cout << "enter a grade: " << endl;
   cin >> data.grade;

   l.push_back(data);
   l.sort();

   cout << endl << "The sorted list by id number:" << endl;
   for (i = l.begin(); i != l.end(); ++i)
       cout << (*i).firstnm << " " << (*i).lastnm << " " << (*i).id << " " << (*i).grade << endl;
}

Code in my editor:

output:

please do up vote.

Thanks.


Related Solutions

Problem Write a movie management system using object-oriented design principles. The program will read from the...
Problem Write a movie management system using object-oriented design principles. The program will read from the supplied data file into a single array list. The data file (movies.txt) contains information about the movies. Each movie record has the following attributes: - Duration (in minutes) - Title - Year of release Each record in the movies.txt file is formatted as follows: - Duration,Title,Year - e.g.: 91,Gravity,2013 Specifically, you have to create an interactive menu driven application that gives the user the...
Problem Statement You are required to read in a list of stocks from a text file...
Problem Statement You are required to read in a list of stocks from a text file “stocks.txt” and write the sum and average of the stocks’ prices, the name of the stock that has the highest price, and the name of the stock that has the lowest price to an output file. The minimal number of stocks is 30 and maximal number of stocks in the input file is 50. You can download a input file “stocks.txt” from Canvas. When...
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Write a program using C to read a list of your friend names which ends by...
Write a program using C to read a list of your friend names which ends by the word end. The program builds a linked list using these names and prints the names in the order stored into the linked list The list can be created using insertion at the beginning or insertion at the end; Use switch case to select the type of insertion; Case 1:insertion in the beginning; Case2:insertion in the end. Once the list is printed after insertion;...
Write a program that uses a while statement to read integers from the user and prints...
Write a program that uses a while statement to read integers from the user and prints the sum, average, and largest of these numbers. The input should terminate if the user enters 999. The average should be output with 4 digits of precision. c++ please ASAP
Java Complete Example.java to fulfill the following problem statement: Write a program to accept a list...
Java Complete Example.java to fulfill the following problem statement: Write a program to accept a list of numbers, one per line. Input is provided from a file if one is provided as the first command line argument. If a file is not provided, input should be read from the terminal. The numbers can either be whole numbers or floating point numbers. The program should continue to accept input until a number equal to 00 is input. Of course, if the...
Problem description Write a program that uses a loop to read integer values from the standard...
Problem description Write a program that uses a loop to read integer values from the standard input stream. Each non-zero value, x, is the first number of a sequence. Define a0 = x; an+1 = an / 2 if an is even; an+1 = 3 * an + 1 if an is odd. Then there exists an integer k such that ak = 1. For each non-zero value of x, the program outputs the integer k such that ak =...
Discuss tradeoffs between using the C++ STL list and vector.
Discuss tradeoffs between using the C++ STL list and vector.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT