PROBLEM STATEMENT:
Using the list container from the STL, write a program that willread the information from a file into a list
and then display the list to the screen. Add your name. Sort thelist 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 l;
The driver is to demonstrate that the above mentioned operationsare performed.
Overload the << and >> operators so you may easilyinput 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
List ::List()
{
previous = 0;
currentNode = 0;
head = 0;
numNodes = 0;
currentPos = 0;
}
template
List ::List(List &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
void List ::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
BaseData * List::examine()
{
BaseData *temp;
if (currentNode)
{
temp = new BaseData;
*temp = currentNode->listData;
return (temp);
}
else
return 0;
}
template
List ::~List()
{
destroy();
}
template
void List::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
void List ::first()
{
if (numNodes)
{
previous = 0;
currentNode = head;
currentPos = 1;
}
else
currentPos = 0;
}
template
void List ::last()
{
while (currentNode->link)
{
previous = currentNode;
currentNode = currentNode->link;
}
currentPos = numNodes;
}
template
void List::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
void List::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
void List::next()
{
if (currentNode->link)
{
previous = currentNode;
currentNode = currentNode->link;
currentPos++;
}
else
{
cout << \"walking over end of list\";
currentPos = 0;
}
}
template
int List::current()
{
return (currentPos);
}
template
int List::count()
{
return (numNodes);
}
template
void List::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
void List::remove()
{
ListNode *p, *temp;
p = currentNode;
if (numNodes)Â Â //there are nodes
{if (previous)Â Â //this is not the first node in thelist
{Â Â //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
void List::replace(BaseData &item)
{
if (currentNode)
currentNode->listData = item;
}
template
List& List:: operator =(List &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
#include
using std::cout;
using std::endl;
using namespace std;
template
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& operator = (List&source);
void destroy();
protected:
ListNode* head, * currentNode, * previous;
int numNodes;
int currentPos;
};
#include \"list.t\"
#endif