Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- ---------------------------------------
- #include <iostream>
- using std::cin;
- using std::endl;
- #include <string>
- using std::string;
- #include "list.h" // List class definition
- void instructions();
- // function to test a List
- template< class T >
- void testList( List< T > &listObject, const string &typeName )
- {
- cout << "Testing a List of " << typeName << " values\n";
- instructions(); // display instructions
- int choice;
- T value;
- int pos;
- do {
- cout << "? ";
- cin >> choice;
- switch ( choice ) {
- case 1:
- cout << "Enter " << typeName << ": ";
- cin >> value;
- listObject.insertAtFront( value );
- listObject.print();
- break;
- case 2:
- cout << "Enter " << typeName << ": ";
- cin >> value;
- listObject.insertAtBack( value );
- listObject.print();
- break;
- case 3:
- cout << "Enter " << typeName << ": ";
- cin >> value;
- cout << "Enter position to insert: ";
- cin >> pos;
- listObject.insertMiddle( value , pos );
- listObject.print();
- break;
- case 4:
- if ( listObject.removeFromFront( value ) )
- cout << value << " removed from list\n";
- listObject.print();
- break;
- case 5:
- if ( listObject.removeFromBack( value ) )
- cout << value << " removed from list\n";
- listObject.print();
- break;
- case 6:
- cout << "Enter position to delete: ";
- cin >> pos;
- if ( listObject.removeMiddle( value, pos ) )
- cout << value << " removed from list\n";
- listObject.print();
- break;
- } // end switch
- } while ( choice != 7 ); // end do/while
- cout << "End list test\n\n";
- } // end function testList
- // display program instructions to user
- void instructions()
- {
- cout << "Enter one of the following:\n"
- << " 1 to insert at beginning of list\n"
- << " 2 to insert at end of list\n"
- << " 3 to insert at middle in the list\n"
- << " 4 to delete from beginning of list\n"
- << " 5 to delete from end of list\n"
- << " 6 to delete from middle in the list\n"
- << " 7 to end list processing\n";
- } // end function instructions
- int main()
- {
- // test List of int values
- List< int > integerList;
- testList( integerList, "integer" );
- // test List of double values
- List< double > doubleList;
- testList( doubleList, "double" );
- return 0;
- } // end main
- -------------------------------------------------------------
- list.h
- --------------------------------
- #ifndef LIST_H
- #define LIST_H
- #include <iostream>
- using std::cout;
- #include <new>
- #include "listnode.h" // ListNode class definition
- template< class NODETYPE >
- class List {
- public:
- List(); // constructor
- ~List(); // destructor
- void insertAtFront( const NODETYPE & );
- void insertMiddle( const NODETYPE &, int);
- void insertAtBack( const NODETYPE & );
- bool removeFromFront( NODETYPE & );
- bool removeMiddle(NODETYPE &, int);
- bool removeFromBack( NODETYPE & );
- bool isEmpty() const;
- void print() const;
- private:
- ListNode< NODETYPE > *firstPtr; // pointer to first node
- ListNode< NODETYPE > *lastPtr; // pointer to last node
- // utility function to allocate new node
- ListNode< NODETYPE > *getNewNode( const NODETYPE & );
- }; // end class List
- // default constructor
- template< class NODETYPE >
- List< NODETYPE >::List()
- : firstPtr( 0 ),
- lastPtr( 0 )
- {
- // empty body
- } // end List constructor
- // destructor
- template< class NODETYPE >
- List< NODETYPE >::~List()
- {
- if ( !isEmpty() ) { // List is not empty
- // cout << "Destroying nodes ...\n";
- ListNode< NODETYPE > *currentPtr = firstPtr;
- ListNode< NODETYPE > *tempPtr;
- while ( currentPtr != 0 ) // delete remaining nodes
- {
- tempPtr = currentPtr;
- // commented out the output -- no need to print what we are deallocating
- // cout << tempPtr->data << '\n';
- currentPtr = currentPtr->nextPtr;
- delete tempPtr;
- }
- }
- // cout << "All nodes destroyed\n\n";
- } // end List destructor
- // insert node at front of list
- template< class NODETYPE >
- void List< NODETYPE >::insertAtFront( const NODETYPE &value )
- {
- ListNode< NODETYPE > *newPtr = getNewNode( value );
- if ( isEmpty() ) // List is empty
- firstPtr = lastPtr = newPtr;
- else { // List is not empty
- newPtr->nextPtr = firstPtr;
- firstPtr = newPtr;
- } // end else
- } // end function insertAtFront
- // insert node at back of list
- template< class NODETYPE >
- void List< NODETYPE >::insertAtBack( const NODETYPE &value )
- {
- ListNode< NODETYPE > *newPtr = getNewNode( value );
- if ( isEmpty() ) // List is empty
- firstPtr = lastPtr = newPtr;
- else { // List is not empty
- lastPtr->nextPtr = newPtr;
- lastPtr = newPtr;
- } // end else
- } // end function insertAtBack
- // delete node from front of list
- template< class NODETYPE >
- bool List< NODETYPE >::removeFromFront( NODETYPE &value )
- {
- if ( isEmpty() ) // List is empty
- return false; // delete unsuccessful
- else
- {
- ListNode< NODETYPE > *tempPtr = firstPtr;
- if ( firstPtr == lastPtr )
- firstPtr = lastPtr = 0;
- else
- firstPtr = firstPtr->nextPtr;
- value = tempPtr->data; // data being removed
- delete tempPtr;
- return true; // delete successful
- } // end else
- } // end function removeFromFront
- // delete node from back of list
- template< class NODETYPE >
- bool List< NODETYPE >::removeFromBack( NODETYPE &value )
- {
- if ( isEmpty() )
- return false; // delete unsuccessful
- else {
- ListNode< NODETYPE > *tempPtr = lastPtr;
- if ( firstPtr == lastPtr )
- firstPtr = lastPtr = 0;
- else {
- ListNode< NODETYPE > *currentPtr = firstPtr;
- // locate second-to-last element
- while ( currentPtr->nextPtr != lastPtr )
- currentPtr = currentPtr->nextPtr;
- lastPtr = currentPtr;
- currentPtr->nextPtr = 0;
- } // end else
- value = tempPtr->data;
- delete tempPtr;
- return true; // delete successful
- } // end else
- } // end function removeFromBack
- // is List empty?
- template< class NODETYPE >
- bool List< NODETYPE >::isEmpty() const
- {
- return firstPtr == 0;
- } // end function isEmpty
- // return pointer to newly allocated node
- template< class NODETYPE >
- ListNode< NODETYPE > *List< NODETYPE >::getNewNode(
- const NODETYPE &value )
- {
- return new ListNode< NODETYPE >( value );
- } // end function getNewNode
- // display contents of List
- template< class NODETYPE >
- void List< NODETYPE >::print() const
- {
- if ( isEmpty() ) {
- cout << "The list is empty\n\n";
- return;
- } // end if
- ListNode< NODETYPE > *currentPtr = firstPtr;
- cout << "The list is: ";
- while ( currentPtr != 0 ) {
- cout << currentPtr->data << ' ';
- currentPtr = currentPtr->nextPtr;
- } // end while
- cout << "\n\n";
- } // end function print
- ////////////////////////////////////////////////////////////////////////////////////////////////
- //my functions
- template< class NODETYPE >
- bool List< NODETYPE >::removeMiddle( NODETYPE &value, int spot )
- {
- if ( isEmpty() || spot <= 0) //when the list is empty or spot is invalid return flase
- return false;
- else
- {
- ListNode< NODETYPE > *tempPtr = firstPtr; //creates temp ptr that intially is the same as the first pointer
- if(spot==1) //if spot is one call remove from front
- {
- removeFromFront(value);
- }
- else
- {
- for(int i=0; i <spot-2;i++) //runs til less than index -2
- {
- //tempptr now points to whatever the next pointer is
- if(tempPtr->nextPtr==lastPtr) //returns false when spot is beyond list capacity
- {
- return false;
- }
- tempPtr=tempPtr->nextPtr; //tempptr points to the (n-1th) node after completion
- }
- ListNode< NODETYPE > *tempPtr2=tempPtr->nextPtr; //tempptr2 points to the nth node
- tempPtr->nextPtr=tempPtr2->nextPtr; //tempptrs next pointer points to (n+1)th node
- return true;
- }
- }
- }
- template< class NODETYPE >
- void List< NODETYPE >::insertMiddle( const NODETYPE &value, int spot )
- {
- ListNode< NODETYPE > *newPtr = getNewNode( value ); //newptr is a new dynamically allocated ptr to the paramater
- ListNode< NODETYPE > *newPtr2 = firstPtr; //newptr2 is now the same as first point
- newPtr->data=value; //the paramaters data is the paramaters data
- newPtr->nextPtr=NULL; //it's next value points to the 0
- if ( isEmpty() ) // if the List is empty the first ptr, newptr and last ptr are the same
- {
- insertAtFront(value);
- return;
- }
- if(spot <= 1) //if index is <= to first spot then set as index 1
- {
- insertAtFront(value);
- return;
- }
- for(int i=0;i<spot-2;i++) //runs til less than index -2
- {
- //newptr2 now points to whatever the next pointer is
- if(newPtr2->nextPtr==lastPtr) //inserts at end when spot is beyond list capacity
- {
- insertAtBack(value);
- return;
- }
- newPtr2= newPtr2->nextPtr; //newPtr2 points to the (n-1th) node after completion
- }
- newPtr->nextPtr=newPtr2->nextPtr; //newptr's next pointer is the paramater
- newPtr2->nextPtr=newPtr; //newptr2's next ptr is newptr;
- }
- #endif
- -----------------------------------------------------
- listnode.h
- ------------------------
- #ifndef LISTNODE_H
- #define LISTNODE_H
- // forward declaration of class List
- template< class NODETYPE > class List;
- template< class NODETYPE >
- class ListNode {
- friend class List< NODETYPE >; // make List a friend
- public:
- ListNode( const NODETYPE & ); // constructor
- NODETYPE getData() const; // return data in node
- private:
- NODETYPE data; // data
- ListNode< NODETYPE > *nextPtr; // next node in list
- }; // end class ListNode
- // constructor
- template< class NODETYPE >
- ListNode< NODETYPE >::ListNode( const NODETYPE &info )
- : data( info ),
- nextPtr( 0 )
- {
- // empty body
- } // end ListNode constructor
- // return copy of data in node
- template< class NODETYPE >
- NODETYPE ListNode< NODETYPE >::getData() const
- {
- return data;
- } // end function getData
- #endif
- ---------------------------------------------------------------------------
- stack.h
- ------------------------------------------
- #ifndef STACK_H
- #define STACK_H
- #include "list.h" // List class definition
- template< class STACKTYPE >
- class Stack : private List< STACKTYPE > {
- public:
- // push calls List function insertAtFront
- void push( const STACKTYPE &data )
- {
- this->insertAtFront(data);
- } // end function push
- // pop calls List function removeFromFront
- bool pop( STACKTYPE &data )
- {
- return this->removeFromFront( data );
- } // end function pop
- // isStackEmpty calls List function isEmpty
- bool isStackEmpty() const
- {
- return this->isEmpty();
- } // end function isStackEmpty
- // printStack calls List function print
- void printStack() const
- {
- this->print();
- } // end function print
- }; // end class Stack
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement