Object Oriented Programming COP3330 / CGS5409

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Lecture 7 : Intro. to STL (Standard Template Library)
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
List Interface and Linked List Mrs. Furman March 25, 2010.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
Standard Template Library
Cpt S 122 – Data Structures Abstract Data Types
18 Chapter Stacks and Queues
Chapter 12 – Data Structures
CSCI-255 LinkedList.
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
The List ADT Sections 3.2, 3.3, 3.5 Introduction
Vectors Holds a set of elements, like an array
Data Structures and Algorithms
Programming Abstractions
Stack and Queue APURBO DATTA.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Lists.
LINKED LISTS CSCD Linked Lists.
Introduction to Linked Lists
CMSC 341 Lecture 5 Stacks, Queues
Lists.
Chapter 16-2 Linked Structures
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Arrays and Linked Lists
Chapter 18: Linked Lists.
Linked List (Part I) Data structure.
Linked List Intro CSCE 121 J. Michael Moore.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Programming Abstractions
Doubly Linked List Implementation
Recursive Linked List Operations
Data Structures and Algorithms
Problem Understanding
Chapter 17: Linked Lists.
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Lecture 8 : Intro. to STL (Standard Template Library)
Containers: Queue and List
CS210- Lecture 6 Jun 13, 2005 Announcements
Linked List Intro CSCE 121.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
Lab 03 – Linked List.
The List Container and Iterators
Problem Understanding
Presentation transcript:

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?