Due by tonight 10:00pm
assn_11/List.cpp
#include "List.h" #include "ListNode.h" #include "Settings.h" namespace cs52 { List::List() { head = NULL; listSize = 0; } List::~List() { #ifdef SHOW_DESTRUCTOR_CALLS std::cout << "about to--> makeEmpty();" << std::endl; #endif makeEmpty(); #ifdef SHOW_DESTRUCTOR_CALLS std::cout << "about to--> delete( head );" << std::endl; #endif delete( head ); } bool List::isEmpty() const { return( head == NULL ); } void List::makeEmpty() { while (head != NULL) { remove( head->getElement() ); } } int List::size() const { return( listSize ); } void List::insert( const int& data ) { // place data into a ListNode at the front of the list // it will move the head node to behind the node // we create dynamically ListNode* temp = head; ListNode* newnode = new ListNode( data ); head = newnode; newnode->setNext( temp ); listSize++; } void List::append( const int& data ) { // Insert code here... } void List::remove( const int& data ) { ListNode* current = head; ListNode* previous = NULL; ListNode* nodeToRemove = NULL; while (current != NULL) { // have we found it at the current node??? if (current->getElement() == data) { // found it at head node if (previous == NULL) { nodeToRemove = head; head = head->getNext(); } // found it inside the list somewhere else { nodeToRemove = current; // skip the current node previous->setNext( current->getNext() ); } delete( nodeToRemove ); listSize--; break; } // keep looking else { previous = current; current = current->getNext(); } } } std::ostream& operator << ( std::ostream& outs, const List& l) { return( l.printList( outs ) ); } std::ostream& operator << ( std::ostream& outs, const List* l) { return( l->printList( outs ) ); } std::ostream& List::printList( std::ostream& outs ) const { if (isEmpty()) outs << "Empty List" << std::endl; else { outs << "List has " << size() << " elements: " << std::endl; ListNode* current = head; while (current != NULL) { outs << current->getElement() << " -> "; current = current->getNext(); } outs << " NULL"; outs << std::endl; } return( outs ); } }
__MACOSX/assn_11/._List.cpp
assn_11/List.h
#ifndef LIST_H #define LIST_H #include <iostream> #include "ListNode.h" namespace cs52 { class List { public: List(); ~List(); // Implement these! void concatenate( const List& B); void insertith( const int& data, const size_t& i); void removeDups(); bool isEmpty() const; int size() const; void makeEmpty(); void insert( const int& data ); void remove( const int& data ); friend std::ostream& operator << ( std::ostream& outs, const List& l ); friend std::ostream& operator << ( std::ostream& outs, const List* l ); private: ListNode* head; int listSize; std::ostream& printList( std::ostream& outs ) const; //Implement this!! void append( const int& data ); }; } #endif
__MACOSX/assn_11/._List.h
assn_11/ListDriver.cpp
assn_11/ListDriver.cpp
// ListDriver.cpp : Defines the entry point for the console application.
//
#include
<
iostream
>
#include
<
cstdlib
>
#include
"List.h"
#include
"ListNode.h"
enum
CHOICE
{
PRINT1STLIST
,
PRINT2NDLIST
,
QUIT
,
INSERT
,
REMOVE
,
ISEMPTY
,
MAKEEMPTY
,
CONCATENATE
,
INSERTITH
,
REMOVEDUPS
};
CHOICE menu
();
int
main
(
int
argc
,
char
*
argv
[])
{
using
namespace
cs52
;
using
namespace
std
;
List
l
;
List
l2
;
CHOICE c
;
int
value
,
pos
;
do
{
c
=
menu
();
switch
(
c
)
{
case
PRINT2NDLIST
:
cout
<<
l2
;
break
;
case
PRINT1STLIST
:
cout
<<
l
;
break
;
case
ISEMPTY
:
if
(
l
.
isEmpty
())
{
cout
<<
"list is empty"
<<
endl
;
}
else
{
cout
<<
"list is not empty"
<<
endl
;
}
break
;
case
MAKEEMPTY
:
l
.
makeEmpty
();
break
;
case
INSERT
:
cout
<<
"enter an int to insert:"
;
cin
>>
value
;
l
.
insert
(
value
);
break
;
case
REMOVE
:
cout
<<
"enter an int to remove:"
;
cin
>>
value
;
l
.
remove
(
value
);
break
;
case
CONCATENATE
:
cout
<<
"doing concatenation"
<<
endl
;
l
.
concatenate
(
l2
);
break
;
case
INSERTITH
:
cout
<<
"enter an int to insert:"
;
cin
>>
value
;
cout
<<
"enter the position to insert into:"
;
cin
>>
pos
;
cout
<<
"inserting into ith position"
<<
endl
;
l
.
insertith
(
value
,
pos
);
break
;
case
REMOVEDUPS
:
cout
<<
"removing duplicates"
<<
endl
;
l
.
removeDups
();
break
;
}
}
while
(
c
!=
QUIT
);
return
(
0
);
}
CHOICE menu
()
{
using
namespace
std
;
char
c
;
CHOICE result
;
cout
<<
"i(S)empty (M)akeEmpty (I)nsert (R)emove Print(1)stList Print(2)ndList (C)concatentate I(N)sertIth R(E)moveDups (Q)uit:"
;
cin
>>
c
;
switch
(
c
)
{
case
'C'
:
case
'c'
:
result
=
CONCATENATE
;
break
;
case
'S'
:
case
's'
:
result
=
ISEMPTY
;
break
;
case
'M'
:
case
'm'
:
result
=
MAKEEMPTY
;
break
;
case
'I'
:
case
'i'
:
result
=
INSERT
;
break
;
case
'N'
:
case
'n'
:
result
=
INSERTITH
;
break
;
case
'R'
:
case
'r'
:
result
=
REMOVE
;
break
;
case
'1'
:
result
=
PRINT1STLIST
;
break
;
case
'2'
:
result
=
PRINT2NDLIST
;
break
;
case
'Q'
:
case
'q'
:
result
=
QUIT
;
break
;
case
'E'
:
case
'e'
:
result
=
REMOVEDUPS
;
break
;
default
:
result
=
menu
();
}
return
(
result
);
}
__MACOSX/assn_11/._ListDriver.cpp
assn_11/ListNode.cpp
#include <iostream> #include "ListNode.h" namespace cs52 { ListNode::ListNode( const int& theElement, ListNode* nextNode ) : element( theElement ), next( nextNode ) { } const int ListNode::getElement() const { return( element ); } void ListNode::setNext( ListNode * nextNode ) { next = nextNode; } ListNode * ListNode::getNext() const { return( next ); } }
__MACOSX/assn_11/._ListNode.cpp
assn_11/ListNode.h
#ifndef LISTNODE_H #define LISTNODE_H #include <iostream> namespace cs52 { class ListNode { public: ListNode( const int& theElement = 0, ListNode * nextNode = NULL ); const int getElement() const; void setNext( ListNode * nextNode ); ListNode * getNext() const; private: int element; ListNode* next; }; } #endif
__MACOSX/assn_11/._ListNode.h
assn_11/Settings.h
// uncomment to show destructor calls #define SHOW_DESTRUCTOR_CALLS