Sei sulla pagina 1di 23

ESSENTIAL DATA STRUCTRURES

STL

STL Standard Template Library


Collections of useful classes for common data structures Ability to store objects of any type (template) Containers form the basis for treatment of data structures Container class that stores a collection of data STL consists of container classes:
Sequence containers Adapter containers Associative containers

STL Containers
Sequence Container
Stores data by position in linear order: First element, second element , etc:

Associate Container
Stores elements by key, such as name, social security number or part number Access an element by its key which may bear no relationship to the location of the element in the container

Adapter Container
Contains another container as its underlying storage structure

STL Containers
Vector Stack Queue Set, multiset Map, multimap

Vector Container
Generalized array that stores a collection of elements of the same data type Vector similar to an array
Vectors allow access to its elements by using an index in the range from 0 to n-1 where n is the size of the vector

Vector vs array
Vector has operations that allow the collection to grow and contract dynamically at the rear of the sequence

Vector
Example: Library:
#include <vector>

Declaration:
vector<int>scores(100);//100 integer scores

Vector
#include <cstdio> #include <vector> using namespace std; int main() { vector<int> v(5, 5); printf("v[2] = %d\n", v[2]); for (int i = 0; i < 5; i++) { v[i] = i; } printf("v[2] = %d\n", v[2]); v.push_back(5); printf("v[5] = %d\n", v[5]); return 0; }

// 5

// 2

// 5

Vector
//Iterator vector<int>::iterator it; //Sort sort(v.rbegin(), v.rend()); for (it = v.begin(); it != v.end(); it++) printf("%d ", *it);

Stack
A data structures which only allow insertion (push) and deletion (pop) from the top only. Stack
allows access at only one end of the sequence (top) Adds objects to container by pushing the object onto the stack Removes objects from container by popping the stack LIFO ordering (last end, first out)

Stack
Important stack operations: 1. Push (C++ STL: push())
Adds new item at the top of the stack.

2. Pop (C++ STL: pop())


Retrieves and removes the top of a stack.

3. Peek (C++ STL: top())


Retrieves the top of a stack without deleting it.

4. IsEmpty (C++ STL: empty())


Determines whether a stack is empty.

Some stack applications


To model "real stack" in computer world: Recursion, Procedure Calling, etc. Checking balanced parentheses. Postfix calculation. Converting mathematical expressions. Prefix, Infix, or Postfix.

Example
#include <stdio.h> #include <stack> using namespace std; void main() { stack<int> s; s.push(3); s.push(1); s.push(2); s.push(7); s.push(6); s.push(5); s.push(4); while (!s.empty()) { printf("%d ",s.top()); s.pop(); } printf("\n"); return 0; }

Ejercicio
Programa que realice n sumas de dos nmeros tan grandes que no quepan en ningn tipo de dato, utilice pilas.
Entrada: Salida:

2 1234 23 2345 34

1257 2379

Queue
A data structures which only allow insertion from the back (rear), and only allow deletion from the head (front). This behavior is called First In First Out (FIFO), similar to normal queue in the real world. Queue
Allows access only at the front and rear of the sequence Items enter at the rear and exit from the front FIFO ordering (first-in first-out ) push(add object to a queue) pop (remove object from queue)

Important queue operations:


1. Enqueue (C++ STL: push())
Adds new item at the back (rear) of a queue.

2. Dequeue (C++ STL: pop())


Retrieves and removes the front of a queue at the back (rear) of a queue.

3. Peek (C++ STL: front())


Retrieves the front of a queue without deleting it.

4. IsEmpty (C++ STL: empty())


Determines whether a queue is empty.

#include <stdio.h> #include <queue> using namespace std; void main() { queue<int> q; q.push(3); q.push(1); q.push(2); q.push(7); q.push(6); q.push(5); q.push(4); while (!q.empty()) { printf("%d ",q.front()); q.pop(); } printf("\n"); return 0; }

A simple calculator

Infix to posfix

Infix to postfix example

Example with parentheses

Evaluating postfix

Postfix evaluation example

Tarea
1100 - Transform the Expression

Potrebbero piacerti anche