41
Basic ADTs in STL What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team http://academy.telerik.com Telerik Software Academy

What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy

Embed Size (px)

Citation preview

Page 1: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Basic ADTs in STLWhat are ADTs,

STL Intro, vector, list, queue, stack

Learning & Development Teamhttp://academy.telerik.com

Telerik Software Academy

Page 2: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Table of Contents

1. Basic Abstract Data Types

1.List

2.Linked List

3.Stack

4.Queue

2. ADTs in STL

1.STL Intro

2.Iterators

3.Vector, list, stack, queue2

Page 3: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Abstract Data Types

Basic Data Structures

Page 4: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Abstract Data Types An Abstract Data Type (ADT) is a data type together with the operations, whose properties are specified independently of any particular implementation

ADT are set of definitions of operations Can have several different implementations Different implementations can have different efficiency

Page 5: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Basic Data Structures Linear structures

Lists: fixed size and variable size Stacks: LIFO (Last In First Out) structure Queues: FIFO (First In First Out) structure

Trees Binary, ordered, balanced, etc.

Dictionaries (maps) Contain pairs (key, value) Hash tables: use hash functions to search/insert

Page 6: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Lists

Page 7: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

The List ADT Data structure (container) that containsa sequence of elements Can have variable size Elements are arranged linearly, in sequence

Can be implemented in several ways Statically (using array fixed size) Dynamically (linked implementation) Using resizable array

Page 8: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Static List Implemented by an array

Direct access by index (fast) Insertion and deletion and resizing

are slow operations

L 2 18 7 1

2 3 6 11 9

0 1 2 3 4 5 6 7

Page 9: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Linked List Dynamic (pointer-based) implementation

Direct access to first/last element No access by index

go through all previous elements (slow)

Insertion and deletion are fast Resizing – add new element at the end or beginning

2

next

7

next

head

4

next

5

next

null

Page 10: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Stacks

Page 11: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

The Stack ADT LIFO (Last In First Out) structure Elements inserted (push) at “top” Elements removed (pop) from “top” Useful in many situations

E.g. the execution stack of the program Can be implemented in several ways

Statically (using array) Dynamically (linked implementation)

Page 12: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Queues

Page 13: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

The Queue ADT FIFO (First In First Out) structure Elements inserted at the tail (Enqueue)

Elements removed from the head (Dequeue)

Useful in many situations Print queues, message queues, etc.

Can be implemented in several ways Statically (using array) Dynamically (using pointers)

Page 14: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Standard Template LibraryIntroduction, Basic ADT

Implementations

Page 15: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Introduction Standard Template Library

C++ Library

Implements a lot of computer science fundamentals Container classes, Algorithms

Iterators

Mostly template-based

Algorithms decoupled from containers through iterators

15

Page 16: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Introduction Containers

Data structures, hold collections of elements

Different benefits and downsides

Implement fundamental Abstract Data Types Sequence, Associative, String,

Adaptors…

Iterators Provide access to container

elements

Used to "traverse" containers

16

Page 17: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Introduction Algorithms

Fundamental algorithms over collections or single

Hook up with iterators to access container elements

Function objects Wrap functions/methods into

objects

Implement () operator – called like functions

Called by algorithms and containers to act over elements (elements passed as parameters)

17

Page 18: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL IntroductionLive Demo

Page 19: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL IteratorsMechanism for traversing container

elements

Page 20: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Iterators "Smart" pointers to objects Specific for each container type

Each container defines how it’s iterators work

If we have an iterator to one element Increase/decrease it to get the

other elements

Types: Input, Output

Forward iterator, Bidirectional iterator

Random access iterator

20

Page 21: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL ContainersFast data structures to store elements

Page 22: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Containers Store collections of other objects Has methods to access elements Types

By traversability: Forward – elements ordered, Forward

Iterators

Reversible – have Bidirectional iterators

Random access – have Random access iterators

By storage: sequence, adaptors, associative

22

Page 23: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Basic STL ContainersVector, List, Deque, Queue, Stack

Page 24: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Vector Vector (#include <vector>)

Defined: template <class T> vector

Sequence, Random Access

Stores a sequence of elements in contiguous memory

Manages memory effectively

Fast at retrieving elements by index and adding elements at the end

Slow insertion/deletion in middle or beginning

24

Page 25: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Vector Declaring and initializing a vector

Vector size and is obtained by calling size()

#include<vector> //required header…vector<int> numbers;numbers.push_back(42); //numbers is now {42}numbers.push_back(13); //numbers is now {42, 13}int consoleNumber; cin>>consoleNumber;numbers.push_back(consoleNumber)

vector<int> numbers;numbers.push_back(42);numbers.push_back(13);cout<<numbers.size(); //prints 2

Page 26: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Vector Accessing vector elements

Done the same way as with arrays, i.e. []

Traversing a vector is the same as traversing an array (e.g. with a for loop)

Element access does not depend on vector size

vector<int> numbers;numbers.push_back(42);numbers.push_back(13);cout<<numbers[1]; //prints 13cout<<endl;numbers[1] = numbers[0];cout<<numbers[1]; //prints 42

Page 27: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL vectorLive Demo

Page 28: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL (Linked) List List (#include <list>)

Defined: template <class T> list

Sequence, Reversible

Stores a sequence of elements in a doubly-linked list

Fast at deletion/insertion anywhere

No random access to elements Have to traverse list to get to an

item

28

Page 29: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL (Linked) List Declaring and initializing a list

List size and is obtained by calling size()

List elements can be removed from front and back fast

list<int> numbers;

numbers.push_back(2);numbers.push_front(1);numbers.push_back(3);

numbers.pop_front();numbers.pop_back();

Page 30: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL (Linked) List Accessing list elements

front and back methods provide first and last element access

Only way to get access to all elements – traversal by iterator

cout<<numbers.front();cout<<numbers.back();

list<int>::iterator numbersIterator;for(numbersIterator = numbers.begin(); numbersIterator != numbers.end(); numbersIterator++){ cout<<*numbersIterator<<endl;}

Page 31: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL listLive Demo

Page 32: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Queue Queue (#include<queue>)

Defined: template <class T> queue

Sequence Adaptor

First in, First out structure (FIFO)

Stores a sequence of elements

Provides access only to first element

Can remove only at front

Can add only at back32

Page 33: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Queue Declaring and initializing a queue

Queue size is obtained by calling size()

Queues allow removing elements only from the front of the sequence

queue<int> q;

q.push(1);q.push(2);q.push(3);

q.pop();

Page 34: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Queue Accessing queue elements

front and back methods provide first and last element access

Other types of access to queue elements are meaningless

The idea of the queue is to restrict access and be FIFO

cout<<q.front();cout<<q.back();

Page 35: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL queueLive Demo

Page 36: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Stack Stack (#include <stack>)

Defined: template <class T> stack

Sequence adaptor

Last in, First out structure (LIFO)

Stores a sequence of elements

Provides access only to last element

Can remove or add elements only at back/top

36

Page 37: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Stack Declaring and initializing a stack

Stack size is obtained by calling size()

Stacks allow removing elements only from the back (top) of the sequence

stack<int> s;

s.push(1);s.push(2);s.push(3);

s.pop();

Page 38: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL Stack Accessing stack elements

top method provides first element access

Other types of access to stack elements are meaningless

The idea of the stack is to restrict access and be LIFO

cout<<s.top();

Page 39: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

STL stackLive Demo

Page 40: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезания

ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGap

free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно

?

? ? ??

?? ?

?

?

?

??

?

?

? ?

Questions?

?

Basic ADTs in STL

http://algoacademy.telerik.com

Page 41: What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team  Telerik Software Academy

Exercises

41

1. …