Sessions 26 and 27 - Data Structures & Algorithm

Introduction to programming sessions 26 and 27 data

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: AtFront Sharif University of Technology Insert a new node as described in the previous diagrams. Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 22 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 // 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 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 23 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 // 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 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 24 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 // 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; while(currentptr!=0) cout // locate second-to-last element while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; NULL } // end else value = tempPtr->data; delete tempPtr; Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 25 154 155 156 157 158 159 160 161 162 163 164 165 166 167 167 168 169 170 171 172 173 174 175 176 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 ) { class template node value return new ListNode< NODETYPE >( value ); } // end function getNewNode Note use of new operator to dynamically allocate a node. Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 26 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 191 192 193 194 195 196 197 198 199 200 201 // display contents of List templa...
View Full Document

This note was uploaded on 09/01/2013 for the course CE CE230 taught by Professor Arash during the Fall '13 term at Sharif University of Technology.

Ask a homework question - tutors are online