Oct 16, 2015 at 12:14am UTC
list_driver.cpp
#include <cctype> // Provides toupper
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include <ctime>
using namespace std;
#include "List.h"
// PROTOTYPES:
void printMenu( );
// Postcondition: A menu of choices has been printed.
int main( )
{
List test; // A List to perform tests on
char choice; // Command entered by the user
cout << "I have initialized an empty list of integers." << endl;
do
{
printMenu( );
cout << "Enter choice: ";
cin >> choice;
choice = toupper(choice);
ElementType val;
int pos;
switch (choice)
{
case 'A':
for (int i = 0; i < 3; i++)
{
cout << "Enter the items to add: ";
cin >> val;
test.insert(val, test.getSize());
}
break;
case 'S':
cout<< "List size: "<< test.getSize()<< endl;
break;
case 'C':
break;
case 'O':
break;
case 'E':
cout <<"\nEmpty: "<< boolalpha << test.empty()<< endl;
break;
case 'P':
test.display(cout);
break;
case 'I':
cout << "Enter value: ";
cin >> val;
cout << "\nEnter position: ";
cin >> pos;
test.insert(val, pos);
break;
case 'F':
cout <<"Enter the data you want to find: ";
cin >> val;
cout << test.find(val);
break;
case 'R':
cout <<"\nEnter position: ";
cin >> pos;
test.erase(pos);
break;
case 'Q': cout << "Test program ended." << endl;
break;
default: cout << choice << " is invalid." << endl;
}
}
while ((choice != 'Q'));
return EXIT_SUCCESS;
}
void printMenu( )
{
cout << endl;
cout << "The following choices are available: " << endl;
cout << " A Append 3 strings to list" << endl;
cout << " S Display the size of list" << endl;
cout << " C Print the result from using the copy constructor" << endl;
cout << " O Print the result from using the assignment operator" << endl;
cout << " E Print the result from the empty( ) function" << endl;
cout << " P Print the entire list" << endl;
cout << " I Insert a new item with the insert(...) function" << endl;
cout << " F Find an item with the find( ) function" << endl;
cout << " R Remove an item with the erase( ) function" << endl;
cout << " Q Quit this test program" << endl;
}
--------------------------------------------------------------------------------
// List.h - linked list specification
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include <cstdlib>
#include <string>
typedef string ElementType;
class List
{
private:
class Node
{
public:
ElementType data;
Node * next;
Node( )
: data( ElementType( ) ), next( NULL )
{ }
Node( ElementType initData )
: data( initData ), next( NULL )
{ }
};
typedef Node * NodePointer;
NodePointer first;
int mySize;
public:
List( );
/* Construct a List object
Precondition: none.
Postcondition: An empty List object has been constructed.
*/
List( const List &source );
/* Construct a copy of a List object.
Precondition: None.
Postcondition: A copy of source has been constructed.
*/
~List( );
/* Destroys a List object.
Precondition: None.
Postcondition: Any memory allocated to the List object has been freed.
*/
List & operator=( const List &rightSide );
/* Assign a copy of a List object to the current object.
Precondition: none
Postcondition: A copy of rightside has been assigned to this
object. A reference to this list is returned.
*/
int getSize( ) const;
/* Returns the size of the list (number of items in the list)
Precondition: none
Postcondition: The return value is the number of items in the list.
*/
bool empty( ) const;
/* Check if this List is empty
Precondition: none
Postcondition: The return value is true if this List object is empty;
otherwise the return value is false.
*/
void insert( ElementType dataVal, int index );
void erase( int index );
void display( ostream &out ) const;
/* Display the contents of this List
Precondition: ostream out is open
Postcondition: the items in this List have been output to stream out
*/
int find( ElementType value) const;
ElementType top() const;
}; //--- end of List class
#endif
-------------------------------------------------------------------------------
/*-- DList.cpp-----------------------------------------------------------
This file implements List member functions.
-------------------------------------------------------------------------*/
#include <cstdlib> // Provides exit
#include <cassert>
#include <new>
using namespace std;
#include "List.h"
//----Definition of class constructor
List::List()
: mySize(0), first(0)
{}
//---Definition of calss destructor
List::~List()
{
List::NodePointer currPtr = first,
nextPtr;
while(currPtr != 0)
{
nextPtr = currPtr->next;
delete currPtr;
currPtr = nextPtr;
}
}
//---Definition of copy constructor
List::List(const List & source)
{
first = 0;
if(!source.empty())
{
first = new List::Node(source.first->data);
List::NodePointer lastPtr = first,
origPtr = source.first -> next;
while(origPtr != 0)
{
lastPtr -> next = new List::Node(origPtr -> data);
lastPtr = lastPtr -> next;
origPtr = origPtr -> next;
}
}
}
//---Definite of assigment operator
List & List::operator =(const List& rightSide)
{
if (this != &rightSide)
{
this->~List();
if(rightSide.empty())
first = 0;
else
{
first = new List::Node(rightSide.top());
List::NodePointer lastPtr = first,
rhsPtr = rightSide.first->next;
while(rhsPtr != 0)
{
lastPtr->next = new List::Node(rhsPtr->data);
lastPtr = lastPtr->next;
rhsPtr = rhsPtr->next;
}
}
}
return *this;
}
//---Defitnition of empty()
bool List::empty() const
{
return(first == 0);
}
//---Definition of display()
void List::display(ostream & out) const
{
List::NodePointer ptr= first;
while(ptr != 0)
{
cout<< ptr->data <<endl;
ptr = ptr->next;
}
cout<<"List size: "<< mySize <<endl;
}
void List::erase(int index)
{
int count = 0;
if(!empty())
{
List::NodePointer predPtr,
ptr;
if(predPtr!= 0)
{
ptr = predPtr->next;
predPtr->next = ptr->next;
}
else
{
while(count != index)
{
predPtr = ptr;
predPtr = ptr ->next;
count++;
}
ptr = predPtr->next;
predPtr->next = ptr ->next;
}
delete ptr;
}
mySize--;
}
void List::insert(ElementType dataVal, int index)
{
int count = 0;
if(!empty())
{
List::NodePointer predPtr = first,
ptr = first,
newPtr = new List::Node(dataVal);
if(predPtr != 0)
{
newPtr->next = predPtr->next;
predPtr->next = newPtr;
}
else
{
while (count != index)
{
predPtr = ptr;
predPtr = ptr->next;
count++;
}
newPtr->next = predPtr->next;
predPtr -> next = newPtr;
}
}
mySize++;
}
int List::find(ElementType value)const
{
List::NodePointer ptr = first;
int i = 0;
while (ptr != 0)
{
if (ptr->data == value)
return i;
i++;
ptr = ptr->next;
}
return -1;
}
ElementType List::top() const
{
if(!empty())
{
return(first->data);
}
else
{
cerr <<"*** List is empty"
"-- returning garbage ***\n";
ElementType * temp = new(ElementType);
ElementType garbage = *temp;
delete temp;
return garbage;
}
}
int List::getSize()const
{
return mySize;
}
Oct 16, 2015 at 1:42am UTC
Use [co de][/code] tags.
You didn't ask a question. What is the problem?