C++ data stracture 13

profileahmeddxn7
CSC240_Linked_List_Practice.zip

CSC240_Linked_List_Practice/.cproject

CSC240_Linked_List_Practice/.project

CSC240_Linked_List_Practice org.eclipse.cdt.managedbuilder.core.genmakebuilder clean,full,incremental, org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder full,incremental, org.eclipse.cdt.core.cnature org.eclipse.cdt.core.ccnature org.eclipse.cdt.managedbuilder.core.managedBuildNature org.eclipse.cdt.managedbuilder.core.ScannerConfigNature

CSC240_Linked_List_Practice/.settings/language.settings.xml

CSC240_Linked_List_Practice/.settings/org.eclipse.cdt.managedbuilder.core.prefs

eclipse.preferences.version=1 environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/CPATH/delimiter=; environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/CPATH/operation=remove environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/CPLUS_INCLUDE_PATH/delimiter=; environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/CPLUS_INCLUDE_PATH/operation=remove environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/C_INCLUDE_PATH/delimiter=; environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/C_INCLUDE_PATH/operation=remove environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/append=true environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/appendContributed=true environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/LIBRARY_PATH/delimiter=; environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/LIBRARY_PATH/operation=remove environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/append=true environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.1064068495/appendContributed=true

CSC240_Linked_List_Practice/CSC240_Linked_List_Practice.exe.stackdump

Exception: STATUS_ACCESS_VIOLATION at eip=00401684 eax=00382D46 ebx=0064CC2C ecx=0064CA48 edx=00382D46 esi=20062BB0 edi=611D7DB2 ebp=0064CB08 esp=0064CB08 program=C:\Users\igt88\eclipseOxygen\CSC240_Linked_List_Practice\Debug\CSC240_Linked_List_Practice.exe, pid 7148, thread main cs=0023 ds=002B es=002B fs=0053 gs=002B ss=002B Stack trace: Frame Function Args 0064CB08 00401684 (00382D46, 611D7DB2, 0064CB68, 6C729E29) 0064CB38 00401A1E (0064CB84, 004040B7, 6C739CE0, 6C6D8EED) 0064CB68 00401AD6 (0064CB84, 0064CB9C, 00000005, 6114E000) 0064CBF8 00401589 (20062BB0, 611D7DB2, 0064CD18, 61007A3F) 0064CD18 61007A3F (00000000, 0064CD74, 61006A70, 00000000) End of stack trace

CSC240_Linked_List_Practice/Debug/CSC240_Linked_List_Practice.exe

CSC240_Linked_List_Practice/Debug/src/CSC240_Linked_List_Practice.o

CSC240_Linked_List_Practice/Debug/src/ItemType.o

CSC240_Linked_List_Practice/Debug/src/unsorted.o

CSC240_Linked_List_Practice/src/CSC240_Linked_List_Practice.cpp

CSC240_Linked_List_Practice/src/CSC240_Linked_List_Practice.cpp

//============================================================================
// Name        : CSC240_Linked_List_Practice.cpp
// Author      : Ivan Temesvari
// Version     : 2/11/2019
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include   < iostream >
#include   "unsorted.h"
using   namespace  std ;
//This function removes all instances of ItemType item from list,
//and returns a list without those items.
UnsortedType   DeleteAllItems ( UnsortedType &  list ,   ItemType  item ){
     UnsortedType  listWithoutItem ;
    list . ResetList ();
     for ( int  i  =   0 ;  i  <  list . GetLength ();  i ++ ){
         ItemType  temp  =  list . GetNextItem ();
         if ( temp . ComparedTo ( item )   !=  EQUAL ){
            listWithoutItem . PutItem ( temp );
         }
     }
     return  listWithoutItem ;
}


//Write a function that returns a list of all instances of item
//found in an UnsortedType linked-list.
UnsortedType   FindAllItems ( UnsortedType &  list ,   ItemType  item ){
     UnsortedType  itemsFound ;
    list . ResetList ();
     for ( int  i  =   0 ;  i  <  list . GetLength ();  i ++ ){
         ItemType  temp  =  list . GetNextItem ();
         if ( temp . ComparedTo ( item )   ==  EQUAL ){
            itemsFound . PutItem ( temp );
         }
     }
     return  itemsFound ;
}

int  main ()   {
     UnsortedType  myList ;
    myList . PutItem ( ItemType ( 8 ));
    myList . PutItem ( ItemType ( 5 ));
    myList . PutItem ( ItemType ( 2 ));
    myList . PutItem ( ItemType ( 5 ));
    myList . PutItem ( ItemType ( 7 ));
    myList . PutItem ( ItemType ( 3 ));
    myList . PutItem ( ItemType ( 5 ));
    myList . Print ();
     FindAllItems ( myList ,   ItemType ( 5 )). Print ();
     DeleteAllItems ( myList ,   ItemType ( 5 )). Print ();
     UnsortedType  newList ;
    newList . PutItem ( ItemType ( 9 ));
    newList . PutItem ( ItemType ( 7 ));
    newList . PutItem ( ItemType ( 2 ));
    newList . PutItem ( ItemType ( 3 ));
    newList . PutItem ( ItemType ( 4 ));
    newList . PutItem ( ItemType ( 6 ));
    newList . PutItem ( ItemType ( 1 ));
    cout  <<   "Before assignment: "   <<  endl ;
    newList . Print ();
    newList  =  myList ;     //use the overloaded assignment operator
    cout  <<   "After assignment: "   <<  endl ;
    newList . Print ();
    myList . Print ();
     UnsortedType  copy ( myList );     //use the copy constructor
    copy . Print ();
     //if the copy constructor is not implemented, the following assignment will be shallow copy
     UnsortedType  otherList  =  copy ;     //NOT the assignment operator, but copy constructor
    cout  <<   "otherList should have the same contents of copy."   <<  endl ;
    otherList . Print ();
    cout  <<   "copy."   <<  endl ;
    copy . DeleteItem ( ItemType ( 3 ));
    copy . Print ();
    cout  <<   "otherList should NOT have the same contents of copy."   <<  endl ;
    otherList . Print ();
     return   0 ;
}

CSC240_Linked_List_Practice/src/ItemType.cpp

CSC240_Linked_List_Practice/src/ItemType.cpp

// The following definitions go into file ItemType.cpp. 
#include   < fstream >
#include   < iostream >
#include   "ItemType.h"
ItemType :: ItemType ( int  x ){
     value   =  x ;
}

ItemType :: ItemType ()
{  
   value   =   0 ;
}

int   ItemType :: GetValue (){
     return   value ;
}

RelationType   ItemType :: ComparedTo ( ItemType  otherItem )   const  
{
   if   ( value   <  otherItem . value )
     return  LESS ;
   else   if   ( value   >  otherItem . value )
     return  GREATER ;
   else   return  EQUAL ;
}

void   ItemType :: Initialize ( int  number )  
{
   value   =  number ;
}

void   ItemType :: Print ( std :: ostream &  out )   const  
// pre:  out has been opened.
// post: value has been sent to the stream out.
{
  out  <<   value ;
}

CSC240_Linked_List_Practice/src/ItemType.h

#ifndef ITEM_TYPE_H #define ITEM_TYPE_H // The following declarations and definitions go into file // ItemType.h. #include <fstream> const int MAX_ITEMS = 5; enum RelationType {LESS, GREATER, EQUAL}; class ItemType { public: ItemType(); ItemType(int x); int GetValue(); RelationType ComparedTo(ItemType) const; void Print(std::ostream&) const; void Initialize(int number); private: int value; }; #endif

CSC240_Linked_List_Practice/src/unsorted.cpp

CSC240_Linked_List_Practice/src/unsorted.cpp


// This file contains the linked implementation of class
// UnsortedType.

#include   "unsorted.h"

#include   < iostream >
using   namespace  std ;
struct   NodeType
{
     ItemType  info ;
     NodeType *  next ;
};

UnsortedType :: UnsortedType ()    // Class constructor
{
  length  =   0 ;
  listData  =  NULL ;
}
bool   UnsortedType :: IsFull ()   const
// Returns true if there is no room for another ItemType
//  on the free store; false otherwise.
{
   NodeType *  location ;
   try
   {
    location  =   new   NodeType ;
     delete  location ;
     return   false ;
   }
   catch ( std :: bad_alloc exception )
   {
     return   true ;
   }
}

int   UnsortedType :: GetLength ()   const
// Post: Number of items in the list is returned.
{
   return  length ;
}

void   UnsortedType :: MakeEmpty ()
// Post: List is empty; all items have been deallocated.
{
   NodeType *  tempPtr ;

     while   ( listData  !=  NULL )
     {
      tempPtr  =  listData ;
      listData  =  listData -> next ;
       delete  tempPtr ;
   }
  length  =   0 ;
}
void   UnsortedType :: PutItem ( ItemType  item )
// item is in the list; length has been incremented.
{
   NodeType *  location ;             // Declare a pointer to a node

  location  =   new   NodeType ;        // Get a new node 
  location -> info  =  item ;          // Store the item in the node
  location -> next  =  listData ;      // Store address of first node 
                         //   in next field of new node
  listData  =  location ;        // Store address of new node into
                         //   external pointer
  length ++ ;               // Increment length of the list
}

ItemType   UnsortedType :: GetItem ( ItemType &  item ,   bool &  found )
// Pre:  Key member(s) of item is initialized.
// Post: If found, item's key matches an element's key in the 
//       list and a copy of that element has been stored in item;
//       otherwise, item is unchanged. 
{
   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 ;
}

void   UnsortedType :: DeleteItem ( ItemType  item )
// Pre:  item's key has been initialized.
//       An element in the list has a key that matches item's.
// Post: No element in the list has a key that matches item's.
{
   NodeType *  location  =  listData ;
   NodeType *  tempLocation ;

   // Locate node to be deleted.
   if   ( item . ComparedTo ( listData -> info )   ==  EQUAL )
   {
    tempLocation  =  location ;
    listData  =  listData -> next ;        // Delete first node.
   }
   else
   {
     while   ( item . ComparedTo (( location -> next ) -> info )   !=  EQUAL )
      location  =  location -> next ;

     // Delete node at location->next
    tempLocation  =  location -> next ;
    location -> next  =   ( location -> next ) -> next ;
   }
   delete  tempLocation ;
  length -- ;
}

void   UnsortedType :: ResetList ()
// Post: Current position has been initialized.
{
  currentPos  =  NULL ;
}
 
ItemType   UnsortedType :: GetNextItem ()
// Post:  A copy of the next item in the list is returned.
//        When the end of the list is reached, currentPos
//        is reset to begin again.
{

   if   ( currentPos  ==  NULL )    //start at the beginning
    currentPos  =  listData ;
   else     //move to the next item
    currentPos  =  currentPos -> next ;
   ItemType  item (( currentPos -> info ). GetValue ());
   return  item ;
}

UnsortedType ::~ UnsortedType ()
// Post: List is empty; all items have been deallocated.
{
   NodeType *  tempPtr ;

   while   ( listData  !=  NULL )
   {
    tempPtr  =  listData ;
    listData  =  listData -> next ;
     delete  tempPtr ;
   }
}

void   UnsortedType :: Print (){
     ResetList ();
     if ( length  ==   0 ){
        cout  <<   "Empty list."   <<  endl ;
     }
     else {
        cout  <<   "The list: "   <<  endl ;
         do {
             GetNextItem (). Print ( cout );
            cout  <<   " " ;
         } while ( currentPos -> next  !=  NULL );
     }
    cout  <<  endl ;
}

//Assignment operator
UnsortedType &   UnsortedType :: operator = ( const   UnsortedType &  rhs ){
     NodeType *  tempPtr ;
     if ( this   ==   & rhs ){      //don't allow list = list;
         return   * this ;
     }
     else {
         //delete old contents of this
         int  tempLength  =   GetLength ();
         for ( int  i  =   0 ;  i  <  tempLength ;  i ++ ){
             ItemType  item ( listData -> info );
             this -> DeleteItem ( item );
         }
         //copy new contents over to this from rhs
        tempPtr  =  rhs . listData ;
         for ( int  i  =   0 ;  i  <  rhs . GetLength ();  i ++ ){
             ItemType  item ( tempPtr -> info );
             this -> PutItem ( item );
            tempPtr  =  tempPtr -> next ;
         }
     }
     return   * this ;
}

//Copy Constructor
UnsortedType :: UnsortedType ( const   UnsortedType &  ut ){
    length  =   0 ;
    listData  =  NULL ;
     NodeType *  tempPtr ;
    tempPtr  =  ut . listData ;
     for ( int  i  =   0 ;  i  <  ut . GetLength ();  i ++ ){
         ItemType  item ( tempPtr -> info );
         this -> PutItem ( item );
        tempPtr  =  tempPtr -> next ;
     }
}



CSC240_Linked_List_Practice/src/unsorted.h

#ifndef UNSORTED_H #define UNSORTED_H // File ItemType.h must be provided by the user of this class. // ItemType.h must contain the following definitions: // MAX_ITEMS: the maximum number of items on the list // ItemType: the definition of the objects on the list // RelationType: {LESS, GREATER, EQUAL} // Member function ComparedTo(ItemType item) which returns // LESS, if self "comes before" item // GREATER, if self "comes after" item // EQUAL, if self and item are the same // Iterator: currentPos, GetNextItem // When the end of the list is reached, currentPos // is reset to begin again. #include "ItemType.h" struct NodeType; class UnsortedType { public: UnsortedType(); // Constructor ~UnsortedType(); // Destructor UnsortedType& operator=(const UnsortedType& rhs); //Assignment operator UnsortedType(const UnsortedType& ut); //Copy Constructor void MakeEmpty(); // Function: Returns the list to the empty state. // Post: List is empty. bool IsFull() const; // Function: Determines whether list is full. // Pre: List has been initialized. // Post: Function value = (list is full) int GetLength() const; // Function: Determines the number of elements in list. // Pre: List has been initialized. // Post: Function value = number of elements in list ItemType GetItem(ItemType& item, bool& found); // Function: 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. // List is unchanged. void PutItem(ItemType item); // Function: Adds item to list. // Pre: List has been initialized. // List is not full. // item is not in list. // Post: item is in list. void DeleteItem(ItemType item); // Function: 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 ResetList(); // Function: Initializes current position for an iteration through the list. // Pre: List has been initialized. // Post: Current position is prior to list. ItemType GetNextItem(); // Function: Gets the next element in list. // Pre: List has been initialized and has not been changed since last call. // Current position is defined. // Element at current position is not last in list. // // Post: Current position is updated to next position. // item is a copy of element at current position. void Print(); //Complete this... private: NodeType* listData; int length; NodeType* currentPos; //iterator }; #endif