DATA STRUCTURES ACM EXECUTIVE BODY 2k11
A series of elements of same type Placed in contiguous memory locations Can be individually referenced by adding an index to a unique identifier Example: Sort 10^7 numbers ranging from 100 to 200. ARRAY
Abstract Data Types (ADTs) or data structures or collections store data and allow various operations on the data to access and change it Why Abstract? ◦ Specify the operations of the data structure and leave implementation details to later Every collection ADT provides a way to : insert, remove, find and access a data item Abstract Data Type (ADT)
Last In First Out (LIFO) Operations ◦ Push ◦ Pop ◦ Top ◦ isEmpty (Underflow) ◦ isFull (Overflow) STACK Data 4 Data 3 Data 2 Data 1
Infix to Postfix Conversion Matching tags in XML/HTML Undo sequence in a text editor Implementing functional calls (Recursion) History of Web Browsers Applications of Stack
Balancing Of Parenthesis ◦ Given a sequence of ‘(‘ and ‘)’ ◦ Validate the sequence ◦ A sequence is valid if for every ‘(‘, there is a counter-balancing ‘)’ which occurs only later in the sequence ◦ Examples (()()()) : Valid (()(() : Invalid )()()( : Invalid Problems
Nearest Smaller Element ◦ Given a sequence of integers ◦ For every integer, output the nearest integers to the left and to the right which are smaller to it. ◦ If there is no such number, output -1. ◦ Example (-1,1) (-1,-1) (1,4) (1,-1) (4,-1) Problems
Maximum Rectangle Area in a Histogram Maximum Rectangular Area = 4x6 = 24 Problems
Water Harvesting ◦ Find the maximum volume of water that can be collected in between the adjacent buildings ◦ Maximum Volume Of Water Collected = = 9 Problems
First In First Out (FIFO) Operations ◦ Enqueue (Insert) ◦ Dequeue (Remove) ◦ Front ◦ Rear / Back ◦ isEmpty ◦ isFull Queue Data 1Data 2Data 3Data 4
Operating System Job Scheduling Breadth First Traversal of a Graph Level Order Traversal of a Binary Tree Applications
Print first 100 numbers comprising only of 1s, 3s and 5s. The first few numbers in the sequence are 1,3,5, 11,13,15,31,33,35,51,53,55, 111,113,115,131,133,135,151,153,155,311, and so on. Problem
9/14/2015 Circular Queue Complete use of space Uses single, fixed size buffer, as if it were connected from end to beginning
An generalized queue, for which elements can be added to or removed from either the front or the rear. Operations: ◦ PushBack ◦ PushFront ◦ PopBack ◦ PopFront ◦ Front ◦ Back Deque
Given an array and an integer k, find the maximum element for each and every sub- array of size k Example: ◦ a = [ 8, 5, 10, 7, 9, 4, 15, 12, 19, 13 ] ◦ k = 4 ◦ Output : [ 10, 10, 10, 15, 15, 19, 19 ] Problems on Deque
Input Restricted Deque ◦ Deletion can be made from both ends ◦ Insertion can be made at one end only Output Restricted Deque ◦ Insertion can be made at both ends ◦ Deletion can be made at one end only Variations of Deque
Standard Template Library Part of ISO standard C++ library Why Use STL ◦ Reduces Development time ◦ Highly Reliable Code ( no or small amount of debugging effort required ) ◦ Highly Robust ( Container size grows automatically ) ◦ Portable and easily maintainable STL
Templates allow the usage of generic types ◦ Advantage: no need of writing the same code all over again for a different data type Example: ◦ template // X can represent any data type that support the operations class ABC { ABC() { } public: getMax(X a, X b) { return (a > b ? a: b); } }; ABC a; max_val = a.getMax(15.30, 53.12); Templates
Including the library ◦ #include Declaration ◦ stack s; Push Operation ◦ s.push(5); Pop Operation ◦ s.pop(); Access the Top Element ◦ int x = s.top(); Check if Stack is Empty ◦ if ( s.empty() ) cout<< “ Stack is Empty ” << endl; STL - Stack
Including the library ◦ #include Declaration ◦ queue q; Push Operation ◦ q.push(5); Pop Operation ◦ q.pop(); Access the Top Element ◦ int x = q.front(); Check if Stack is Empty ◦ if ( q.empty() ) cout<< “ Queue is Empty ” << endl; STL - Queue
Including the library ◦ #include Declaration ◦ deque dq; Push Operation ◦ dq.push_back(5); ◦ dq.push_front(5); Pop Operation ◦ dq.pop_back(); ◦ dq.pop_front(); Access the front and back elements ◦ int x = dq.front(); int y = dq.back(); Check if Stack is Empty ◦ if ( dq.empty() ) cout<< “ Deque is Empty ” << endl; STL - Deque
9/14/2015 STL-Map Introduction Associative data structure ◦ a value is associated with the key Stores the values in an order, sorted by the key values. Internally implemented as a balanced BST Multimap is used if multiple associated entries are allowed for same key values.
9/14/2015 STL- Map Including the library ◦ #include Declaring a map ◦ map m; Inserting a value ◦ m.insert( pair ("abc", 2); ◦ m["abc"]=2; Finding a value ◦ if( m.find("abc") != m.end() ) cout << "Not found" << endl; Clear all entries ◦ m.clear();
9/14/2015 STL- Pair and Vector Vector ◦ Dynamic array of variables, struct or objects of same type ◦ Resizes itself when inserting or erasing an element Pair ◦ A simple associative container consisting of a 2- tuple of data elements or objects, denoted by ‘first' and ‘second'
9/14/2015 STL- Vector Including the library ◦ #include Declaration ◦ vector v; //1-D ◦ vector v[10000] ; //2-D Pushing a value ◦ v.push_back(5); //1-D ◦ v[i].push_back(5); //2-D Removing a value ◦ v.pop_back(); //1-D Accessing an element at position i ◦ int x= v[i]; //1-D Clear all entries ◦ v.clear(); //1-D
9/14/2015 STL- Pair Including the library ◦ #include Declaration ◦ pair p; Definition ◦ p = make_pair("abc",10); Accessing the elements ◦ p.first ◦ p.second
9/14/2015 STL-Set Used to store unique elements in a sorted order Elements in a set can't be modified once they has been inserted. But they can be inserted or removed from the container. Internally implemented as balanced BST Multiset can be used to allow duplicate entries
9/14/2015 STL-Set Library to be included ◦ #include Declaration ◦ set s; Inserting an element ◦ s.insert(5); Removing an element ◦ s.erase(5); Get the size of the set ◦ int x= s.size();
9/14/2015 Useful functions int a[]={1,2,2,3,5}; vector v ( a, a+5 ) ; ◦ sort ( a, a+5 );// sort an array ◦ sort ( v.begin(), v.end() );// sort a vector ◦ next_permutation( a, a+5 ); find next permutation of elements of array ◦ prev_permutation( a, a+5 ); ◦ lower_bound( v.begin(), v.end(), 2); find the first occurence of a value in a sorted container ◦ upper_bound( v.begin(), v.end(), 2 );
9/14/2015 Using Iterators Used with map, vector and set Iterator: vector ::iterator it;//declaration it = v.begin();// points to start of the container it = v.end(); //points to end of the container ◦ Other operations: it++ ; it-- ; it != m.end() ; Reverse Iterator: iterates in a reverse fashion from end to beginning vector ::reverse_iterator it; it = v.rbegin(); it = v.rend();
If you have any problem solving a question or understanding some concept, feel free to bug us, we will be happy to debug you… ACM Executive body o Nimesh Ghelani: o Prem Kamal: o Utkarsh Raj: o Rishikesh Jha: o Vivek Verma: o Nischal Kumar: o Chandan Agarwal: o Sanket Singhal: o Aparajita Choudhary: o Bhavini Mishra: Happy Coding… 9/14/2015