2
STACK USING ARRAYS Let us take an array a[5] and take a variable top points to -1. PUSH:  To INSERT the element in to stack using top.  Here we check for the OVERFLOW condition. POP:  To RETRIEVE elements from the stack using top.  Here we check for the UNDERFLOW condition. This concept is nothing but LIFO. SOURCE CODE: /* Program To Implement Stack using Array */ #include #define MAX 10

7
STACK USING LINKED LIST We will create a linked list and insert an element ‘10’ and address as ‘0’.using top for the first node. For second node insert data element ‘20’ and insert first node address at second node address field. For third node insert data element ‘30’ and insert second node address at third node address field. after thirty we will stop the process. If we want to print the elements 30,20,10 will be displayed, Thiss follows LIFO conceot.

11
QUEUE USING ARRAYS Here we will take an array a[5],and two variables front, rear points to -1. WRITE:  Here will insert the element into the queue using rear variable.  Here check for the Overflow condition. READ:  Here we will retrieve the elements from the queue using front variable.  Here check for the Underflow condition. This follows the FIFO concept. 01234 rear front

20
BINARY TREE USING RECURSION A binary tree is a tree data structure in which each node has at most two children. Typically the child nodes are called left and right. Binary trees are commonly used to implement binary search trees and binary heaps. Starting at the root of a binary tree, there are three main steps that can be performed and the order in which they are performed define the traversal type. There are 3 types of traversals:  1. Pre-Order  2. In-Order  3. Post-Order To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node: 1.Visit the root. 2.Traverse the left sub tree. 3.Traverse the right sub tree. To traverse a non-empty binary tree in in order, perform the following operations recursively at each node, starting with the root node: 1.Traverse the left sub tree. 2.Visit the root. 3.Traverse the right sub tree. To traverse a non-empty binary tree in post order, perform the following operations recursively at each node, starting with the root node: 1.Traverse the left sub tree. 2.Traverse the right sub tree. 3.Visit the root.

21
15 722 BINARY TREE: Preorder:- 15,7,22 will be displayed. Post order:- 7,22,15 will be displayed. In order:- 7,15,22 will be displayed.

33
SPARSE MATRIX AIM: Write a program in C++ to implement ADDITION and MULTIPLICTION of two SPARSE matrixes. THEORY: If a lot of elements from a matrix have a value 0 then the matrix is known as SPARSE MATRIX. If the matrix is sparse we must consider an alternate way of representing it rather the normal row major or column major arrangement. This is because if majority of elements of the matrix are 0 then an alternative through which we can store only the non-zero elements and keep intact the functionality of the matrix can save a lot of memory space. Example: Sparse matrix of dimension 7 x 7. COLUMNS 0123456 0000-5000 10400007 20000900 ROWS30302000 41020000 50000000 60080000

34
A common way of representing non-zero elements of a sparse matrix is the 3-tuple forms. In this form each non-zero element is stored in a row, with the 1 st and 2 nd element of this row containing the row and column in which the element is present in the original matrix. The 3 rd element in this row stores the actual value of the non- store element. For example 3-tuple representation of the sparse matrix as shown in below. int spmat[10][3]={ 7,7,9, 0,3,-5, 1,1,4, 1,6,7, 2,4,9, 3,1,3, 3,3,2, 4,0,11, 4,2,2, 6,2,8 }

40
INFIX TO POSTFIX CONVERTIONic Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P. Step 1. Push “(“ onto stack and add “)” to the end of Q. 2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until the stack is empty. 3. If an operand is encountered, add it to p. 4. If a left parenthesis is encountered,push it onto stack. 5 If an operator * is encountered, then: a. repeatedly pop from stack and top each operator (on the top of stack ) which has the same precedence or higher precedence than *. b. Add * to stack. 6. If a right parenthesis is encountered, then: a. repeatedly from stack and add to P each operator (on the top of stack) until a left parenthesis is encountered. b. remove the left parenthesis [ Do not add the left parenthesis top] [End of if structure] [End of step 2 loop] 7. Exit

46
POSTFIX EVALUATION THEORY: Reverse Polish notation is a mathematical notation wherein every operator follows all of its operands. It is also known as Postfix notation and is parenthesis free. In Reverse Polish notation the operators follow their operands; for instance, to add three and four, one would write “3 4 +” rather than “3 + 4”. If there are multiple operations, the operator is given immediately after its second operand; so the expression written “3 − 4 + 5” in conventional infix notation would be written “3 4 − 5 +” in RPN: first subtract 4 from 3, then add 5 to that. Infix Expression: Any expression in the standard form like "2*3-4/5" is an Infix(In order) expression. Postfix Expression: The Postfix(Post order) form of the above expression is "23*45/-". Postfix Evaluation: In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. The algorithm for the conversion is as follows: Scan the Postfix string from left to right. Initialize an empty stack. If the scanned character is an operand, add it to the stack. If the scanned character is an operator, there will be at least two operands in the stack  If the scanned character is an Operator, then we store the top most element of the stack(topStack) in a variable temp. Pop the stack. Now evaluate topStack(Operator)temp. Let the result of this operation be retVal. Pop the stack and Push retVal into the stack.  Repeat this step till all the characters are scanned. After all characters are scanned, we will have only one element in the stack. Return topStack.

47
StackExpression Example: Postfix String: 1 2 3 * + 4 -. Initially the Stack is empty. Now, the first three characters scanned are 1,2 and 3, which are operands. Thus they will be pushed into the stack in that order. Next character scanned is "*", which is an operator. Thus, we pop the top two elements from the stack and perform the "*" operation with the two operands. The second operand will be the first element that is popped. The value of the expression(2*3) that has been evaluated(6) is pushed into the stack. StackExpression StackExpression

48
StackExpression Next character scanned is "+", which is an operator. Thus, we pop the top two elements from the stack and perform the "+" operation with the two operands. The second operand will be the first element that is popped. The value of the expression(1+6) that has been evaluated(7) is pushed into the stack. Next character scanned is "4", which is added to the stack. Next character scanned is "-", which is an operator. Thus, we pop the top two elements from the stack and perform the "-" operation with the two operands. The second operand will be the first element that is popped. StackExpression StackExpression

49
StackExpression The value of the expression(7-4) that has been evaluated(3) is pushed into the stack. Now, since all the characters are scanned, the remaining element in the stack (there will be only one element in the stack) will be returned. End result: Postfix String : 1 2 3 * + 4 - Result : 3 StackExpression

55
STEP7: 2711346892134 left, pivot right STEP8: 2711346892134 left, pivot right STEP9: 273 1146892134 left right, pivot STEP10: 273 1146892134 left, right, pivot Here we will stop the main process as the left and right pointers are equal. Now see the elements left to ‘11’ are less than ‘11’ and elements right to ‘11’ are grater than ‘11’. Now divide the main list into 2 sub lists such as(2,7,3) and (46,89,21,34) and do the same above process.

59
Selection sort Consider the elements as shown, 7733441188226655 min i Here min is compared with a[1]  as min is > a[1]  min=a[1] min i This min is compared with a[2],as this is < a[2]  min is same that is 33 This min is compared with a[3],as this is > a[3]  min =a[3]. min I Now this is compared with a[4],a[5],a[6],a[7] as min is less than all of these min remains 33 At last swap min and a[i] like this continue the process with i=1,2,3…… 77 0 330 110

62
10 20 30 40 50 60 70 LINEAR SEARCH 0 1 2 3 4 5 6 Here we want to search for ‘50’. So compare ’50’ with a[i] where i=0,1,2,3,….  If (a[i]==50) Then element is found at location i that is 4 Else i++ Here the time complexity is O(n).

75
SINGLE LINKED LIST THEORY: Figure shows a Linked List. Each item in the list is called a node and contain two fields, a data field and a next address field. The data field holds the actual element on the list. The next address field contains the address of the next node in the list. Such an address which is used to access a particular node, is known as a pointer. The entire linked list is accesses from an external pointer list, that points to the first node in the list. The next field of last node in the list contains a special value, known as NULL. The null pointer is used to signal the end of the list. The singly-linked list is the most basic of all the linked data structures. A singly-linked list is simply a sequence of dynamically allocated objects, each of which refers to its successor in the list. Despite this obvious simplicity, there are myriad implementation variations. The following code inserts a node after an existing node in a singly linked list. The diagram shows how it works. Inserting a node before an existing one cannot be done; instead, you have to locate it while keeping track of the previous node.

76
Similarly, we have functions for removing the node after a given node, and for removing a node from the beginning of the list. The diagram demonstrates the former. To find and remove a particular node, one must again keep track of the previous element.

85
SINGLE CIRCULAR LINKED LIST THEORY: The linked list that we have seen so far is often know as linear lists. The elements of such a linked list can be accessed, first by setting up a pointer pointing to the first node in the list and then traversing the entire list using this pointer. Although a linear linked list is a useful data structure, it has several shortcomings.

95
DOUBLE LINKED LIST AIM: Write a program in C++ to implement DOUBLE LINKED LIST THEORY: A two-way list is a linear collection of data elements, called nodes, where each node N is divided into three parts: An item data field. A pointer field next which contains the location of the next node in the list. A pointer field prev which contains the location of the previous node in the list. The list requires two list pointer variables: FIRST, which points to the first node in the list, and LAST, which points to the last node in the list. The figure contains a schematic diagram of such a list. Observe that the null pointer appears in the next field of the last node in the list and also in the prev field of the first node in the list. Observe that, using the variable FIRST and the pointer field next, we can traverse a two-way list in the forward direction as before. On the other hand, using the variable LAST and the pointer field prev, we can also traverse the list in the backward direction.

106
GRAPH TRAVERSING: DEPTH FIRST SEARCH THEORY: DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hadn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a LIFO stack for exploration. Space complexity of DFS is much lower than BFS (breadth-first search). It also lends itself much better to heuristic methods of choosing a likely-looking branch. Time complexity of both algorithms are proportional to the number of vertices plus the number of edges in the graphs they traverse (O(|V| + |E|)).

110
GRAPH TRAVERSING: BREADTH FIRST THEORY: BFS is an uninformed search method that aims to expand and examine all nodes of a graph or combinations of sequence by systematically searching through every solution. In other words, it exhaustively searches the entire graph or sequence without considering the goal until it finds it. From the standpoint of the algorithm, all child nodes obtained by expanding a node are added to a FIFO queue. In typical implementations, nodes that have not yet been examined for their neighbors are placed in some container (such as a queue or linked list) called "open" and then once examined are placed in the container "closed".  Algorithm for Breadth First Search 1.Enqueue the root node. 2.Dequeue a node and examine it. If the element sought is found in this node, quit the search and return a result. Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered. 3.If the queue is empty, every node on the graph has been examined – quit the search and return "not found". 4.Repeat from Step 2.