~~~~~~~~~~~~~~~~~~ DSA MASTER LIST ~~~~~~~~~~~~~~~~~~
UNIT – 1 1 - Linear Search 2 – Binary Search 3 – Bubble Sort 4 – Insertion Sort UNIT - 2 QUESTION – INSERTION AND DELETION IN DOUBLY LINKED LIST Possible insertion and deletion in doubly linked list: Insertion: - Front - Back - Middle/After specified node Deletion: - Front - Back - Middle/After specified node PS: Might ask program but unlikely. Insertion: At Front: The new node is added before the head of the given Linked List. And newly added node becomes the new head of DLL. ALGORITHM: Step 1: Allocate memory for newNode Step 2: Assign data to newNode Step 3: Point next of newNode to the first node of the doubly linked list Step 4: Point prev to NULL Step 5: Point previous of the first node (now first node is the second node) to newNode Step 6: Head points to newNode At Back: The new node is added after the end of the given Linked List. And newly added node becomes the new end of DLL. ALGORITHM: Step 1: Allocate memory for node Step 2: Assign data to newNode Step 3: Assign NULL to next of newNode Step 4: Store the head node temporarily (for later use) Step 5: If the linked list is empty, make the newNode as head node Step 6: If the linked list is not empty, traverse to the end of the linked list Step 7: Now, the last node of the linked list is temp Step 8: Point the next of the last node (temp) to newNode. Step 9: Assign prev of newNode to temp At Middle: The new node is added after the specified node. ALGORITHM: Step 1: check if previous node is NULL Step 2: allocate memory for newNode Step 3: assign data to newNode Step 4: set next of newNode to next of prev node Step 5: set next of prev node to newNode Step 6: set prev of newNode to the previous node Step 7: set prev of newNode's next to newNode Deletion: At Front: Deletion in doubly linked list at the beginning is the simplest operation. We just need to copy the head pointer to pointer ptr and shift the head pointer to its next. ALGORITHM: 1. Check if there is only 1 node or not 2. If there is one node: a. Assign head to NULL b. Free memory 3. Else: a. Assign head to next node in the list b. Assign head->prev to NULL c. Free memory At Back: Deletion of the last node in a doubly linked list needs traversing the list in order to reach the last node of the list and then make pointer adjustments at that position. ALGORITHM: 1. Traverse till the target node 2. Check if this is the last node i.e. if node->next = NULL, then its last node 3. Assign last node’s previous node’s next pointer to the last node’s next node’s address, which basically is NULL in this case 4. Free the memory At Middle: Removing the node which is present just after the node containing the given data. ALGORITHM: 1. Traverse till the target node 2. create a node called the previous storing previous node of the target node 3. Assign previous node’s next pointer to the next node of the target node 4. For the next node of the target node, its previous pointer is assigned to the targets node’s previous node’s address 5. Free memory of target node UNIT - 3 QUESTION 1: INFIX TO POSTFIX CONVERSION What is Infix, Postfix & Prefix? (TABLE OPTIONAL) • Infix Expression: The operator appears in-between every pair of operands. operand1 operator operand2 (a+b) • Postfix expression: The operator appears in the expression after the operands. operand1 operand2 operator (ab+) • Prefix expression: The operator appears in the expression before the operands. operator operand1 operand2 (+ab)
Why postfix representation of the expression? (OPTIONAL)
• Infix expressions are readable and solvable by humans because of easily distinguishable order of operators, but compiler doesn't have integrated order of operators. • The compiler scans the expression either from left to right or from right to left. • Consider the below expression: a op1 b op2 c op3 d If op1 = +, op2 = *, op3 = + then a + b *c +d • The compiler first scans the expression to evaluate the expression b * c, then again scan the expression to add a to it. The result is then added to d after another scan. • The repeated scanning makes it very in-efficient. It is better to convert the expression to postfix (or prefix) form before evaluation. • The corresponding expression in postfix form is: abc*+d+. The postfix expressions can be evaluated easily in a single scan using a stack. Associativity & Precedence: 1. ( ) { } [ ] 2. ^ ; Right to left 3. * / ; Left to right 4. + - ; Left to right Algorithm: Step 1: Scan the Infix Expression from left to right. Step 2: If the scanned character is an operand, append it with final Infix to Postfix string. Step 3: Else, Step 3.1: If the precedence order of the scanned(incoming) operator is greater than the precedence order of the operator in the stack [or the stack is empty or the stack contains a ‘(’], push it on stack. Step 3.2: Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.) Step 4: If the scanned character is an ‘(‘, push it to the stack. Step 5: If the scanned character is an ‘)’, pop the stack and output it until a ‘)’ is encountered, and discard both the parenthesis. Step 6: Repeat steps 2-6 until infix expression is scanned. Step 7: Print the output Step 8: Pop and output from the stack until it is not empty. Example: IMPORTANT: Might ask your own example (or) will give an expression themselves to solve. UNIT - 4 1 – Binary search tree: (Definition, Construction, Searching, Insertion, Deletion) DEFINITION: CONSTRUCTION: SEARCHING: Searching means finding or locating some specific element or node within a data structure. However, searching for some specific node in binary search tree is pretty easy due to the fact that, element in BST are stored in a particular order. ALGORITHM: 1.Compare the element with the root of the tree. 2.If the item is matched then return the location of the node. 3.Otherwise check if item is less than the element present on root, if so then move to the left sub- tree. 4.If not, then move to the right sub-tree. 5.Repeat this procedure recursively until match found. 6.If element is not found then return NULL. INSERTION: Insert function is used to add a new element in a binary search tree at appropriate location. Insert function is to be designed in such a way that, it must node violate the property of binary search tree at each value. ALGORITHM: DELETION: Delete function is used to delete the specified node from a binary search tree. However, we must delete a node from a binary search tree in such a way, that the property of binary search tree doesn't violate. There are three situations of deleting a node from binary search tree. ALGORITHM: 1. Input the number of nodes. 2. Input the nodes of the tree. 3. Consider the first element as the root element and insert all the elements. 4. Input the data of the node to be deleted. 5. If the node is a leaf node, delete the node directly. 6. Else if the node has one child, copy the child to the node to be deleted and delete the child node. 7. Else if the node has two children, find the in-order successor of the node. 8. Copy the contents of the in-order successor to the node to be deleted and delete the in-order successor. UNIT - 5 1 – Hashing Definition: Hashing is a technique or process of mapping keys, and values into the hash table by using a hash function. It is done for faster access to elements. The efficiency of mapping depends on the efficiency of the hash function used. Hash Table: Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data. Hash Functions: 1. Division method 2. Mid Square 3. Multiplicative hash function 4. Digit folding