Object Oriented Programming COP3330 / CGS5409 Recitation Week 11 Object Oriented Programming COP3330 / CGS5409
Today’s Recitation Data Structures continued Vectors Linked Lists
Vectors Extension to array objects Stores arbitrary sequence of objects Like an array, elements in a vector are accessed using their index
Vectors Advantages: Random access - i.e. quick locating of data if the index is known. Disadvantages: Inserts and Deletes are typically slow, since they may require shifting many elements to consecutive array slots
Implementing Vectors Vector maintains Operations: A primitive C++ array The maximum capacity allocated The current number of items stored in the Vector Operations: Copy constructor operator= Destructor to reclaim primitive array
STL Vector Operations int size() returns the number of elements in the vector void clear() removes all elements from the vector bool empty() returns true of the vector has no elements void push_back ( const Object &x ) adds x to the end of the vector void pop_back ( ) removes the object at the end of the vector
STL Vector Operations Object & back ( ) returns the object at the end of the vector Object & front ( ) returns the object at the front of the vector Object & operator[] ( int index ) returns the object at location index (without bounds checking) Both accessor and mutator versions
STL Vector Operations Object & at( int index ) returns the object at location index (with bounds checking) int capacity() returns the internal capacity of the vector void reserve(int newCapacity) sets the new capacity of the vector void resize(int newSize ) change the size of the vector
Linked Lists Collections of data items linked together with pointers, lined up "in a row". Typically a list of data of the same type, like an array, but storage is arranged differently. Made up of a collection of "nodes", which are created from a self-referential class (or struct).
Linked Lists Advantages: Inserts and Deletes are typically fast. Require only creation of a new node, and changing of a few pointers. Disadvantage: No random access. Possible to build indexing into a linked list class, but locating an element requires walking through the list. Notice that the advantages of the array (vector) are generally the disadvantages of the linked list, and vice versa
Linked Lists Nodes can be anywhere in memory (not restricted to consecutive slots, like in an array). Nodes generally allocated dynamically, so a linked list can grow to any size, theoretically (within the boundaries of the program's memory).
Implementing Linked Lists A linked list maintains A collection of nodes Pointer to the list head element Pointer to the list tail element The current number of items Each linked list nodes contains: A data element Pointer to the next element in the list (NULL if last) Pointer to the previous element in the list (NULL if first)
Linked List Visualization
Empty Linked List
Linked List Operations int size() const; returns current number of elements in list bool empty() const; returns true if list has no elements iterator begin(); returns an iterator to head list node const iterator begin() const; constant version of above
Linked List Operations iterator end(); returns an iterator to the tail list node const iterator end() const; constant version of above Object & front(); returns reference to the head list node Object & back(); returns reference to the tail list node
Linked List Operations int push_front(const Object & x); push element to front of list (element becomes new head node) int push_back(const Object & x); push element to end of list (element becomes new tail node) int pop_front(); remove element from front of list (2nd element becomes new head node) int pop_back(); remove element from tail of list (2nd to last element becomes new tail node)
Linked List Operations int insert(iterator & itr, const Object & x); insert element x at iterator position iterator erase( iterator itr); remove node pointed to by iterator iterator erase( iterator start, iterator end ); erase nodes in range between iterators void clear(); removes all nodes from list. List is now an empty list
Linked List insert() node
Linked List erase() node
Questions?