Question
Using C++ I keep getting message: 'head does not name a type' 'teal does not name a type' Where do I place this code in
Using C++
I keep getting message:
'head does not name a type'
'teal does not name a type'
Where do I place this code in my program:
/*search and delete with respect to location as current, head and tail //define below 2 lines in LinkedList() function
head = new NodeType; tail = new NodeType;
void UnsorderedType::DeleteItem(ItemType item) { NodeType *tempLocation, *location; bool stop = false;
if(!isEmpty()) { location = head; tempLocation = head->link; while (templocation != tail && !stop) { if (templocation->info == item) stop = true; else { location = templocation; templocation = templocation->link; } } if (!stop) cout << "The node to delete is not in the list!" << endl; else { location->link = templocation->link; delete templocation; count--; } } else { cout << "The list is empty!" << endl; } }*/
/Problem and code:
Implement the UnsortedList class to store a list of strings that are input into the list from data2.txt.
- create a main.cpp file that gets the numbers from the file
- insert the word "cat" into the list
- insert another word "antibacterial" into the list
- delete the word "letter" from the list
- print out the following:
--the entire list
- the greatest
- the least
2. Attach the main.cpp, UnsortedList.cpp, the ItemType.h, and the output file one called outfile1.txt
- Yes you need to make your program output an "outfile1.txt"
3. Implement the UnsortedList class to store a list of numbers that are input into the list from data.txt.
- create a main.cpp file that gets the numbers from the file
- insert the number 7 into the list
- insert another number 300 into the list
- delete the number 6 from the list
- print out the following:
--the entire list
- the greatest
- the least
2. Attach the main.cpp, UnsortedList.cpp, the ItemType.h, and the output file two called outfile2.txt
- Yes you need to make your program output an "outfile2.txt"
data.txt
super formula travel free thick Josephine Clara education
data2.txt
super formula travel free thick Josephine Clara education
//My main.cpp
//--------------------------------------------------------------
// Test driver for Linked List UnsortedType list
// Navarr Barnier
// Your class CS3350 TTh 1:00
// Due date: Thursday, September 13, 2012
//
// Compile command: g++ hw2.cpp ch03-UnsortedType.cpp ch03-ItemType.cpp
// Input file name: hw2.txt
// Contains list of commands to add items to list, split original
// list into two lists, print each list and get the length of
// each list.
// Output: The result of each command is displayed on the screen.
// Filename: hw2.cpp
//--------------------------------------------------------------
#include
#include
#include
#include
#include
#include "ItemType.h"
using namespace std;
void PrintList(UnsortedType&);
void SplitList(UnsortedType&, ItemType);
ItemType GetItem(ItemType& item, bool& found);
int main()
{
ifstream inFile; // file containing operations
ofstream outFS; // Output file stream
string data; // operation to be executed
string command; // operation to be executed
int count = 0;
ItemType item;
UnsortedType list;
bool found;
//int numCommands;
//----------------------------------------
// Open input file with commands for testing
// list operations, check success of open
//----------------------------------------
inFile.open("data2.txt");
if (!inFile)
{
cout << "Unable to open input file - ending program." << endl;
return 1;
}
//----------------------------------------
// Read in and process commands to apply to list
//----------------------------------------
//inFile >> data;
//numCommands = 0;
while (!inFile.eof())
{
//numCommands++;
//cout << "Command " << numCommands << ": " << command << " ";
//----------------------------------------
// PutItem
//----------------------------------------
inFile >> data;
item.Initialize(data);
list.PutItem(item);
++count;
item.Print();
cout << ", " << "added to list" << endl;
}
inFile.close();
cout << " " << count << " words total in list." << " ";
inFile.open("hw6.txt");
if (!inFile)
{
cout << "Unable to open input file - ending program." << endl;
return 1;
}
//----------------------------------------
// Read in and process commands to apply to list
//----------------------------------------
inFile >> command;
//numCommands = 0;
while (command != "Quit")
{
// numCommands++;
// cout << "Command " << numCommands << ": " << command << " ";
//----------------------------------------
// PutItem
//----------------------------------------
if (command == "PutItem")
{
inFile >> data;
item.Initialize(data);
list.PutItem(item);
item.Print();
cout << " added to list" << endl;
}
//----------------------------------------
// GetItem
//----------------------------------------
else if (command == "GetItem")
{
inFile >> data;
item.Initialize(data);
item = list.GetItem(item, found);
item.Print();
if (found)
cout << " found in list." << endl;
else
cout << " not in list." << endl;
}
//----------------------------------------
// DeleteItem
//----------------------------------------
else if (command == "DeleteItem")
{
inFile >> data;
item.Initialize(data);
list.DeleteItem(item);
item.Print();
cout << " deleted from list" << endl;
}
//----------------------------------------
// GetLength
//----------------------------------------
else if (command == "GetLength")
cout << "Length of list = " << list.GetLength() << endl;
//----------------------------------------
// IsFull
//----------------------------------------
else if (command == "IsFull")
if (list.IsFull())
cout << "List is full." << endl;
else
cout << "List is not full." << endl;
//----------------------------------------
// MakeEmpty
//----------------------------------------
else if (command == "MakeEmpty")
{ list.MakeEmpty();
cout << "List is empty." << endl;
}
//----------------------------------------
// PrintList
// Non-member function to print list of items
//----------------------------------------
else if (command == "PrintList")
{
cout << " List values" << endl;
PrintList(list);
}
//----------------------------------------
// SplitList
// Split the list
//----------------------------------------
else if (command == "SplitList")
{
inFile >> data;
item.Initialize(data);
//SplitList(list,item);
}
//----------------------------------------
// Invalid command
//----------------------------------------
else
cout << command << " is not a valid command." << endl;
inFile >> command;
};
cout << "Testing completed." << endl;
inFile.close();
/*//----------------------------------------
// PutItem
//----------------------------------------
if (command == "PutItem")
{
inFile >> data;
item.Initialize(data);
list.PutItem(item);
item.Print();
cout << " added to list" << endl;
}
//----------------------------------------
// GetItem
//----------------------------------------
else if (command == "GetItem")
{
inFile >> data;
item.Initialize(data);
item = list.GetItem(item, found);
item.Print();
if (found)
cout << " found in list." << endl;
else
cout << " not in list." << endl;
}
//----------------------------------------
// DeleteItem
//----------------------------------------
else if (command == "DeleteItem")
{
inFile >> data;
item.Initialize(data);
list.DeleteItem(item);
item.Print();
cout << " deleted from list" << endl;
}
//----------------------------------------
// PrintList
// Non-member function to print list of items
//----------------------------------------
else if (command == "PrintList")
{
cout << " List values" << endl;
PrintList(list);
}
*/
return 0;
}
//----------------------------------------
// PrintList
// Non-member function to print all items in list
// Pre: list has been initialized
// Post: Each component in list has been written to cout
//----------------------------------------
void PrintList(UnsortedType &list)
{
int length;
ItemType item;
list.ResetList();
length = list.GetLength();
for (int counter = 1; counter <= length; counter++)
{
item = list.GetNextItem();
item.Print();
cout << endl;
}
cout << "Length of list = " << length << endl << endl;
}
//My UnsortedList.cpp Code
//------------------------------------------------------
// ItemType
// Class Implementation File
// Ch. 3, C++ Plus Data Structures, Dale 5e, p. 155
// Filename: ch03-ItemType.cpp
//------------------------------------------------------
#include
#include
#include "ItemType.h"
using namespace std;
//------------------------------
// ItemType
// default constructor
//------------------------------
ItemType::ItemType()
{ value = ""; }
//------------------------------
// ComparedTo
// Compares one ItemType object to another. Returns
// LESS, if self "comes before" item
// GREATER, if self "comes after" item
// EQUAL, if self and item are the same
//------------------------------
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
//------------------------------
// Initialize
//------------------------------
void ItemType::Initialize(string data)
{ value = data; }
//------------------------------
// Adds ItemType value to output stream
//------------------------------
void ItemType::Print() const
// pre: out has been opened.
// post: value has been sent to the stream cout.
{ cout << value; }
//------------------------------------------------------
// UnsortedType
// Linked List - Class Implementation File
// Your name
// Your class CS3350 classtime
// Due date: Thursday, September 13, 2012
//------------------------------------------------------
//---------------------------------------------
// Constructor
//---------------------------------------------
UnsortedType::UnsortedType()
{
length = 0;
listData = NULL;
}
//---------------------------------------------
// Destructor
// Deallocates all items in list
// Post: List is empty
// All items have been deallocated
//---------------------------------------------
UnsortedType::~UnsortedType()
{
NodeType* tempPtr;
// Loop removes all nodes from list
// deallocating space for each one
while(listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
}
//---------------------------------------------
// MakeEmpty
// Returns the list to the empty state
// Post: List is empty
//---------------------------------------------
void UnsortedType::MakeEmpty()
// Post: List is empty
{
NodeType* tempPtr;
// Loop removes all nodes from list
// deallocating space for each one
while(listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}
//---------------------------------------------
// IsFull
// Function: Determines whether list is full.
// Pre: List has been initialized.
// Post: Function value = (list is full)
// Returns: true if there is no room for another
// ItemType on the free store; false otherwise.
//---------------------------------------------
bool UnsortedType::IsFull() const
{
NodeType* location;
// Try adding a new node, if successful, there
// is room for more nodes so list is NOT full
try
{
location = new NodeType;
delete location;
return false;
}
// If adding a new node was unsuccessful,
// the list is full
catch(bad_alloc)
{
return true;
}
}
//---------------------------------------------
// GetLength
// Determines number of elements in list
// Pre: List has been initialized
// Post: Number of items in the list is returned
//---------------------------------------------
int UnsortedType::GetLength() const
{
return length;
}
//---------------------------------------------
// PutItem
// Adds item to list
// Pre: List has been initialized
// List is not full
// item is not in list
// Post: item is in list; length has been incremented
//---------------------------------------------
void UnsortedType::PutItem(ItemType item)
{
NodeType* location = new NodeType;
location->info = item;
location->next = listData;
listData = location;
length++; // Increment length of list
}
//---------------------------------------------
// GetItem
// Retrieves list element whose key matches item's key (if present)
// Pre: List has been initialized.
// Key member of item is initialized.
// Post: If there is an element someItem whose key matches
// item's key, then found = true and someItem is returned;
// otherwise found = false and item is returned unchanged.
// List is unchanged.
//---------------------------------------------
ItemType UnsortedType::GetItem(ItemType& item, bool& found)
{
bool moreToSearch;
NodeType* location;
location = listData;
found = false;
moreToSearch = (location != NULL);
while (moreToSearch && !found)
{
switch (item.ComparedTo(location->info))
{
case LESS :
case GREATER : location = location->next;
moreToSearch = (location != NULL);
break;
case EQUAL : found = true;
item = location->info;
break;
}
}
return item;
}
//---------------------------------------------
// DeleteItem
// Deletes the element whose key matches item's key.
// Pre: List has been initialized.
// Key member of item is initialized.
// One and only one element in list has a key
// matching item's key.
// Post: No element in list has a key matching item's key.
//---------------------------------------------
void UnsortedType::DeleteItem(ItemType item)
{
NodeType* location;
NodeType* tempLocation;
location = listData;
if (item.ComparedTo(location->info) == EQUAL)
{
tempLocation = location;
listData = listData->next;
}
else
{
while (!((item.ComparedTo((location->next)->info) == EQUAL)))
break;
location = location->next;
tempLocation = location->next;
location->next = (location->next)->next;
}
delete tempLocation;
length--;
}
//---------------------------------------------
// ResetList
// Initializes current position for an iteration through the list
// Pre: List has been initialized
// Post: Current position has been initialized
// and is prior to list
//---------------------------------------------
void UnsortedType::ResetList()
{
currentPos = NULL;
}
//---------------------------------------------
// GetNextItem
// Gets next element in list
// Pre: ResetList was called to initialize iteration
// No transformer has been executed since last call
// Current position is defined.
// Post: item is copy of element at current position
// Current position is updated to next position
// Returns: copy of next item in list
//---------------------------------------------
ItemType UnsortedType::GetNextItem()
{
if (currentPos == NULL)
currentPos = listData;
else
currentPos = currentPos->next;
return currentPos->info;
}
//My ItemType.h Code
//------------------------------------------------------
// ItemType
// Class Specification File
// Encapsulates type of items in list
// Ch. 3, C++ Plus Data Structures, Dale 5e, p. 154, 155
// same as code from Ch. 4 ItemType.h
// Filename: ch03-ItemType.h
//------------------------------------------------------
#include
using namespace std;
#ifndef ITEMTYPE_H
#define ITEMTYPE_H
const int MAX_ITEMS = 25;
enum RelationType {LESS, GREATER, EQUAL};
class ItemType
{
private:
string value;
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Print() const;
void Initialize(string data);
};
//------------------------------------------------------
// UnsortedType
// Linked List - Class Specification File
// Your name
// Your class CS3350 classtime
// Due date: Thursday, September 13, 2012
//
// Defines an unsorted list type whose elements are of ItemType
// Ch. 3, C++ Plus Data Structures, Dale 5e, p. 183-185
// Filename: hw2-UnsortedType.h
// File ch03-ItemType.h must be provided by the user of this class.
// It must contain the following definitions:
// MAX_ITEMS: the maximum number of items on the list
// RelationType: {LESS, GREATER, EQUAL}, an enumerated type
// ItemType: class with definition of the objects in the list
//------------------------------------------------------
class UnsortedType
{
private:
struct NodeType
{
ItemType info;
NodeType* next;
};
NodeType* listData; // Pointer to head of list
int length; // # of items (nodes) in list
NodeType* currentPos;
public:
UnsortedType(); // Constructor
~UnsortedType(); // Destructor
void MakeEmpty(); // Returns the list to the empty state
bool IsFull() const; // Determines whether list is full
int GetLength() const; // Determines the number of elements in list
ItemType GetItem(ItemType& item, bool& found);
// Retrieves list element whose key
// matches item's key (if present)
void PutItem(ItemType item); // Adds item to list
void DeleteItem(ItemType item); // Deletes element whose key
// matches item's key.
void ResetList(); // Initializes current position for
// an iteration through the list
ItemType GetNextItem(); // Gets the next element in list
//void SplitList(ItemType item, UnsortedType &list1, UnsortedType &list2);
// Splits a list into two based on the item
};
#endif // CH03-ITEMTYPE_H_INCLUDED
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started