Q. No. 1 Illustrate the meaning of following terms in 3-4 lines 1) 2) 3) 4) 5) Algorithm Abstract Data Types Dynamic Arrays List Operations Doubly Linked List 6) 7) 8) 9) 10) 10x2=20 CLO-1 Stacks Enqueue(X) and Dequeue() Priority Queue Complete Binary Tree Prefix, Infix and Postfix Ans: 1) Algorithm An algorithm is the step-by-step unambiguous instructions to solve a given problem. Examples: An algorithm to add two numbers entered by user. Step 1: Start Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2. Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2 Step 5: Display sum Step 6: Stop 2) Abstract Data Types To simplify the process of solving problems, we combine the data structures with their operations and we call this Abstract Data Types (ADTs). An ADT consists of two parts: 1. Declaration of data 2. Declaration of operations Commonly used ADTs include: Linked Lists, Stacks, Queues, Priority Queues, Binary Trees, Dictionaries, Disjoint Sets (Union and Find), Hash Tables, Graphs, and many others. 3) Dynamic Arrays The dynamic arrays are the arrays which are allocated memory at the runtime and the memory is allocated from heap. Arrays created with operator new[] have dynamic storage duration and are stored on the heap (technically the "free store"). They can have any size, but we need to allocate and free them our self since they're not part of the stack frame: int* foo = new int[10]; delete[] foo; 4) List Operations List is a set of elements in a linear order. For example, data values a1, a2, a3, a4 can be arranged in a list: (a3, a1, a2, a4) Useful operations of List ADT createList(): create a new list (presumably empty) copy(): set one list to be a copy of another clear(); clear a list (remove all elements) insert(X, ?): Insert element X at a particular position in the list remove(?): Remove element at some position in the list get(?): Get element at a given position update(X, ?): replace the element at a given position with X find(X): determine if the element X is in the list length(): return the length of the list. 5) Doubly Linked List A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list. 6) Stacks Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). 7) Enqueue(X) and Dequeue() Enqueue(X) – place X at the rear of the queue. Dequeue() -- remove the front element and return it. 8) Priority Queue Priority Queue is an extension of queue with following properties. Every item has a priority associated with it. An element with high priority is dequeued before an element with low priority. If two elements have the same priority, they are served according to their order in the queue. 9) Complete Binary Tree A complete binary tree of depth d is the strictly binary all of whose leaves are at level d. At level k, there are 2k nodes. Total number of nodes in the tree of depth d: 20+ 21+ 22 + ………. + 2d = 2j = 2d+1 – 1 In a complete binary tree, there are 2d, leaf nodes and (2d - 1) non-leaf (inner) nodes. 10) Prefix, Infix and Postfix Stacks can be used to implement algorithms involving Infix, postfix and prefix expressions. INFIX An infix expression is a single letter, or an operator, proceeded by one infix string and followed by another infix string. A A+B (A + B) + (C – D) PREFIX A prefix expression is a single letter, or an operator, followed by two prefix strings. Every prefix string longer than a single variable contains an operator, first operand and second operand. A +AB ++AB–CD POSTFIX A postfix expression (also called Reverse Polish Notation) is a single letter or an operator, preceded by two postfix strings. Every postfix string longer than a single variable contains first and second operands followed by an operator. A AB+ AB+CD Prefix and postfix notations are methods of writing mathematical expressions without parenthesis. Q. No. 2 Interpret the following short questions. I) Answer II) Answer A. 123456 III) Answer Stacks play a key role in implementation of function calls in programming languages. In C++, for example, the “call stack” is used to pass function arguments and receive return values. The call stack is also used for “local variables”. IV) Answer void push(int x) { A[++current] = x; int pop() { return A[current--]; } V) Determine and explain the functionality of the following code fragment: Answer: Binary search for item x in array a[]. Grading: Give 2 marks only if answer is correct, some explanation or hand-execution is shown, and assumption is stated. Give only 1.5 mark if just the correct answer is mentioned with some explanation. Give only 1 mark if correct answer is mentioned without any explanation or some rambling blah blah is given as explanation! Give only 0.5 marks for partial attempts that seem to be heading in the right direction. Give 0 for attempts with incorrect answers as well as incorrect logic. Purpose of question: Analysis to realize that the search will work correctly only if the array is already sorted. Expectation: Most will get the correct answer. Only a few will explicitly state the assumption of sorted array. Q. No. 3 Evaluate the following postfix expression using stack. I) Answer Q. No. 4 Answer: (a) Implementing the Queue operation deque() (b) Answer int deque() { int x = front->get(); Node* p = front; front = front->getNext(); delete p; return x; } (c) Answer #include <iostream> #include <deque> int main() { // Create a deque containing integers std::deque<int> d = {7, 5, 16, 8}; // Add an integer to the beginning and end of the deque d.push_front(13); d.push_back(25); // Iterate and print values of deque for(int n : d) { std::cout << n << '\n'; }} (d) Answer Any answer for this is acceptable and give full marks for this. (e) Answer Deque or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends. Applications of Deque: Since Deque supports both stack and queue operations, it can be used as both. The Deque data structure supports clockwise and anticlockwise rotations in O(1) time which can be useful in certain applications. Also, the problems where elements need to be removed and or added both ends can be efficiently solved using Deque. For example see Maximum of all subarrays of size k problem., 0-1 BFS and Find the first circular tour that visits all petrol pumps. A nice application of the deque is storing a web browser's history. Recently visited URLs are added to the front of the deque, and the URL at the back of the deque is removed after some specified number of insertions at the front. Another common application of the deque is storing a software application's list of undo operations. Q. No. 5 Answer: (a) Answer for stack Output: SYEUQTSAONIE Grading: Give 3 marks only if the sequence is correct and the stack state (contents) is also shown at some of the steps. Give only 2 marks if the sequence is correct but stack state is not shown. Give only 1 mark if the sequence is partially correct. Give 0 if there does not seem to be any logic behind the output. Purpose of the question: Straight-forward application of the stack operations learnt in class. Expectation: Most should get the output sequence correctly. All of them will do the push properly. Some may goof up when consecutive pop (***) are to be done. (b) Answer for Queue Output: EASYQUESTION Grading: Give 2 marks only if the sequence is correct and the queue state (contents) is also shown at some of the steps. Give only 1.5 marks if the sequence is correct but queue state is not shown. Give only 0.5 marks if the sequence is partially correct. Give 0 if there does not seem to be any logic behind the output. Purpose of the question: Straight-forward application of the queue operations learnt in class. Expectation: All should get the output sequence correctly. There is unlikely to be any goof up as in stack above.