Recitation Week 11 Object Oriented Programming COP 3330
Recitation Week 11 Object Oriented Programming COP 3330 / CGS 5409
Today’s Recitation � Data Structures continued ◦ Vectors ◦ Linked Lists
Vectors � Extension � Stores � Like to array objects arbitrary sequence of objects 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 ◦ 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() � void clear() removes all elements from the vector bool empty() elements returns true of the vector has no � � � returns the number of elements in the vector void push_back ( const Object &x ) of the vector void pop_back ( ) of the vector adds x to the end removes the object at the end
STL Vector Operations � Object & back ( ) of the vector returns the object at the end � Object returns the object at the & front ( ) 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() vector returns the internal capacity of the � void reserve(int new. Capacity) capacity of the vector � void resize(int new. Size ) vector sets the new change the size of the
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; elements in list � bool empty() const; has no elements � iterator begin(); list node � const returns current number of returns true if list returns an iterator to head iterator begin() const; version of above constant
Linked List Operations � iterator end(); tail list node � const returns an iterator to the iterator end() const; version of above & front(); head list node constant � Object returns reference to the & back(); 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 (2 nd element becomes new head node) � int pop_back(); remove element from tail of list (2 nd 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?
- Slides: 21