Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming COP3330 / CGS5409

Similar presentations


Presentation on theme: "Object Oriented Programming COP3330 / CGS5409"— Presentation transcript:

1 Object Oriented Programming COP3330 / CGS5409
Recitation Week 11 Object Oriented Programming COP3330 / CGS5409

2 Today’s Recitation Data Structures continued Vectors Linked Lists

3 Vectors Extension to array objects
Stores arbitrary sequence of objects Like an array, elements in a vector are accessed using their index

4 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

5 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

6 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

7 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

8 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

9 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).

10 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

11 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).

12 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)

13 Linked List Visualization

14 Empty Linked List

15 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

16 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

17 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)

18 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

19 Linked List insert() node

20 Linked List erase() node

21 Questions?


Download ppt "Object Oriented Programming COP3330 / CGS5409"

Similar presentations


Ads by Google