Download presentation

Presentation is loading. Please wait.

Published byBraeden Felice Modified over 3 years ago

1
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing elements by sequence (not by index) -queues, stacks, linked lists -Require iterators to locate elementsContainers

2
In principle one can solve any problem with arrays or vectors. In practice queues, stacks and linked lists are more convenient and more efficient performance- wise for select applications. Why Sequential Containers?

3
Suppose we are dealing with N points organized in array. To copy them from one location to another will take an order of N operations – O(N). To get i-th point – pt[i] – from array would always take the same constant time – O(1). To sort the array via brute force approach would take order N*N operations – O(N 2 ). To sort the array via bubble sort would take order N*log 2 (N) – O(NlogN). Efficiency: Big-O Notation

4
Huge collection of generic classes at your disposal! string, vector, queue, dequeue, list, etc. Include files match class names. Type class name and click F1 for reference. Include header file then use IntelliSense. Standard Template Library (STL)

5
#include string s1, s2; cin >> s1 >> s2; cout << (s1 + s2); size() returns string size;, =, !=, and == comparison operators; > stream I/O operators; + Concatenates two string objects; [ ] index operator for character access. string class (STL)

6
#include vector v; v.push_back(1); size(), capacity() returns vector size, capacity; insert() inserts element at specified pos; erase() removes element at specified pos; push_back() adds element at the end; pop_back() removes element at the end. begin() returns position of the first element; end() returns position of the last element. [] index operator Generic vector class (STL)

7
vector is cool, but insert() and erase() operations operate in linear time – O(N). What if we need to insert and remove quickly? We can use linked list to insert and remove in constant time – O(1)! Linked List: Rationale

8
Linked List: Illustration Max Lex Nika Vlad Joe insert move down to make room vector Max Lex Nika Vlad Joe insert linked list Max Lex Nika Vlad Joe

9
Each list node holds data and a pointer to the next list node. In the last node the pointer to the next node is NULL. Linked List: Node Data SomeData Value; Node* Next; Node

10
Node* node = listHead; do { // do something with current node … // next node node = node->Next } while ( node != NULL); Linked List: Traversing

11
Double-Linked List: Illustration Max Lex Nika Vlad Double-linked list

12
Each list node holds data and a pointer to the next and previous list node. In the last node the pointer to the next node is NULL. In the first node the pointer to the previous node is NULL. Double-Linked List: Data SomeData Value; Node* Next; Node Node* Previous;

13
Double-linked list #include list myList; size() returns list size; insert() / erase() inserts / removes element at specified pos; push_front() adds element at the beginning; push_back() adds element at the end; pop_front() removes element at the beginning; pop_back() removes element at the end; begin() / end() returns position of the first / last element; end() returns position of the last element. Iterator - iterator for the list ++ next element in the list -- previous element in the list * current element Generic list Class (STL)

14
#include using namespace std; list myList; for ( list ::iterator pos = myList.begin(); pos != myList.end(); ++pos ) { // Current element string s = *myList; } list Traversal Example

15
// Prefix: ++iter; // Modifies self and returns the NEW value iterator& operator++(); // Postfix: iter++; // Modifies self, but returns a copy of OLD self created prior to the modification iterator operator++(int); for (; ; ++pos ) // More efficient (no copying) for (; ; pos++ ) // Equivalent to prefix++ in this loop MyFunc(pos++); // Firtst MyFunc is called then pos++ MyFunc(++pos); // First pos++ then MyFunc is called Postfix / Prefix ++ Operator

16
Using string and list -Read 5 strings from cin -store them in the list in alphabetically sorted order – HOW?? -Print the list (which must be sorted) TIP: You can use, = operators for string comparison. Todays Lab

17
Read chapter 4, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.Assignment

Similar presentations

OK

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

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

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on network switching systems Converter pub to ppt online reader Ppt on political parties and electoral process in usa Ppt on question tags in english grammar Ppt on fourth and fifth state of matter Ppt on line drawing algorithms Ppt on dressing etiquettes meaning Ppt on project management training Download ppt on nutrition in plants for class 7 Free ppt on balanced diet