180 likes | 188 Views
COSC 2030. Built in Data Structures. Standard template library. Many languages have a set of libraries for some data structures. They follow a set of guide lines to ensure that the efficient as well. We'll look at some of the STL for C++ and some python. std ::list.
E N D
COSC 2030 Built in Data Structures
Standard template library. • Many languages have a set of libraries for some data structures. • They follow a set of guide lines to ensure that the efficient as well. • We'll look at some of the STL for C++ and some python.
std::list • Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions. • List containers are implemented as double-linked list. • Note, there is a forward_list. It's a single-linked lists. It's smaller and more efficient, but only has forward iterators.
std:list • #include <list> • operations • empty: returns t/f if empty. • clear: empties the list. • push_front: add at the beginning of the list • pop_front: delete first element. Note, no return value • front: first element • back: last element • push_back: add at the end of the list • pop_back: delete the last element • insert: giving an iterator, insert a new element before it. • erase: giving an iterator, remove that element. • remove: given a value, removes all elements that are equal to the value.
std:list example #include <iostream> #include <list> int main () { std::list<int> mylist(1,100); //add 1 element of 100 mylist.push_front(200); mylist.push_front(300); std::cout << "mylist contains:"; for (std::list<int>::iterator it=mylist.begin(); it != mylist.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; std::cout <<"front is " <<mylist.front()<<'\n'; mylist.pop_front(); std::cout <<"front is " <<mylist.front()<<'\n'; return 0; } • output mylist contains: 300 200 100 front is 300 front is 200
std:stack • include <stack> • The underlining container class maybe a vector, deque, or a list. The standard doesn't specify which one. Only that it has a standard set of operations. • operations: • constructor: empty or with a list( ) to initialize the stack. • std::stack<int> mystack; • bool empty() • mystack.empty(); • size_type size() • basically int size, but more specific more other data structures. • mystack.size(); • void push ( value) • mystack.push(10); • void pop() • mystack.pop(); //can be called even only empty stacks. • type & top • int t = mystack.top(); • mystack.top() = 5; //only if stack is not empty.
std:stack example #include <iostream> #include <stack> int main () { std::stack<int> mystack; for (inti=0; i<5; ++i) mystack.push(i); std::cout << "elements: "; while (!mystack.empty()) { std::cout << ' ' << mystack.top(); mystack.pop(); } std::cout << '\n'; return 0; } • output elements: 4 3 2 1 0
std: queue • include <queue> • The underlining container class maybe a deque or a list. The standard doesn't specify which one. Only that it has a standard set of operations. • operations: • constructor: empty or with a list( ) to initialize the stack. • std::queue<int> myqueue; • bool empty() • size_type size() • basically int size, but more specific more other data structures. • myqueue.size(); • void push ( value) • myqueue.push(10); • void pop() • myqueue.pop(); //can be called on empty queues. • type front • int t = myqueue.front(); • type back • int t = myqueue.back();
std: priority_queue • which is a variant on the queue container. • Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion. • two changes • std::priority_queue<int> mypq; • use top() instead of front()
queue examples #include <iostream> #include <queue> int main () { std::queue<int> mypq; mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); std::cout << "elements: "; while (!mypq.empty()) { std::cout << ' ' << mypq.front(); mypq.pop(); } std::cout << '\n'; return 0; } output: elements: 30 100 25 40 #include <iostream> #include <queue> int main () { std::priority_queue<int> mypq; mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); std::cout << "elements: "; while (!mypq.empty()) { std::cout << ' ' << mypq.top(); mypq.pop(); } std::cout << '\n'; return 0; } output: elements: 100 40 30 25
std: vector • Vector • Vectors are sequence containers representing arrays that can change in size. • they use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in array • Vectors are more efficient then dequesand lists for access, like arrays. • Adding and remove from the end is also efficient • adding and removing other positions performs far worse then lists and deques. • Has the full set of operations like lists, plus .at and [] operator • full set of iterators as well.
std: deque • deque (usually pronounced like "deck") is an irregular acronym of double-ended queue. • sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). • They provide a functionality similar to vectors, but with efficient insertion and deletion of elements also at the beginning of the sequence, and not only at its end. • Like vectors they have the full set of operations and iterators • .at() method and [] operator.
std: deque example #include <iostream> #include <deque> int main () { std::deque<int> mydeque (1,100); //insert 1 element of 100 mydeque.push_front (200); mydeque.push_front (300); std::cout << "elements:"; for (std::deque<int>::iterator it = mydeque.begin(); it != mydeque.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; std::cout <<"element 1 is " <<mydeque[1]<<'\n'; return 0; } output: elements: 300 200 100 element 1 is 200
Question • Why use the deque instead of vector? • note, the deque is an industry standard for c++. • versions of a deque data structure exist for other languages as well.
using a deque • So if we choose to use a deque instead of a stack or queue data structure then • What functions would be used to treat it as a stack only? • What function would be used to treat it as a queue only?
python list • python doesn't have any support for arrays. Instead it all a collection called lists. • List is a collection which is ordered and changeable. Allows duplicate members. https://www.w3schools.com/python/python_lists.asp • example: thislist = ["apple", "banana", "cherry"] for x in thislist: print(x) • output: apple banana cherry
python deque • part of the High-performance container datatypes: collections • https://docs.python.org/2/library/collections.html • short example: from collections import deque d = deque(["apple", "banana"]) # make a new deque with two items d.append("cherry") #note also appendleft() for elem in d: # iterate over the deque's elements print elem • output: cherry apple banana
Q A &