Telerik Software
Academy
Learning &
http://academy.telerik.com
Development Team
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, queue 2
Abstract Data
Types
Basic Data Structures
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
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
Lists
The List ADT
Data structure (container) that
contains
a 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)
Static List
Implemented by an array
Direct access by index (fast)
Insertion and deletion and resizing
are slow operations
0 1 2 3 4 5 6 7
L 1 1 1
2 7 3 6 9
8 2 1
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 7 4 5 null
15
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
16
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
17
STL Introduction
Live Demo
STL Iterators
Mechanism for traversing container
elements
STL Iterators
"Smart" pointers to objects
Specific for each container type
Types
By traversability:
Forward elements ordered, Forward
Iterators
Reversible have Bidirectional
iterators
Random access have Random
access iterators
By storage:
22
Basic STL Containers
Vector, List, Deque, Queue, Stack
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
STL Vector
Declaring and initializing a vector
#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 size and is obtained by
calling size()
vector<int> numbers;
numbers.push_back(42);
numbers.push_back(13);
cout<<numbers.size(); //prints 2
STL Vector
Accessing vector elements
Done the same way as with arrays,
i.e. []
vector<int> numbers;
numbers.push_back(42);
numbers.push_back(13);
cout<<numbers[1]; //prints 13
cout<<endl;
numbers[1] = numbers[0];
cout<<numbers[1]; //prints 42
28
STL (Linked) List
Declaring and initializing a list
list<int> numbers;
numbers.push_back(2);
numbers.push_front(1);
numbers.push_back(3);
32
STL Queue
Declaring and initializing a queue
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
36
STL Stack
Declaring and initializing a stack
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
?
?
?
? ?
Questions?
?
?
?
?
?
?
? ?
?
?
http://algoacademy.tele
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
SEO - ,
, iPhone, Android, W P7, PhoneGap
, HTML, CSS, JavaScript, Photoshop -
free C# book, C#, Java, C# -
" "
" cloud " C# , ,
Exercises
1.
41