Question

Write a program that will read the information from a file into a list and then...

Write a program that will read the information from a file into a list and then display the list to the screen.
Remove the fifth item in the list and display the list again. Ask the program user for an entry into
the list and add it to the list. Display the list one last time.

disneyin.txt file

daisy   123
donald   345
goofy   654
mickey   593
minnie   489
daffy   432
pluto   765
huey   321
dewey   987
lewey   554
porky   333
buggs   778

list.h

#ifndef LIST_H_
#define LIST_H_


template <typename BaseData>
class List
{
//template <typename ifstream, typename empData>
//friend ifstream &operator>>(ifstream &input, empData temp);
//template <typename ostream, typename inStruct>
//friend ostream &operator<<(ifstream& output, empData inStruct);

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

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;
}
//----------------i added this-----------------
//extraction/insertion operators have to be a friend function
/*
template <typename ifstream, typename empData>
ifstream &operator>>(ifstream &input, empData temp)
{
   //input >> "take in the employee name"
   input >> temp.empName;
   //input >> "take in the employee ID num"
   input >> temp.empID;
  
return temp;
}

template <typename ostream, typename empData>
ifstream &operator<<(ostream &output, empData inStruct)
{
  
   //output << inStruct.empName;
   //output << inStruct.empID;
  
return inStruct.empName << inStruct.empId;
}
*/

#endif

Homework Answers

Answer #1

Rawcode:

1.list.cpp

#include "list.h"

2.lab4_client_driver.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "list.h"

struct Disney
{
   string name;
   int 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;
   }

   Disney data;
   List<string> name;
   List<int> id;

   string *nameP;
   int *idP;

   inFile >> data.name >> data.id;
   while (inFile){

       name.insertAfter(data.name);
       id.insertAfter(data.id);

       nameP = name.examine();
       idP = id.examine();
       cout << *nameP << " " << *idP << endl;

       inFile >> data.name >> data.id;
   }
   inFile.close();


   cout << endl;

   cout << "After deleting the fifth element:" << endl;

   inFile.open(fileName.c_str());

   if (inFile){
       name.makeCurrent(5);
       nameP = name.examine();
       name.remove();
       id.makeCurrent(5);
       idP = id.examine();
       id.remove();

   }

   name.first();
   id.first();
   inFile >> data.name >> data.id;
   while (inFile){
       nameP = name.examine();
       idP = id.examine();
      
       if (name.current() != name.count())
       {
           cout << *nameP << " " << *idP << endl;
           name.next();
           id.next();
       }

       else{
           cout << *nameP << " " << *idP << endl;
           break;
       }

       inFile >> data.name >> data.id;
   }
   inFile.close();

   cout << endl;

   cout << "Please enter the name and id: ";
   cin >> data.name >> data.id;

   inFile.open(fileName.c_str());

   if (inFile){
       name.last();
       name.insertAfter(data.name);
       id.last();
       id.insertAfter(data.id);
   }

   name.first();
   id.first();
   inFile >> data.name >> data.id;
   while (inFile){
       nameP = name.examine();
       idP = id.examine();
       cout << *nameP << " " << *idP << endl;
       if (name.current() != name.count()){
           name.next();
           id.next();
       }
       inFile >> data.name >> data.id;
   }
   inFile.close();

   return 0;
}

code in my editor:

output:

Please enter your file name : disneyin.txt
daisy 123
donald 345
goofy 654
mickey 593
minnie 489
daffy 432
pluto 765
huey 321
dewey 987
lewey 554
porky 333
buggs 778
After deleting the fifth element :
daisy 123
donald 345
goofy 654
mickey 593
daffy 432
pluto 765
huey 321
dewey 987
lewey 554
porky 333
buggs 778
Please enter the name and id : chip 178
daisy 123
donald 345
goofy 654
mickey 593
daffy 432
pluto 765
huey 321
dewey 987
lewey 554
porky 333
buggs 778
chip 178
Press any key to continue . . .

Please do up vote.

Thanks.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
in c++ Each of the member functions in WrongCode.cpp has errors in the way it performs...
in c++ Each of the member functions in WrongCode.cpp has errors in the way it performs a linked list operation. Find as many mistakes as you can //--------------------one void NumberList::appendNode(double num) { ListNode *newNode, *nodePtr; // Allocate a new node & store num newNode = new listNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node. if (!head) head = newNode; else // Otherwise, insert newNode. { // Find the last...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...