Sessions 26 and 27 - Data Structures & Algorithm

The type of member data depends on how the class

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 17 23 24 list of nodes 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 // 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 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 16 17 18 19 20 21 22 23 24 25 // Template List class definition. #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 insertAtBack( const NODETYPE & ); bool removeFromFront( NODETYPE & ); bool removeFromBack( NODETYPE & ); bool isEmpty() const; void print() const; Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 19 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 Each List has a firstPtr and lastPtr. } // end List constructor Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 20 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 // 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; cout << tempPtr->data << '\n'; currentPtr = currentPtr->nextPtr; delete tempPtr; } // end while } // end if cout << "All nodes destroyed\n\n"; } // end List destructor Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 21 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 // 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 insert...
View Full Document

Ask a homework question - tutors are online