1 / 18

COSC 2030

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.

dtommy
Download Presentation

COSC 2030

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COSC 2030 Built in Data Structures

  2. 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.

  3. 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.

  4. 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.

  5. 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

  6. 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.

  7. 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

  8. 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();

  9. 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()

  10. 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

  11. 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.

  12. 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.

  13. 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

  14. 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.

  15. 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?

  16. 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

  17. 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

  18. Q A &

More Related