Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Data Structures Through C Laboratory Manual II YEAR B.TECH (CSE, IT) Department of Computer Science & Engineering Data Structures Through C Laboratory Manual II YEAR B.TECH (CSE, IT) Department of Computer Science & Engineering Document No: Date of Issue Compiled by 30 JUNE 2012 B. Madhuravani Authorized by (Assistant Professor) MLRIT/CSE/LAB MANUAL/DSC Date of Revision VERSION 1.1.3 JUNE 2013 Verified by Mr. Niladri 2|Page HOD(CSE) PREFACE DS Through C is one of the important subjects included in the second year curriculum by JNTU, Hyderabad. In addition to theory subject also includes DS Through C as lab practical’s using C language. The DS Through C constitutes step-by-step procedure encompassing understanding of the problem, developing logic to solve the problem in the form of Algorithms and converting logic into source code using C language. The students must grasp three aspects while doing DS Through C in the lab as given below. a. Understanding grammar of C language to enable writing correct program from syntax point of view. b. Developing the logic to find solution to a given problem from semantic point of view. c. Importance of space and time complexity to enable writing a program which meets requirements of memory constraints and time constraints. Students will be in a position to grasp the above three aspects while doing lab practical’s as defined in the manual through four steps i.e design and code, compile, linkage and run and test. This manual is a collective effort of the faculty teaching second year DS Through C subject. This manual will need constant up gradation based on the student feedback and change in the syllabus. HOD (IT) 3|Page HOD (CSE) DEAN PRINCIPAL Steps in Program Development Program development is a multi step process. The steps are 1) Understand the problem 2) Develop a solution 3) Write the program and test it. 1. Understand the problem: When you are assigned to develop a program for solving a problem, you are given the programming requirements. Study these requirements carefully resolve all doubts and ensure that you have understood it fully as per the user requirements. 2. Develop a solution: Once you have understood the problem, you have to develop the solution in terms of some programming language. The tools that help in this are: a) Algorithm / Pseudo code b) Flowchart c) Programming Language, like C etc., d) Test cases a) Algorithm Definition: A method of representing the step-by-step logical procedure for solving a problem in natural language (like English, etc.) is called as an Algorithm. Algorithm can also be defined as an ordered sequence of well-defined and effective operations that, when executed, will always produce a result and eventually terminate in a finite amount of time. On the whole an algorithm is a recipe for finding a right answer to a problem by breaking it down into simple steps. Properties an Algorithm should possess: a. Generality: The algorithm must be complete in itself so that it can also be used to solve all problems of a specific type for given input data. b. Input / Output: Each algorithm can take zero, one or more input data and must produce one or more output values. c. Optimization: Unnecessary steps should be eliminated so as to make the algorithm to terminate in finite number of steps. d. Effectiveness: Each step must be effective in the sense that it should be primitive (easily convertible into program statement) and can be performed exactly in a finite amount of time. e. Definiteness: Each step of the algorithm, should be precisely and unambiguously stated. 4|Page b) Flowchart: Definition: It is a diagrammatic representation of an algorithm. It is constructed using different types of symbols. Standard symbols used in drawing flowcharts: Oval Parallelogram Document Rectangle Diamond Circle Arrow 5|Page Terminal Input/output Printout Process Decision Connector Flow Start/stop/begin/end Making data available for processing(input) or recording of the processed information(output) Show data output in the form of document Any processing to be performed. A process changes or moves data. An assignment operation Decision or switching type of operation Used to connect different parts of flowchart Joins two symbols and also represents flow of execution Bracket with broken line Double sided rectangle Annotation Predefined process Descriptive explanation comments Modules or subroutines specified elsewhere Programming and Testing: A computer program is the sequence of instructions written in a computer language according to the algorithm and computer follows these in carrying out its computations. The process of writing a program is called programming. Steps involved in Computer Programming: Problem Understanding Problem Definition Program writing Error analysis Validation & Verification Documentation & Maintenance 6|Page or LAB CODE 1. Student should report to the concerned as per the time table. 2. Students who turn up late to the labs will in no case be permitted to the program schedule for the day. 3. After completion of the program, certification of the concerned staff in-charge in the observation book is necessary. 4. Student should bring a note book of 100 pages and should enter the readings/observations into the note book while performing the experiment. 5. The record of observations along with the detailed experimental Algorithm of the experiment in the immediate last session should be submitted and certified staff member in-charge 6. Not more than 3 students in a group are permitted to perform the experiment on the set. 7. The group-wise division made in the beginning should be adhered to and no mix up of students among different groups will be permitted. 8. The components required pertaining to the experiment should be collected from stores in-charge after duly filling in the requisition form. 9. When the experiment is completed, should disconnect the setup made by them, and should return all the components / instruments taken for the purpose. 10. Any damage of the equipment or burn –out components will be viewed seriously either by putting penalty or by dismissing the total group of students from the lab for the semester / year. 11. Students should be present in the labs for total scheduled duration. 12. Students are required to prepare thoroughly to perform the experiment before coming to laboratory. 13. Algorithm sheets/data sheets provided to student’s groups should be maintained neatly and to be returned after the experiment. 7|Page JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD II Year B.Tech. CSE - I Sem L T/P/D 0 -/3/- C 2 DATA STRUCTURES LAB Objectives: To write and execute programs in C to solve problems using data structures such as arrays, linked lists, stacks, queues, trees, graphs, hash tables and search trees. To write and execute write programs in C to implement various sorting and searching methods. Recommended Systems/Software Requirements: Intel based desktop PC with minimum of 166 MHZ or faster processor with at least 64 MB RAM and 100MB free disk space. C compiler. Week1: Write a C program that uses functions to perform the following: a) Create a singly linked list of integers. b) Delete a given integer from the above linked list. c) Display the contents of the above list after deletion. Week2: Write a C program that uses functions to perform the following: a) Create a doubly linked list of integers. b) Delete a given integer from the above doubly linked list. c) Display the contents of the above list after deletion. Week3: Write a C program that uses stack operations to convert a given infix expression into its postfix Equivalent, Implement the stack using an array. Week 4: Write C programs to implement a double ended queue ADT using i)array and ii)doubly linked list respectively. Week 5: Write a C program that uses functions to perform the following: a) Create a binary search tree of characters. b) Traverse the above Binary search tree recursively in Postorder. Week 6: Write a C program that uses functions to perform the following: a) Create a binary search tree of integers. b) Traverse the above Binary search tree non recursively in inorder. Week 7: Write C programs for implementing the following sorting methods to arrange a list of integers in Ascending order : a) Insertion sort b) Merge sort Week 8: Write C programs for implementing the following sorting methods to arrange a list of integers in ascending order: a) Quick sort b) Selection sort 8|Page Week 9: i) Write a C program to perform the following operation: a)Insertion into a B-tree. ii) Write a C program for implementing Heap sort algorithm for sorting a given list of integers in ascending order. Week 10: Write a C program to implement all the functions of a dictionary (ADT) using hashing. Week 11: Write a C program for implementing Knuth-Morris- Pratt pattern matching algorithm. Week 12: Write C programs for implementing the following graph traversal algorithms: a)Depth first traversal b)Breadth first traversal TEXT BOOKS: 1. C and Data Structures, Third Edition, P.Padmanabham, BS Publications. 2. C and Data Structures, Prof. P.S.Deshpande and Prof. O.G. Kakde, Dreamtech Press. 3. Data structures using C, A.K.Sharma, 2nd edition, Pearson. 4. Data Structures using C, R.Thareja, Oxford University Press. 5. C and Data Structures, N.B.Venkateswarlu and E.V.Prasad,S.Chand. 6. C Programming and Data Structures, P.Radha Krishna, Hi-Tech Publishers. 9|Page INDEX S.No 1 2 3 4 5 6 7 8 9 10 11 12 13 Experime Name of the Experiment nt No Objective , Outcomes, Hardware Software Requirements Write a C program that uses functions to perform the 1 following: a) Create a singly linked list of integers. b) Delete a given integer from the above linked list. c) Display the contents of the above list after deletion. Write a C program that uses functions to perform the 2 following: a) Create a doubly linked list of integers. b) Delete a given integer from the above doubly linked list. c) Display the contents of the above list after deletion Write a C program that uses stack operations to convert a 3 given infix expression into its postfix Equivalent, Implement the stack using an array. Write C programs to implement a double ended queue ADT 4 using i) Array ii) Doubly linked list respectively. Write a C program that uses functions to perform the 5 following: a) Create a binary search tree of characters. b) Traverse the above Binary search tree recursively in Postorder. Write a C program that uses functions to perform the 6 following: a) Create a binary search tree of integers. b) Traverse the above Binary search tree non recursively in Inorder. Write C programs for implementing the following sorting 7 methods to arrange a list of integers in Ascending order : a) Insertion sort b) Merge sort Write C programs for implementing the following sorting 8 methods to arrange a list of integers in ascending order: a) Quick sort b) Selection sort i) Write a C program to perform the following operation: 9 a)Insertion into a B-tree. ii) Write a C program for implementing Heap sort algorithm for sorting a given list of integers in ascending order. Write a C program to implement all the functions of a 10 dictionary (ADT) using hashing. Write a C program for implementing Knuth-Morris- Pratt 11 pattern matching algorithm. Write C programs for implementing the following graph 12 traversal algorithms: a)Depth first traversal b)Breadth first traversal 10 | P a g e Page No Objective To write and execute programs in C to solve problems using data structures such as arrays, linked lists, stacks, queues, trees, graphs, hash tables and search trees. To write and execute write programs in C to implement various sorting and searching methods. Outcome Ability to identify the appropriate data structure for given problem. Graduate able to design and analyze the time and space complexity of algorithm or program. Ability to effectively use compilers includes library functions, debuggers and trouble shooting. Hardware and Software required 1. 2. 3. 4. 5. 11 | P a g e Intel based desktop PC Minimum of 166 MHZ or faster processor At least 64 MB RAM At least 100 MB free disk space C++ compiler and STL Recommended Experiment No: 1 Experiment as given in the JNTUH curriculum. Week1: Write a C program that uses functions to perform the following: a) Create a singly linked list of integers. b) Delete a given integer from the above linked list. c) Display the contents of the above list after deletion AIM: To Implement singly linked list of integers in data structure using functions. THEORY: Singly Linked List Generally a Linked List means "Singly Linked List". It is a chain of records known as Nodes. Each node has at least two members, one of which points to the next Node in the list and the other holds the data. Figure 1: Singly Linked List Basically Single Linked Lists are uni-directional as they can only point to the next Node in the list but not to the previous. We use below structure for a Node in our example. struct Node { int Data; struct Node *Next; }; Variable Data holds the data in the Node (It can be a pointer variable pointing to the dynamically allocated memory) while Next holds the address to the next Node in the list. Figure 2: Node in a Singly Linked List Head is a pointer variable of type struct Node which acts as the Head to the list. Initially we set 'Head' as NULL which means list is empty. Basic Operations on a Singly Linked List Traversing a List Inserting a Node in the List Deleting a Node from the List . 12 | P a g e Inserting a Node in Singly Linked List 1.Inserting a Node at the beginning of the List Inserting a Node at the End of the List Inserting a Node at position 'p' in the List Deletion of a Node from a Singly Linked List Deleting First Node in Singly Linked List Deleting Last Node in the Singly Linked List Deleting Node from position 'p' in the List ALGORITHM: Initialize the first and last nodes with NULL values struct node *first=NULL,*last=NULL,*next,*prev,*cur; 1. Algorithm creating a new node: Step 1: if the list is empty then first==NULL Step 2: Create a new node cur=(struct node*) malloc (sizeof (struct node)); Step 3: Read the content of node Step 4: Assign new node link to NULL cur->link=NULL Step 5: Assign new node to first & last node first=cur last=cur Step 6: If the list is not empty call insert function insert () Step 7 : Stop 2. Algorithm for Inserting a new node: Step 1 : Initialize count c to 1 Step 2 : Create inserting node cur=(struct node*)malloc(sizeof (struct node)); Step 3: Read the content of node Step 4: Read the position of insertion Step 5: Inserting in first position Check if the pos=1 and first!=NULL cur->link=first; first=cur; Step 6: Inserting in a given position next=first; repeat the steps a to c until c < pos a. prev=next; b. next=prev->link; c. c++; 13 | P a g e cur->link=prev->link; prev->link=cur; Step 7 : Stop 3. Algorithm for Deleting a node: Step 1 : Initialize count c to 1 Step 2 : Read the position for deletion Step 3 : Check if first=NULL print list is empty Step 4 : If the list contains single element Check if pos=1 and first->link=NULL print deleted element is first->data Step 5 : Assign first to NULL first=NULL; Step 6 : If the list contains more than one element and to delete first element if pos=1 and first->link!=NULL cur=first; first=first->link; cur->link=NULL; print deleted element is cur->data free(cur) Step 7 : If the list contains more than one element and to delete an element at given position next=first; repeat the steps a to c until c < pos a. cur=next; b. next=next->link; c. c++; cur->link=next->link; next->link=NULL; print deleted element is next->data free(next); Step 8 : Stop 4. Algorithm for Displaying a node: Step1 : Check if first node is NULL print list is empty Step2: If first node is not NULL then cur=first; repeat the steps a to b until cur!=NULL a . print cur->data b . cur=cur->link; Step3 : Stop 14 | P a g e INPUT & OUTPUT Insert() : 1 2 3 4 5 Display () : 1->2->3->4->5 Delete() : 3 Display() : 1->2->4->5 PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS FOR WEEK1 P1: Write a C program that uses functions to perform the following: a) Create a singly linked list of even integers between 1 to 100. b) Delete the nodes which are divisible by 5 from the above linked list. c) Display the first five contents of the above list after deletion. Test Case 1: 12 10 15 Result : 12->10 Number not in range or not even Test Case 2: 10 12 24 34 40 44 66 68 Result: display: 10 12 24 34 40 44 66 68 delete: 10 40 display:12 24 34 44 66 68 P2: Write a C program that uses functions to perform the following: a) Create a singly linked list of odd integers between 1 to 50. b) Delete the nodes which are multiples of 3 from the above linked list. c) Display the last five contents of the above list after deletion. Test Case 1: 9 11 51 Result : 9->11 Number not in range or not odd Test Case 2: 9-->11-->13-->17-->19-->23-->25--> Result: display: 9-->11-->13-->17-->19-->23-->25--> deleted: 9 display: 25-> 23-> 19-> 17->13 P3: Write a C program that uses functions to perform the following: a) Create a singly linked list of prime integers between 10 to 100. b) Delete the nodes which are less than 30 from the above linked list. c) Display the nodes from the given position(read position). Test Case 1: 11->37>41>43>7 Result: 11->37>41>43> Number not in range or not prime 15 | P a g e Test Case 2: 11->37>41>43 Delete: 37>41>43 Display: position 2 41->43 P4: Write a C program that uses functions to perform the following: a) Create a singly linked list of integers divisible by 5 between 100 to 1000. b) Delete the nodes which are greater than 500 from the above linked list. c) Display the nodes from the beginning of list to the given position(read position). Test Case 1: 600-->700-->500-->400-->300-->24 Result: 600-->700-->500-->400-->300 the number is not divisible by 5 or not in range Test Case 2: 600-->700-->500-->400-->300 Display :position 3 600-->700-->500 Deleted:600 700 P5: Write a C program that uses functions to perform the following: a) Create a singly linked list of integers which are multiple of 3 between 1 to 100. b) Delete the nodes which are multiple of 5 from the above linked list. c) Display the nodes between 50 to 100 from the above given list. Test Case 1: 9-->3-->6-->75-->15-->123 Result: the number is not multiple of 3 or not in range Test Case 2: 9-->3-->6-->75-->15-->123 Display:75-> Delete: 75 15 9-->3-->6 P6: Write a C program that uses functions to perform the following: a) Create a singly linked list of integers less than 100. b) Delete the nodes which are greater than 50 from the above linked list. c) Display the nodes between 75 to 100 from the above given list. Test Case 1: 45-->23-->56-->89-->99--> Result: 45-->23-->56-->89-->99--> 123 the number is not less than 100 Test Case 2: 45-->23-->56-->89-->99--> Display: 89-->99--> Delete: 56 89 99 45-->23 P7: Write a C program that uses functions to perform the following: a) Create a singly linked list of integers greater than 100. b) Delete the nodes which are less than 500 from the above linked list. c) Display the above given list in ascending order. Test Case 1: 600-->200-->300-->500-->600-->900-->90 Result: the number is not greater than 100 Test Case 2: 600-->200-->300-->500-->600-->900 16 | P a g e Display: data in ascending order is 200 300 500 600 600 900 Delete: 200 300 600-->500-->600-->900 P8: Write a C program that uses functions to perform the following: a) Create a singly linked list of integers between 100 to 1000. b) Delete the nodes which are multiple of 10 from the above linked list. c) Display the nodes which are multiples of 3 from the above given list. Test Case 1: 345-->315-->310-->200-->100-->1000-->1001 Result: the number is not in range 100-1000 Test Case 2: 345-->315-->310-->200-->100-->1000 Display: 345-->315 Delete: 310 200 100 1000 345-->315 P9: Write a C program that uses functions to perform the following: a) Create a singly linked list of perfect integers between 1 to 1000. b) Delete a node from a given position(read position)from the above linked list. c) Display the above given list in reverse order. Test Case 1: 6-->28-->496-->455 Result: the number is not in range 1-1000 or not perfect number Test Case 2: 6-->28-->496 Display: 496 28 6 Delete: position 1 28->6 P10: Write a C program that uses functions to perform the following: a) Create a singly linked list of squares of integers between 1 to 50. b) Delete the previous node from a given position(read position)from the above linked list. c) Display the above given list in descending order. Test Case 1: 16-->9-->4-->254964 Result: The number not in range 1-50 Test Case 2: 16-->9-->4-->254964 Display: 49 25 16 9 4 Delete: pos 1 Previous element not exist Pos=2 942549 17 | P a g e APPLICATIONS: Various applications of Singly Linked List are: A singly-linked list is ideally suited to stacks (last in, first out). The list maintains a link with the head node in the list and each node points to the next node in the list (the last node points to NULL). When a new node is added it is added to the head of the list. Since enqueue and dequeue both occur at the head of the list, processing is always in constant time [O(1)]. VIVA VOICE QUESTIONS 1. The linked list structure is defined as a. struct node b. node { { int item; int item; struct node *next; struct node *next; }; }; c. struct node { int item; struct node *next; }; d. node { int item; node next; }; 2.Single linked list uses _ _ _ _ _ _ no. of pointers a. Zero b. one c. Two d. Three 3. No.of pointers to be manipulated in a linked list to insert an item in the middle _ _ _ __ _ _ _ a. Two b. Three c. One d. Zero 4. Linked lists are not suitable for data structures of which one of the following problem? a. insertion sort b. Binary search c. radix sort d. polynomial manipulation problem 5. The linked list field(s) are a. data b. pointer c. pointer to next node 18 | P a g e d. data and pointer to next node Experiment No: 2 Experiment as given in the JNTUH curriculum. Week2: Write a C program that uses functions to perform the following: a) Create a doubly linked list of integers. b) Delete a given integer from the above doubly linked list. c) Display the contents of the above list after deletion. AIM: To implement doubled linked list of integers in data structure using functions. THEORY: 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. Following is representation of a DLL node in C language. /* Node of a doubly linked list */ struct node { int data; struct node *next; // Pointer to next node in DLL struct node *prev; // Pointer to previous node in DLL }; Following are advantages/disadvantages of doubly linked list over singly linked list. Advantages over singly linked list 1) A DLL can be traversed in both forward and backward direction. 2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given. In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer. Disadvantages over singly linked list 1) Every node of DLL Require extra space for an previous pointer. It is possible to implement DLL with single pointer though. 2) All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers. For example in following functions for insertions at different positions, we need 1 or 2 extra steps to set previous pointer. 19 | P a g e Insertion A node can be added in four ways 1) At the front of the DLL 2) After a given node. 3) At the end of the DLL 4) Before a given node. 1.Insertion at the front of list 2.Insertion in the middle of the list 20 | P a g e 3.Insertion at the end of the list Deletion Write a function to delete a given node in a doubly linked list. (a) Original Doubly Linked List (a) After deletion of head node (a) After deletion of middle node (a) After deletion of last node 21 | P a g e ALGORITHM : Initialize the first and last nodes with NULL values struct node *first=NULL,*last=NULL,*next,*prev,*cur; 1. Algorithm creating a new node: Step 1: if the list is empty then first==NULL Step 2: Create a new node cur=(struct node*) malloc (sizeof (struct node)); Step 3: Read the content of node Step 4: Assign new node left and right links to NULL cur->left=NULL; cur->right=NULL; Step 5: Assign new node to first & last node first=cur last=cur Step 6: If the list is not empty call insert function insert () Step 7 : Stop 2. Algorithm for Inserting a new node: Step 1 : Initialize count c to 1 Step 2 : Create inserting node cur=(struct node*)malloc(sizeof (struct node)); Step 3: Read the content of node Step 4: Read the position of insertion Step 5: Inserting in first position Check if the pos=1 and first!=NULL cur->right=first; cur->left=NULL; first=cur; Step 6: Inserting in a given position next=first; repeat the steps a to c until c < pos a. prev=next; b. next=prev->right; c. c++; prev->right=cur; cur->right=next; Step 7 : Stop 22 | P a g e 3. Algorithm for Deleting a node: Step 1 : Initialize count c to 1 Step 2 : Read the position for deletion Step 3 : Check if first=NULL print list is empty Step 4 : If the list contains single element Check if pos=1 and first->right=NULL print deleted element is first->data Step 5 : Assign first to NULL first=NULL; Step 6 : If the list contains more than one element and to delete first element if pos=1 and first->right!=NULL cur=first; first=first->right; cur->right=NULL; print deleted element is cur->data free(cur); Step 7 : If the list contains more than one element and to delete an element at given position next=first; repeat the steps a to c until c < pos a. cur=next; b.next=next->right; c. c++; cur->right=next->right; next->right=NULL; next->left=NULL; print deleted element is next->data free(next); Step 8 : Stop 4. Algorithm for Displaying a node: Step1 : Check if first node is NULL print list is empty Step2: If first node is not NULL then cur=first; repeat the steps a to b until cur!=NULL a . print cur->data b . cur=cur->right; Step3 : Stop 23 | P a g e INPUT & OUTPUT: Insert() : 10 20 30 40 50 Display () : 10->20->30->40->50 Delete() : 40 Display() : 10->20->30->50 PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS FOR WEEK-2 P1: Write a C program that uses functions to perform the following: a) Create a doubly linked list of even integers between 1 to 100. b) Delete the nodes which are divisible by 5 from the above linked list. c) Display the first five contents of the above list after deletion. Test Case 1: 12 10 15 Result : 12->10 Number not in range or not even Test Case 2: 10 12 24 34 40 44 66 68 Result: display: 10 12 24 34 40 44 66 68 delete: 10 40 display:12 24 34 44 66 68 P2: Write a C program that uses functions to perform the following: a) Create a doubly linked list of odd integers between 1 to 50. b) Delete the nodes which are multiples of 3 from the above linked list. c) Display the last five contents of the above list after deletion. Test Case 1: 9 11 51 Result : 9->11 Number not in range or not odd Test Case 2: 9-->11-->13-->17-->19-->23-->25--> Result: display: 9-->11-->13-->17-->19-->23-->25--> deleted: 9 display: 25-> 23-> 19-> 17->13 P3: Write a C program that uses functions to perform the following: a) Create a doubly linked list of prime integers between 10 to 100. b) Delete the nodes which are less than 30 from the above linked list. c) Display the nodes from the given position(read position). Test Case 1: 11->37>41>43>7 Result: 11->37>41>43> Number not in range or not prime Test Case 2: 11->37>41>43 24 | P a g e Delete: 37>41>43 Display: position 2 41->43 P4: Write a C program that uses functions to perform the following: a) Create a doubly linked list of integers divisible by 5 between 100 to 1000. b) Delete the nodes which are greater than 500 from the above linked list. c) Display the nodes from the beginning of list to the given position(read position). Test Case 1: 600-->700-->500-->400-->300-->24 Result: 600-->700-->500-->400-->300 the number is not divisible by 5 or not in range Test Case 2: 600-->700-->500-->400-->300 Display :position 3 600-->700-->500 Deleted:600 700 P5: Write a C program that uses functions to perform the following: a) Create a doubly linked list of integers which are multiple of 3 between 1 to 100. b) Delete the nodes which are multiple of 5 from the above linked list. c) Display the nodes between 50 to 100 from the above given list. Test Case 1: 9-->3-->6-->75-->15-->123 Result: the number is not multiple of 3 or not in range Test Case 2: 9-->3-->6-->75-->15-->123 Display:75-> Delete: 75 15 9-->3-->6 P6: Write a C program that uses functions to perform the following: a) Create a doubly linked list of integers less than 100. b) Delete the nodes which are greater than 50 from the above linked list. c) Display the nodes between 75 to 100 from the above given list. Test Case 1: 45-->23-->56-->89-->99--> Result: 45-->23-->56-->89-->99--> 123 the number is not less than 100 Test Case 2: 45-->23-->56-->89-->99--> Display: 89-->99--> Delete: 56 89 99 45-->23 P7: Write a C program that uses functions to perform the following: a) Create a doubly linked list of integers greater than 100. b) Delete the nodes which are less than 500 from the above linked list. c) Display the above given list in ascending order. Test Case 1: 600-->200-->300-->500-->600-->900-->90 Result: the number is not greater than 100 Test Case 2: 600-->200-->300-->500-->600-->900 Display: data in ascending order is 200 300 500 600 600 900 25 | P a g e Delete: 200 300 600-->500-->600-->900 P8: Write a C program that uses functions to perform the following: a) Create a doubly linked list of integers between 100 to 1000. b) Delete the nodes which are multiple of 10 from the above linked list. c) Display the nodes which are multiples of 3 from the above given list. Test Case 1: 345-->315-->310-->200-->100-->1000-->1001 Result: the number is not in range 100-1000 Test Case 2: 345-->315-->310-->200-->100-->1000 Display: 345-->315 Delete: 310 200 100 1000 345-->315 P9: Write a C program that uses functions to perform the following: a) Create a doubly linked list of perfect integers between 1 to 1000. b) Delete a node from a given position(read position)from the above linked list. c) Display the above given list in reverse order. Test Case 1: 6-->28-->496-->455 Result: the number is not in range 1-1000 or not perfect number Test Case 2: 6-->28-->496 Display: 496 28 6 Delete: position 1 28->6 P10: Write a C program that uses functions to perform the following: a) Create a doubly linked list of squares of integers between 1 to 50. b) Delete the previous node from a given position(read position)from the above linked list. c) Display the above given list in descending order. Test Case 1: 16-->9-->4-->254964 Result: The number not in range 1-50 Test Case 2: 16-->9-->4-->254964 Display: 49 25 16 9 4 Delete: pos 1 Previous element not exist Pos=2 942549 26 | P a g e APPLICATIONS: 1.Applications that have an MRU list (a linked list of file names) 2. The cache in your browser that allows you to hit the BACK button (a linked list of URLs) 3. Undo functionality in Photoshop or Word (a linked list of state) 4. A stack, hash table, and binary tree can be implemented using a doubly linked list. 5. A great way to represent a deck of cards in a game. VIVA VOICE QUESTIONS 1.In a linked list with n nodes, the time taken to insert an element after an element pointed by some pointer is (A) 0 (1) (B) 0 (log n) (C) 0 (n) (D) 0 (n 1og n) Ans:A 2.A linear collection of data elements where the linear node is given by means of pointer is called (A) linked list (B) node list (C) primitive list (D) None of these Ans:A 3. Representation of data structure in memory is known as: (A) recursive (B) abstract data type (C) storage structure (D) file structure Ans:B 27 | P a g e Experiment No: 3 Experiment as given in the JNTUH curriculum. Week3: Write a C program that uses stack operations to convert a given infix expression into its postfix Equivalent, Implement the stack using an array. AIM: To implement stack using array to convert a given infix expression into its postfix Equivalent in data structure using functions. THEORY: Infix To Postfix Conversion Algorithm for converting Infix to Postfix: If the input is an operand, then place it into the output buffer. If the input is an operator, push it into the stack. If the operator in stack has equal or higher precedence than input operator, then pop the operator present in stack and add it to output buffer. If the input is an open brace, push it into the stack If the input is a close brace, pop elements in stack one by one until we encounter close brace. Discard braces while writing to output buffer. Infix Expression: (a - b) / (c + d) Input: ( Input is open brace. So, place it into the stack. | | | ( | ------ Output: Input: a Place the operand in output buffer. | | | ( | ------ Output: a Input: Place the operator into the stack. | | | - | | ( | ------ Output: a Input: b Place the operand in output buffer | | | - | 28 | P a g e | ( | ------- Output: ab Input: ) Pop '-' and '(' | | | | | | ------- Output: ab- Input: / Push the operator into the stack. | | | | | / | ------- Output: ab- Input: ( Push open brace into the stack. | | | ( | | / | ------- Output: ab- Input: c Place the operand in output buffer. | | | ( | | / | ------- Output: ab-c Input: + Place the operator into the stack. | + | | ( | | / | ------- Output: ab-c Input: d Place the operand in output buffer. | + | | ( | | / | ------- 29 | P a g e Output: ab-cd Input: ) Pop all elements until we get open brace. | | | | | / | ------ Output: ab-cd+ No more input to parse. So, pop all other elements from the stack | | | | | | ------ Output: ab-cd+/ ALGORITHM: 1. Algorithm for postfix conversion: void postfix(char inf[15]) Step 1: repeat the steps a to c until inf[i]!='\0' a.check if infix expression is opening brace push it onto stack check if(inf[i]='(') push(inf[i]) b. check if infix expression is alphabetic character add it to postfix expression if(isalpha(inf[i])) pf[j++]=inf[i] c. check if infix expression is closing brace if(inf[i]==')') repeat 1 to 2 until stack[top]!=opening brace 1.pf[j++]=pop() 2.x=pop() Otherwise repeat 1 to 2 until prec(stack[top])>=prec(inf[i]) 1.pf[j++]=pop() 2.push(inf[i]) Step 2:repeat the steps a to b until top!=-1 a.pf[j++]=pop() b.pf[j]='\0' step 3: stop 2. Algorithm for push operation on stack: Step 1:increment top by 1 Step 2: stack[++top]=y Step 3:stop 30 | P a g e 3. Algorithm for pop operation on stack: Step 1:return stack[top] Step 2: decrement top by 1 Step 3:stop 4. Algorithm for finding precedence of operators : Step 1:check if (c=='*' || c=='/' || c=='%') return(5) step 2:check if (c=='+' || c=='-') return(3) Step 3:if(c=='(') return(1) step 4:stop 5. Algorithm for main function : Step 1:start Step 2: read infix expression Step 3:call postfix(infix) Step 4:print postfix expression result Step 5:stop INPUT: a+b*c OUTPUT: abc*+ PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS FOR WEEK-3 Problem definitions different from JNTU topics for week3 P1: Write a C program that uses stack operations to convert a given infix expression having + and - operators into its postfix Equivalent, Implement the stack using an array. (ex: a+b-c) Test case 1: (a+b-c) Result: ab+cTest case 2: a+(b-c) Result: abc-+ P2: Write a C program that uses stack operations to convert a given infix expression having + and * operators into its postfix Equivalent, Implement the stack using an array. (ex: a+b*c) Test case 1: (a+b*c) Result: abc*+ Test case 2: (a+b)*c 31 | P a g e Result: ab+c* P3: Write a C program that uses stack operations to convert a given infix expression having *,% and / operators into its postfix Equivalent, Implement the stack using an array. (ex: a*b%d/c) Test case 1: (a*b%d/c) Result: ab*d%c/ Test case 2: (a*(b%d)/c) Result: abd%*c/ P4: Write a C program that uses stack operations to convert a given infix expression having / and % operators into its postfix Equivalent, Implement the stack using an array. (ex: a/b%c) Test case 1: (a/b%c) Result: ab/c% Test case 2: a/(b%c) Result: abc%/ P5: Write a C program that uses stack operations to convert a given infix expression having && and + operators into its postfix Equivalent, Implement the stack using an array. (ex: a+b && c) Test case 1: (a+b & c) Result: abc&+ Test case 2: (a+b)&c Result: ab+c& P6: Write a C program that uses stack operations to convert a given infix expression having ^ and * operators into its postfix Equivalent, Implement the stack using an array. (ex: a^b*c ) Test case 1: (a^b * c) Result: abc*^ Test case 2: (a^b)*c Result: ab^c* P7: Write a C program that uses stack operations to convert a given infix expression having < and + operators into its postfix Equivalent, Implement the stack using an array. (ex: a < b + c) Test case 1: (a<b+ c) Result: abc+< 32 | P a g e Test case 2: (a<b)+c Result: ab<c+ P8: Write a C program that uses stack operations to convert a given infix expression having ! and + operators into its postfix Equivalent, Implement the stack using an array. (ex: !a + b + c) Test case 1: (!a+b+ c) Result: a!b+c+ Test case 2: !a+(b+c) Result: a!bc++ P9: Write a C program that uses stack operations to convert a given infix expression having << and * operators into its postfix Equivalent, Implement the stack using an array. (ex: a & b * c) Test case 1: (a&b* c) Result: ab&c* Test case 2: a&(b*c) Result: abc*& P10: Write a C program that uses stack operations to convert a given infix expression having == and / operators into its postfix Equivalent, Implement the stack using an array. (ex: a = b / c) Test case 1: (a=b/ c) Result: abc/= Test case 2: (a=b)/c) Result: ab=c/ APPLICATIONS: 1. infix notation is easy to read for humans, whereas pre-/postfix notation is easier to parse for a machine. The big advantage in pre-/postfix notation is that there never arise any questions like operator precedence. 2. Postfix notation, also known as RPN, is very easy to process left-to-right. An operand is pushed onto a stack; an operator pops its operand(s) from the stack and pushes the result. Little or no parsing is necessary. It's used by Forth and by some calculators (HP calculators are noted for using RPN). 33 | P a g e VIVA VOICE QUESTIONS: 1. The equivalent of (a+b↑c↑d)*(e+f/d) in the post fix notation is a. ab+c↑d↑e &fd/ b. abcd↑+↑efd/+* c. abcdefd/+*↑↑+ d. abcd↑↑+efd/+* 2. The infix form of the postfix expression ABC-/D*E+ is a. A/B-C*D+E b. A-B/C*D+E c. (A-B)/C*D+E d. A/(B-C)*D+E 3. The postfix expression for the infix expression A/B*C+D*E is a. AB/C*DE*+ b. ABC/*DE+* c. ABCD/*E+* d. ABC*/D*E+ 4. The prefix expression for the infix expressionA/B*C+D*E is a. AB/C*DE*+ b. +*/ABC*DE c. +*AB/C*DE 5. Suffix expression is a. Infix b. postfix c. prefix d. post & prefix c. prefix d. post & prefix 6. polish expression is a. infix 34 | P a g e b. postfix d. /+ABCDE Experiment No: 4 Experiment as given in the JNTUH curriculum. Week 4 : Write C programs to implement a double ended queue ADT of integers using i) Array and ii) Doubly linked list respectively. AIM: To implement a double ended queue ADT of integers using array and doubly linked list in data structure using functions. THEORY: A double-ended queue is an abstract data type similar to an simple queue, it allows you to insert and delete from both sides means items can be added or deleted from the front or rear end. Dequeue is also called as double ended queue and it allows user to perform insertion and deletion from the front and rear position. And it can be easily implemented using doubly linked list. On creating dequeue, we need to add two special nodes at the ends of the doubly linked list(head and tail). And these two nodes needs to be linked between each other(initially). head tail ---------------------------------------------------------| | | ---|---------->| | | | | NULL | 0 | | | | 0 | NULL | | | | |<----------|--| | | ---------------------------------------------------------So, the header node goes before the first node of the queue and the trailer node goes after the last node in the queue. To do insertion at the front position, place the new node next to the head. And to do insertion at the rear position, place the new node before the tail. Similarly, dequeue operation can also be performed at the front and rear positions. Operations 1. Enter New Element From Right 2. Enter New Element From Left 3. Remove Element From Right 4. Remove Element From Left 35 | P a g e 4a) AIM: To implement a double ended queue ADT of integers using array ALGORITHM: 1. Algorithm for insertion at rear end: Step 1: check if(front==-1) front++ rear++ read the element to insert a[rear] count++; Step 2: check if(rear>=SIZE-1) print Insertion is not possible, overflow!!! return Step 3: Increment the rear by 1 read the element to insert a[rear] count++; Step 4:stop 2. Algorithm for insertion at front end: Step 1: check if(front==-1) front++ rear++ read the element to insert a[rear] count++ Step 2: check if(rear>=SIZE-1) print Insertion is not possible, overflow!!! return Step 3: Repeat the steps a to d until i>0 a. b. c. d. a[i]=a[i-1] read the element to insert a[i] count++ rear++; Step 4:stop 3. Algorithm for deletion at rear end: Step 1: check if (front==-1) Print Deletion is not possible: Dequeue is empty return Step 2: otherwise Print The deleted element is a[rear] Check if(front==rear) front=rear=-1 otherwise rear=rear-1 36 | P a g e Step 3:stop 4. Algorithm for deletion at front end: 1: check if (front==-1) Print Deletion is not possible: Dequeue is empty return Step 2: otherwise Print The deleted element is a[front] Check if(front==rear) front=rear=-1 otherwise front=front-1 Step 3:stop 5.Algorithm for displaying the dequeue: Step 1: check if (front==-1) Print Dequeue is empty return Step 2: otherwise Repeat step a until i<=rear (i=front) Print a[i] Step 3:stop INPUT & OUTPUT: enqueue: front 400 300 200 150 Enqueue rear: 200 500 600 700 Display: 400 300 200 150 200 Dequeue rear:700 600 500 200 Dequeue front:200 500 600 700 37 | P a g e 500 600 700 4b) AIM: To implement a double ended queue ADT of integers using Doubly linked list. ALGORITHM: ALGORITHM: Initialize the front and rear nodes with NULL values struct node *front=NULL,*rear=NULL,*cur,*temp 1. Algorithm for insertion at rear end: Step 1: Create a new node cur=(struct node*) malloc (sizeof (struct node)) Step 2: Read the content of node (cur->data) Step 3: Assign new node right link to NULL cur->right=NULL Step 4: Assign new node to front & rear node Check if(rear==NULL&&front==NULL) rear=front=cur Step 5: otherwise rear->right=cur cur->left=rear rear=cur Step 6:stop 2. Algorithm for insertion at front end: Step 1: Create a new node cur=(struct node*) malloc (sizeof (struct node)) Step 2: Read the content of node (cur->data) Step 3: Assign new node right link to NULL cur->right=NULL Step 4: Assign new node to front & rear node Check if(rear==NULL&&front==NULL) rear=front=cur Step 5: otherwise cur->right=front front->left=cur front=cur Step 6:stop 3. Algorithm for deletion at front end: Step 1: check if(front==NULL) Print deQueue is empty Step 2: otherwise 38 | P a g e Check if(front==rear) Print Deleted data is front->data front=rear=NULL Step 3: otherwise temp=front; print Deleted data is temp->data front=front->right front->left=NULL free(temp) Step 4: stop 4. Algorithm for deletion at rear end: Step 1: check if(front==NULL) Print deQueue is empty Step 2: otherwise Check if(front==rear) Print Deleted data is rear->data front=rear=NULL Step 3: otherwise temp=rear; print Deleted data is temp->data if(front==rear) front=rear=NULL rear=rear->left rear->right=NULL free(temp) Step 4: stop 5.Algorithm for displaying the dequeue: Step 1: check if(front==NULL) Print deQueue is empty Step 2: otherwise temp=front; repeat steps a-b until temp!= NULL a. printf("<-%d ->",temp->data) b. temp=temp->right; Step 3: stop 39 | P a g e INPUT & OUTPUT : enqueue: front<-400 -><-300 -><-200 -><-150 -> Enqueue rear: <-200 -><-500 -><-600 -><-700-> Display: <-400 -><-300 -><-200 -><-150 -><-200 -><-500 -><-600 -><-700-> Dequeue rear:700 600 500 200 Dequeue front:200 500 600 700 Problem definitions different from JNTU topics for week4 P1: Write C programs to implement a double ended queue ADT of even integers between 1 to 100 using array. Test case 1: 12 10 8 2 4 6 7 Result: the number is not even or not in range 12 10 8 2 4 6 8 Result: Queue overflow Test case 2: 12 10 8 2 4 6 Result: Delete rear 6 4 2 Delete front 12 10 8 P2: Write C programs to implement a double ended queue ADT of odd integers between 10 to 100 using doubly linked list. Test case 1: 19 17 15 13 25 27 29 8 Result: the number is not even or not in range 19 17 15 13 25 27 29 Test case 2: 19 17 15 13 25 27 29 Result: Delete rear 29 27 25 13 15 17 19 Delete front dequeue empty P3: Write C programs to implement a double ended queue ADT of prime integers between 1 to 50 using array. Test case 1: 19 17 13 23 29 43 25 Result: the number is not prime or not in range 19 17 13 23 29 43 47 Result: Queue overflow Test case 2: 19 17 13 23 29 43 25 Result: Delete rear 25 43 29 Delete front 19 17 13 40 | P a g e P4: Write C programs to implement a double ended queue ADT of integers divisible by 5 between 100 to 1000 using doubly linked list Test case 1: <-400 -><-300 -><-200 -><-150 -><-200 -><-500 -><-600 -><-700 ->20 Result: the number is not divisible by 5 or not in range 100 to 1000 Test case 2: :<-400 -><-300 -><-200 -><-150 -><-200 -><-500 -><-600 -><-700 -> Result: Delete rear 700 600 500 200 150 Delete front 400 300 200 Queue underflow P5: Write C programs to implement a double ended queue ADT of integers which are multiples of 10 between 1 to 1000 using array. Test case 1: 600 500 400 100 200 300 1200 Result: the number is not multiple of 10 or not in range 600 500 400 100 200 300 700 Result: Queue overflow Test case 2: 600 500 400 100 200 300 Result: Delete rear 300 200 100 Delete front 600 500 400 P6: Write C programs to implement a double ended queue ADT of integers less than 100 using doubly linked list. Test case 1: <-36 -><--1 -><-42 -><-85 -><-100 -><-45 -><-56 ->101 Result: the number is greater than100 <-36 -><--1 -><-42 -><-85 -><-100 -><-45 -><-56 -> Test case 2: <-36 -><--1 -><-42 -><-85 -><-100 -><-45 -><-56 -> Result: Delete front 36 -1 42 85 100 45 56 Delete rear dequeue underflow P7: Write C programs to implement a double ended queue ADT of integers greater than 500 using array. Test case 1: 850 733 600 400 550 525 300 Result: the number is not less than 500 850 733 600 400 550 525 900 Result: Queue overflow Test case 2: 850 733 600 400 550 525 Result: Delete rear 525 550 400 600 733 850 Delete front dequeue empty P8: Write C programs to implement a double ended queue ADT of integers between 500 and 1000 using doubly linked list Test case 1: <-700 -><-600 -><-500 -><-800 -><-900 -><-1000 ->1100 41 | P a g e Result: the number is not between 500-1000 <-700 -><-600 -><-500 -><-800 -><-900 -><-1000 -> Test case 2: <-700 -><-600 -><-500 -><-800 -><-900 -><-1000 -> Result: Delete front 700 600 Delete rear 1000 900 800 500 dequeue empty P9: Write C programs to implement a double ended queue ADT of perfect integers between 1 and 1000 using array. Test case 1: 496 6 28 24 Result: the number is not perfect number or not in range 1-1000 496 6 28 Test case 2: 496 6 28 Result: Delete front 496 6 28 Delete rear queue empty P10: Write C programs to implement a double ended queue ADT of squares of integers between 1 and 50 using doubly linked list. Test case 1: <-9 -><-4 -><-1 -><-16 -><-25 -><-36 -><-49 ->64 Result: the number is not in range between 1-50 <-9 -><-4 -><-1 -><-16 -><-25 -><-36 -><-49 -> Test case 2:delete rear 49 36 25 16 1 Delete front 9 4 Dequeue empty APPLICATIONS: 1. When modeling any kind of real-world waiting line: entities (bits, people, cars, words, particles, whatever) arrive with a certain frequency to the end of the line and are serviced at a different frequency at the beginning of the line. 2. To implements task scheduling for several processors. 3. The steal-job scheduling algorithm is used by Intel's Threading Building Blocks (TBB) library for parallel programming uses deque. VIVA VOICE QUESTIONS: 1. To simulate people waiting in a line, which data structure would you use? a)Vector b)DeQueue c)Stack 2. To implement which data structure can be used? a)array b)SLL c)DLL d)Set e) List D)none 3.dequeue rear operation is similar to a)push() 42 | P a g e b)pop() c)enqueue () d)none Experiment No: 5 Experiment as given in the JNTUH curriculum. Week 5:Write a C program that uses functions to perform the following: a) Create a binary search tree of characters. b) Traverse the above Binary search tree recursively in Postorder. AIM: To implement a binary search tree of characters in data structure using functions. THEORY: A binary search tree is a tree where each node has a left and right child. Either child, or both children, may be missing. Figure 3-2 illustrates a binary search tree. Assuming k represents the value of a given node, then a binary search tree also has the following property: all children to the left of the node have values smaller than k, and all children to the right of the node have values larger than k. The top of a tree is known as the root, and the exposed nodes at the bottom are known as leaves. Insertion in Binary Search Tree: Check whether root node is present or not(tree available or not). If root is NULL, create root node. If the element to be inserted is less than the element present in the root node, traverse the left sub-tree recursively until we reach T->left/T->right is NULL and place the new node at T->left(key in new node < key in T)/T->right (key in new node > key in T). If the element to be inserted is greater than the element present in root node, traverse the right sub-tree recursively until we reach T->left/T->right is NULL and place the new node at T->left/T->right. Binary search tree deletion There are three different cases that needs to be considered for deleting a node from binary search tree. case 1: Node with no children (or) leaf node case 2: Node with one child case 3: Node with two children. Binary search tree searching Binary search tree is a tree in which the key value of left child is less than the parent node and the key value in right child is greater than the root. Check whether the root node is NULL or not. If it's NULL, there is no tree available. So, return NULL. If node T has the search element, then return that node. 43 | P a g e If the search element is less than the key in node T, recursively search the left sub tree of T. If the search element is greater than the key in node T, recursively search the right sub tree of T. THEORY: Tree Traversals Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. Following are the generally used ways for traversing trees. Tree Traversals: (a) Inorder (b) Preorder (c) Postorder Inorder Traversal: Algorithm Inorder(tree) 1. Traverse the left subtree, i.e., call Inorder(left-subtree) 2. Visit the root. 3. Traverse the right subtree, i.e., call Inorder(right-subtree) Uses of Inorder In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order. To get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder itraversal s reversed, can be used. Preorder Traversal: Algorithm Preorder(tree) 1. Visit the root. 2. Traverse the left subtree, i.e., call Preorder(left-subtree) 3. Traverse the right subtree, i.e., call Preorder(right-subtree) Uses of Preorder Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on of an expression tree. Postorder Traversal: Algorithm Postorder(tree) 1. Traverse the left subtree, i.e., call Postorder(left-subtree) 2. Traverse the right subtree, i.e., call Postorder(right-subtree) 3. Visit the root. 44 | P a g e Uses of Postorder Postorder traversal is used to delete the tree. Postorder traversal is also useful to get the postfix expression of an expression tree. ALGORITHM: Step 1.declare a structure struct BST { char data; struct BST *left,*right; }node; Step 2: initialize binay search tree root to NULL struct BST* root=NULL,*temp,*cur; 1. Algorithm for creating a binary search tree: Step 1: Start Step2 :Declare c[10] Step3: Initialize temp=root; Step4 : Read the character cur=(struct BST*)malloc(sizeof(struct BST)); Step5: initialize cur->data=c[0] cur->left=NULL cur->right=NULL Step6: Check the node is null assign root to curr if(temp==NULL) root=cur; Step7: repeat until temp!=NULL upto Step 16 Step8 : Check the current data less than temp data if((cur->data))<(temp->data)) Step9: check the temp->left equal to NULL if(temp->left==NULL) Step 10: Assign temp->left=cur; Step 11 : return Step12 : Assign temp=temp->left; Step13: otherwise check temp right equal to null 45 | P a g e temp->right==NULL Step14:Assign temp->right=cur; return; Step 15 : otherwise temp=temp->right; Step 16: stop Algortihm postorder(struct BST *temp) Step1: Start Step2: check the temp notequal to null temp!=NULL Step3: call to postorder with temp->left postorder(temp->left); Step4: call to postorder with temp->right postorder(temp->right); Step5: print temp->data Step6: stop INPUT & OUTPUT: ABC CBA Problem definitions different from JNTU topics for week5 P1: Write a C program that uses functions to perform the following: a) Create a binary search tree of capital alphabets. b) Traverse the above Binary search tree recursively in Preorder. Test case 1: A B C a Result: A B C lower case letters are not allowed Test case 2: D C E B F Result: D C E B F P2: Write a C program that uses functions to perform the following: a) Create a binary search tree of small case alphabets. b) Traverse the above Binary search tree recursively in inorder. Test case 1: a b c A Result: a b c upper case letters are not allowed 46 | P a g e Test case 2: d c e b f Result: b c d e f P3: Write a C program that uses functions to perform the following: a) Create a binary search tree of lower case vowels. b) Traverse the above Binary search tree recursively in Postorder. Test case 1: i o e u a x Result: i o e u a consonant letters are not allowed Test case 2: i o e u a Result: a e u o i P4: Write a C program that uses functions to perform the following: a) Create a binary search tree of consonants. b) Traverse the above Binary search tree recursively in converse Postorder. Test case 1: a Result: vowels are not allowed dcfbg Test case 2: d c f b g Result: g f b c d P5: Write a C program that uses functions to perform the following: a) Create a binary search tree of upper case vowels symbols. b) Traverse the above Binary search tree recursively in converse Preorder. Test case 1: a Result: small case vowels are not allowed IEOAU Test case 2: I E O A U Result: I O U E A P6: Write a C program that uses functions to perform the following: a) Create a binary search tree of capital alphabets and operators(+,*,/). b) Traverse the above Binary search tree recursively in converse Inorder. Test case 1: A + B - * a Result: small case letters are not allowed A+B-* Test case 2: A + B - * Result: B A + * P7: Write a C program that uses functions to perform the following: a) Create a binary search tree of small alphabets and operators(%,-,*). b) Traverse the above Binary search tree recursively in preorder. Test case 1: a % - b * A 47 | P a g e Result: Upper case letters are not allowed a % * b Test case 2: a % - b * Result: a % * b P8: Write a C program that uses functions to perform the following: a) Create a binary search tree for a given infix expression.(a+b*c) b) Traverse a specific node of the above Binary search tree recursively in Inorder. Test case 1: a + b * c A Result: Upper case letters are not allowed Test case 2: a + b * c Result: * + a b c P9: Write a C program that uses functions to perform the following: a) Create a binary search tree for a given prefix expression.(-a/bc) b) Traverse a specific node of the above Binary search tree recursively in preorder. Test case 1: - a / b c B Result: Upper case letters are not allowed Test case 2: - a Result: a / b c / b c P10: Write a C program that uses functions to perform the following: a) Create a binary search tree for a given postfix expression.(a*b% c) b) Traverse a specific node of the above Binary search tree recursively in postorder. Test case 1: a * b % c C Result: Upper case letters are not allowed Test case 2: a * b % c Result: % * c b a APPLICATIONS: 1. 2. 3. 4. 5. Used in many search applications where data is constantly entering/leaving, such as the map and set objects in many languages' libraries. Binary Space Partition - Used in almost every 3D video game to determine what objects need to be rendered. Binary Tries - Used in almost every high-bandwidth router for storing router-tables. Hash Trees - used in p2p programs and specialized image-signatures in which a hash needs to be verified, but the whole file is not available. Heaps - Used in implementing efficient priority-queues, which in turn are used for scheduling processes in many operating systems, Quality-of-Service in routers, and 48 | P a g e A* (path-finding algorithm used in AI applications, including robotics and video games). Also used in heap-sort. 6. Huffman Coding Tree - used in compression algorithms, such as those used by the .jpeg and .mp3 file-formats. . VIVA VOICE QUESTIONS: 1.Every binary tree with n elements have _ no of edges a)n b)n-1 c)n-2 d)n+1 2.Define inorder traversal a)left,root,right b)root,left,right c)left,right,root d)none c)left,right,root d)none c)right,left,root d)none 3. Define postorder traversal a)left,root,right b)root,left,right 4. Define converse postorder traversal a)left,root,right 49 | P a g e b)root,left,right Experiment No: 7 (A) AIM: Write C programs for implementing the following sorting methods to arrange a list of integers in Ascending order using Insertion sort THEORY: Example: The following table shows the steps for sorting the sequence {3, 7, 4, 9, 5, 2, 6, 1}. In each step, the key under consideration is underlined. The key that was moved (or left in place because it was biggest yet considered) in the previous step is shown in bold. 37495261 37495261 37495261 34795261 34795261 34579261 23457961 23456791 12345679 ALGORITHM 1. Algorithm: Insertion_Sort (int A[20],int n) Step 1: Start Step 2: Declare integer variables i, j, index Step 3: for i from 1 to i<n repeat steps a to d a) Initialize index=a[i] b) j=i; c) While ((j>0) && (a[j-1]>index)) { a[j]=a[j-1] j=j-1 } d) a[j]=index 50 | P a g e Step 4: print a[i] Step5: stop 2. Algorithm Main Function: main () Step 1: Declare an integer variables n, i, a [20] Step 2: Read size of array n Step 3: Read the elements of an array a[i] Step 4: call the sub function Insertion_Sort (a,n) INPUT: {3, 7, 4, 9, 5, 2, 6, 1}. OUTPUT: 1 2 3 4 5 6 7 9 PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS FOR WEEK7 P1: Write C program for implementing the insertion sort method to arrange a list of even integers between 1 to 100 integers in ascending order. Test Case 1: 56 90 20 10 15 Not Even 10 20 56 90 P2: Write C programs for implementing the insertion sort method methods to arrange a list of odd integers between 1 to 100 integers in ascending order. Test Case 1: 55 91 21 11 16 Not Odd 11 21 55 91 P3: Write C programs for implementing the insertion sort method to arrange a list of integer divisible by 3 & 5 between 10 to 100 integers in ascending order Test Case 1: 15 90 25 45 10 Not divisible 3 or 5 15 25 45 90 P4: Write C programs for implementing the insertion sort method to arrange a list of integer divisible by 5 between 100 to 1000 in ascending order. Test Case 1: 150 910 220 715 14 Not divisible by 5 150 220 715 910 P5: Write C programs for implementing the insertion sort method to arrange a list of integer multiple of 3 between 1 to 100 in Ascending order Test Case 1: 56 90 15 10 Not Multiple of 3 15 56 90 P6: Write C programs for implementing the insertion sort method to arrange a list of integer less than 100 & divisible by 3 & 6 in ascending order. Test Case 1: 56 90 20 10 150 51 | P a g e Greater than 100 10 20 56 90 P7: Write C programs for implementing the f insertion sort method to arrange a list of integer greater than 100 & multiply of 4 in ascending order Test Case 1: 156 190 120 100 90 Less than 100 100 120 156 190 P8: Write C programs for implementing the insertion sort method to arrange a list of integer between 100 to 1000 in ascending order. Test Case 1: 156 190 200 500 1500 Not in range 100 1000 156 190 200 500 P9: Write C programs for implementing the insertion sort method to arrange a list of cube integer between 1 t0 500 in ascending order. Test Case 1: 5 4 3 2 10 Not in range 1 - 500 8 27 64 125 P10: Write C programs for implementing the insertion sort method to arrange a list of squares of integers between 1 to 50 in ascending order. Test Case 1: 5 4 3 2 10 Not in range 1 - 50 4 9 16 25 APPLICATIONS: VIVA VOICE QUESTIONS: 1. Define insertion sort? 2. What is the average & worst Time complexity of an insertion sort? 3. Difference between bubble sort & insertion sort? 52 | P a g e EXPERIMENT NO: 7 (B) Write C programs for implementing the Merge sort methods to arrange a list of integers in ascending order AIM: To implements the Merge sort methods to arrange a list of integers in ascending order THEORY: Merge sort is based on Divide and conquer method. It takes the list to be sorted and divide it in half to create two unsorted lists. The two unsorted lists are then sorted and merged to get a sorted list. The two unsorted lists are sorted by continually calling the merge-sort algorithm; we eventually get a list of size 1 which is already sorted. The two lists of size 1 are then merged. Properties: Best case – When the array is already sorted O(nlogn). Worst case – When the array is sorted in reverse order O(nlogn). Average case – O(nlogn). Extra space is required, so space complexity is O(n) for arrays and O(logn) for linked lists. ALGORITHM: Globally declaring array int a[100] 1. Algorithm: Merge_Sort(int a[100],int low,int high) Step1: Start Step2: Declare an integer middle variable int mid Step3: Check the condition low less than high if(low<high) Step4: Initialize mid mid=(low+high)/2 Step5: call sub_function mergsort from low to middle Merge_Sort (a,low,mid) Step6: call sub_function mergsort from middle+1 to high Merge_Sort (a, mid+1,high) Step7: call sub_function Merg from low to high Merge (a,low,high,mid) Step8: Stop 2. Algorithm: Merge (int a[100],int low,int high,int mid) Step1: Start Step2: Declare integer variables and an array i, j, k, c[50] Step3: Initialize i to low i=low Step4: Initialize j to mid +1 53 | P a g e j=mid+1 Step5: Initialize k to low k=low Step6: Repeat Step(a) to Step(c) upto while((i<=mid)&&(j<=high)) a) Check (a) upto an array a[i] less than a[j] otherwise (b) if(a[i]<a[j]) c[k]=a[i] i++ k++ b) Otherwise c[k]=a[j] j++ k++ c) Repeat upto while (i<=mid) c[k]=a[i]; i++; k++; Step7: Repeat a to b until while(j<=high) a) c[k]=a[j]; b) j++; c) k++; Step8: Repeat until for(i=0;i<k;i++) a[i]=c[i] Step9: stop 3. Algorithm: main () Step1: Start Step2: Declare integer variables i, n Step3: Read the size / no. of elements Step4: Read the array elements a[i] Step5: call the sub_function Merge_Sort (a, 0, n-1) Step6: Print the sorted array: a[i] INPUT: OUTPUT: PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS P1: Write C program for implementing the merge sort method to arrange a list of even integers between 1 to 100 integers in Ascending order . Test Case 1: 56 90 20 10 15 Not Even 10 20 56 90 54 | P a g e P2: Write C program for implementing the merge sort method methods to arrange a list of odd integers between 1 to 100 integers in Ascending order . Test Case 1: 55 91 21 11 16 Not Odd 11 16 21 55 91 P3: Write C program for implementing the merge sort method to arrange a list of integer divisible by 3 & 5 between 10 to 100 in Ascending order Test Case 1: 15 90 25 45 10 Not divisible 3 or 5 15 25 45 90 P4: Write C program for implementing the merge sort method to arrange a list of integer divisible by 5 between 1 to 100 in Ascending order Test Case 1: 50 90 20 10 14 Not divisible by 5 10 14 20 50 90 P5: Write C program for implementing the merge sort method to arrange a list of integer multiple of 3 between 1 to 100 in Ascending order Test Case 1: 56 90 15 10 75 Not Multiple of 3 15 56 75 90 P6: Write C program for implementing the merge sort method to arrange a list of integer less than 100 in Ascending order . Test Case 1: 56 90 20 10 150 greater than 100 10 20 56 90 P7: Write C program for implementing the merge sort method to arrange a list of integer greater than 100 in Ascending order Test Case 1: 156 190 120 100 90 Less than 100 100 120 156 190 P8: Write C program for implementing the merge sort method to arrange a list of integer multiples of 7 between 100 to 1000 in Ascending order Test Case 1: 156 190 200 500 1500 Not in range 100 1000 156 190 200 500 P9: Write C program for implementing the merge sort method to arrange a list of cube integer between 1 t0 500 in Ascending order Test Case 1: 5 4 3 2 10 Not in range 1 - 500 8 27 64 125 P10: Write C program for implementing the merge sort method to arrange a list of squares of integers between 1 to 50 in Ascending order . Test Case 1: 5 4 3 2 Not in range 1 - 50 4 9 16 VIVA VOICE QUESTIONS: 55 | P a g e 10 25 1. Define Merge sort? 2. worst time complexity of merge sort? 3. Merge sort the following elements 50 30 10 60 20 56 | P a g e 5 WEEK-8 Write C programs for implementing the following sorting methods to arrange a list of integers in ascending order: (A) Quick sort (B) Selection sort (A) Quick sort • It was developed by C.A.R Hoare in 1962. • Like bubble sort, quick sort is an exchange sort, i.e., items swap positions till the entire array is sorted. • The quick sort is more efficient because it requires fewer exchanges to correctly position an element. Basic principle • Pick one element in the array and rearrange the remaining elements around it. The chosen element is called the pivot(usually first element of the list). • Once the pivot is chosen, all the elements lesser than the pivot are moved to the left of the pivot, and all elements equal to or greater than the pivot are moved to the right of the pivot. • The pivot acts as a partition dividing the original list into two sub lists, and after the partitioning, the pivot is in its correct position in the sorted list. • This procedure of choosing the pivot and partitioning the list is recursively applied to the sub lists till the subsequent sub lists consists of only one element. Complexity of quick sort: Best case: O(n log n) Average case:O(n log n) Worst case: O(n2) 57 | P a g e WEEK-8 (A) Write C programs for implementing the following sorting methods to arrange a list of integers in ascending order: (A): Quick Sort Algorithm : void qsort(int a[10],int first,int last) step 1 : start step 2 : declare integer variables i,j,t,pivot,n; step 3: check up to first less than last if(first<last) [step 3 to step 10] i=first; j=last; pivot=first; step 4: repeat upto first less than last from step 4 to step 10 while(i<j) step 5: repeat 5 to 7 while(a[i]<=a[pivot]&&i<last) i++; step 6 : while(a[j]>a[pivot]) j--; step 7: if(i<j) { t=a[i]; a[i]=a[j]; a[j]=t; } step 8: t=a[pivot]; a[pivot]=a[j]; a[j]=t; step 9: qsort(a,first,j-1); step 10: qsort(a,j+1,last); step 11: stop 1. Algorithm : Main function step 1: start step 2: declare i,n,a[10] step 3: Read the size of an array step 4: Read the elements of an step 5: call sub function qsort(a,0,n-1); step 6: print sorted elements of an array a[i] step 7: stop 58 | P a g e array int main() { int i=0,count=0,n,x,sum,rem,y; srand(time(NULL)); printf(“\n enter the n”); scanf(“%d”,&n); while(count<n) { x=rand()%900+100; y=x; sum=0 while(x!=0) { rem=x%10; sum=sum+rem*rem*rem; x=x/10; } if(y==sum) { a[i]=y; printf(“%d\n”,y); i++; count++; } } qsort(a,0,n-1) printf(“elements after sorting”); for(i=0;i<n;i++) printf(“%d\n”,a[i]); } 59 | P a g e WEEK – 8(a) problem statements P1: Write C program for implementing the Quick sort method to arrange a list of even integers between 1 to 100 integers in Ascending order . Test Case 1: 56 90 20 10 15 Not Even 10 20 56 90 P2: Write C program for implementing the Quick sort methods to arrange a list of odd integers between 1 to 100 integers in Ascending order . Test Case 1: 55 91 21 11 16 Not Odd 11 21 55 91 P3: Write C programs for implementing the Quick sort method to arrange a list of integer divisible by 3 & 5 between 10 to 100 in Ascending order Test Case 1: 15 90 25 45 10 Not divisible 3 or 5 15 25 45 90 P4: Write C programs for implementing the Quick sort method to arrange a list of integer divisible by 5 between 100 to 1000 in Ascending order Test Case 1: 50 90 20 10 14 Not divisible by 5 10 20 50 90 P5: Write C programs for implementing the Quick sort method to arrange a list of integer multiple of 3 between 1 to 100 in Ascending order Test Case 1: 56 90 15 10 Not Multiple of 3 15 56 90 P6: Write C programs for implementing the Quick sort method to arrange a list of integer less than 100 in Ascending order . Test Case 1: 56 90 20 10 150 greater than 100 10 20 56 90 P7: Write C programs for implementing the Quick sort method to arrange a list of integer greater than 100 in Ascending order Test Case 1: 156 190 120 100 90 Less than 100 100 120 156 190 P8: Write C programs for implementing the Quick sort method to arrange a list of integer multiples of 7 between 100 to 1000 in Ascending order Test Case 1: 156 190 200 500 1500 Not in range 100 1000 156 190 200 500 P9: Write C programs for implementing the Quick sort method to arrange a list of cube integer between 1 t0 500 in Ascending order b) perform insertion sort Test Case 1: 5 4 3 2 10 Not in range 1 - 500 8 27 64 125 60 | P a g e P10: Write C programs for implementing the Quick sort method to arrange a list of squares of integers between 1 to 50 in Ascending order . Test Case 1: 5 4 3 2 10 Not in range 1 - 50 4 9 16 25 Viva 1. Define Quick sort 2. what is the worst time complexity of quick sort? 3. Quick sort the following elements 15,20,10,5,25 61 | P a g e Left Side less than pivot (50) P 62 | P a g e 60 Pivot 20 Right Side Greater than pivot 40 70 10 WEEK-8 (B) Write C program for implementing the Selection sort method to arrange a list of integers in ascending order: Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists Generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited. The algorithm works as follows: 1. Find the minimum value in the list 2. Swap it with the value in the first position 3. Repeat the steps above for the remainder of the list (starting at the second position and advancing each time) Complexity of Selection Sort Best Case : O ( n2 ) Average Case : O ( n2 ) Worst Case : O ( n2 ) 63 | P a g e 8(B): Selection sort 1. Algorithm :void selectionsort(int a[20],int n) Step1 : Start Step2: Declare variables i,j,k,min,temp Step3: i=0, i<n-1,i++ repeat step3 to step 11 Step4: min=i; Step 5: for(j=i+1;j<n;j++) repeat step5 Step6: if(a[min]>a[j]) Step7: min=j; Step8: temp=a[i]; Step9: a[i]=a[min]; Step10: a[min]=temp; Step11: Stop 64 | P a g e to step 7 2. Algorithm : Main step1: start step2: declare i,n,a[10] step3: Read the size of an array step4: Read the elements of an array step5: call sub function step6: print sorted elements of an array a[i] step7: stop 65 | P a g e selectionsort(a,n); WEEK – 8(B) problem statements P1: Write C program for implementing the Selection sort method to arrange a list of even integers between 1 to 100 integers in Ascending order . Test Case 1: 56 90 20 10 15 60 Not Even 10 20 56 60 90 P2: Write C program for implementing the Selection sort method methods to arrange a list of odd integers between 1 to 100 integers in Ascending order . Test Case 1: 55 91 21 11 16 75 Not Odd 11 21 55 75 91 P3: Write C program for implementing the Selection sort method to arrange a list of integer divisible by 3 & 5 between 10 to 100 in Ascending order Test Case 1: 15 90 25 45 10 75 Not divisible 3 or 5 15 25 45 75 90 P4: Write C program for implementing the Selection sort method to arrange a list of integer divisible by 5 between 1 to 1000 in Ascending order Test Case 1: 50 90 20 10 14 Not divisible by 5 10 20 50 90 P5: Write C program for implementing the Selection sort method to arrange a list of integer multiple of 3 between 1 to 100 in Ascending order Test Case 1: 56 90 15 10 Not Multiple of 3 15 56 90 P6: Write C program for implementing the Selection sort method to arrange a list of integer less than 100 in Ascending order . Test Case 1: 56 90 20 10 150 greater than 100 10 20 56 90 P7: Write C programs for implementing the Selection sort method to arrange a list of integer greater than 100 in Ascending order Test Case 1: 156 190 120 100 90 Less than 100 100 120 156 190 P8: Write C programs for implementing the Selection sort method to arrange a list of integer multiples of 7 between 100 to 1000 in Ascending order Test Case 1: 156 190 200 500 1500 Not in range 100 1000 156 190 200 500 P9: Write C programs for implementing the Selection sort method to arrange a list of cube integer between 1 t0 500 in Ascending order Test Case 1: 5 4 3 2 10 Not in range 1 - 500 8 27 64 125 66 | P a g e P10: Write C programs for implementing the Selection sort method to arrange a list of squares of integers between 1 to 50 in Ascending order . Test Case 1: 5 4 3 2 Not in range 1 - 50 4 9 16 67 | P a g e 10 25 Week 9 (A): Write a C program to perform the Insertion into a B-tree. #define M 5 struct node { int n; int keys[M-1]; struct node *p[M]; }*root=NULL; enum KeyStatus { Duplicate,SearchFailure,Success,InsertIt,LessKeys }; 1. Algorithm for search:int searchPos(int key, int *key_arr, int n) Step1: Declare int pos=0; Step2:Repeat from step2 to step4 while (pos < n && key > key_arr[pos]) Step3: Increment the position pos++; Step4: return pos; Step5: Stop 2.Algorithm for insert: enum KeyStatus ins(struct node *ptr, int key, int *upKey,struct node**newnode) Step1: Start Step2:Declare struct node *newPtr, *lastPtr; int pos, i, n,splitPos; int newKey, lastKey; enum KeyStatus value; Step3: Check the pointer is Null upto step6 if (ptr == NULL) Step4: Assign *newnode = NULL; Step5: Assign *upKey = key; Step6: return InsertIt; Step7: n = ptr->n; Step8: call subfunction search pos = searchPos(key, ptr->keys, n); Step9: Check & return if (pos < n && key == ptr->keys[pos]) return Duplicate; Step10: call sub function insert value = ins(ptr->p[pos], key, &newKey, &newPtr); Step11: check value not equal to insert & return if (value != InsertIt) return value; Step12: Check n less than m-1 upto step 20 if (n < M - 1) Step13: call subfunction searchposition pos = searchPos(newKey, ptr->keys, n); Step14: Repeat from step 14 to step 16 for (i=n; i>pos; i--) Step15: ptr->keys[i] = ptr->keys[i-1]; 68 | P a g e Step16: ptr->p[i+1] = ptr->p[i]; Step17: ptr->keys[pos] = newKey; Step18: ptr->p[pos+1] = newPtr; Step19: ++ptr->n Step20 return Success; Step21:check if (pos == M – 1) upto step23 otherwise gotostep 24 Step22: lastKey = newKey; Step23: lastPtr = newPtr; Step24: else lastKey = ptr->keys[M-2]; Step25: lastPtr = ptr->p[M-1]; Step26 : repeat for (i=M-2; i>pos; i--) { ptr->keys[i] = ptr->keys[i-1]; ptr->p[i+1] = ptr->p[i]; } Step27: ptr->keys[pos] = newKey; Step28: ptr->p[pos+1] = newPtr; } Step29: splitPos = (M - 1)/2; Step30: (*upKey) = ptr->keys[splitPos]; Step31:(*newnode)=malloc(sizeof(struct node)); Step32: ptr->n = splitPos; Step33:(*newnode)->n = M-1-splitPos; Step34: repeat upto step 37 for (i=0; i < (*newnode)->n; i++) Step35: (*newnode)->p[i] = ptr->p[i + splitPos + 1]; Step36: check if(i < (*newnode)->n - 1) (*newnode)->keys[i] = ptr->keys[i + splitPos + 1]; Step37: else (*newnode)->keys[i] = lastKey; Step38: (*newnode)->p[(*newnode)->n] = lastPtr; Step39: return InsertIt; Step40: Stop 3. Algorithm :void insert(int key) Step1: Start Step2: Declare struct node *newnode; int upKey; enum KeyStatus value; Step3:value=ins(root,key,&upKey,&newnode); Step4: Check if (value == Duplicate) Step5:print Key already available Step6: check if (value == InsertIt) { struct node *uproot = root; root=malloc(sizeof(struct node)); root->n = 1; root->keys[0] = upKey; 69 | P a g e root->p[0] = uproot; root->p[1] = newnode; } Step7: Stop 4:Algorithm: void display(struct node *ptr, int blanks) Step1:Start Step2: Check if (ptr) upto step9 Step3:Repeat for(i=1;i<=blanks;i++) Step4: print(" ") Step5: repeat for (i=0; i < ptr->n; i++) Step6: print ptr->keys[i] Step7: print "\n" Step8: repeat for (i=0; i <= ptr->n; i++) Step9: call sub function display(ptr->p[i], blanks+10) Step10: Stop 5.Algorithm :int main() Step1: Start Step2: Declare int key,ch Step3:Read the Creation of B tree for node M Step4: repeat while(1) upto step 7 Step5:Read 1.Insert\n2.Display\n3.Quit Step6:Read the your choice ch Step7: switch(ch) case 1: printf("Enter the key : "); scanf("%d",&key); insert(key); break; case 2: printf("Btree is :\n"); display(root,0); break; case 3: exit(0); default:printf("Wrong choice\n"); break; Step8: stop 70 | P a g e Week 9(b): Write a C program for implementing Heap sort algorithm for sorting a given list of integers in ascending order. Globally declaring int a[10], n, i, j, c, root, temp; 1. Algorithm :void maxheap() Step1: Start Step2: repeat from step 2 to step 11 for (i = 1; i < n; i++) Step3: c=i Step4: repeat from step 5 to step to step 11 do Step5: root = (c - 1) / 2; Step6: check root less than c upto step 9 if (a[root] < a[c]) /* to create MAX heap array */ Step7: temp = a[root] Step8: a[root] = a[c] Step9: a[c] = temp; Step10: c = root; Step11: while (c != 0); Step12 print Max Heap array a[i] Step13: stop 2. Algorithm: main() Step1: start Step2: Read n no of elements Step3: Read the array of elements a[i] Step4: Call sub program maxheap() Step5: Repeat from step 6 to step 19 for (j = n - 1; j >= 0; j--) Step6: temp = a[0]; Step7: a[0] = a[j]; /* swap max element with rightmost leaf element*/ Step8: a[j] = temp; Step9: root = 0; Step10: Repeat from step 11 to step 19 do Step11: c = 2 * root + 1; /* left node of root element */ Step12: Check if ((a[c] < a[c + 1]) && c < j-1) Step13: c++; Step14: Check if (a[root]<a[c] && c<j) upto Step 17 Step15: temp = a[root] Step16: a[root] = a[c] Step17: a[c] = temp Step18: root = c Step19: while (c < j); Step20: print The sorted array is :a[i] Step21: Stop 71 | P a g e WEEK – 9(B) problem statements P1: Write C program for implementing the Heap sort method to arrange a list of even integers between 1 to 100 integers in Ascending order . Test Case 1: 56 90 20 10 15 60 Not Even 10 20 56 60 90 P2: Write C program for implementing the Heap sort method methods to arrange a list of odd integers between 1 to 100 integers in Ascending order . Test Case 1: 55 91 21 11 16 75 Not Odd 11 21 55 75 91 P3: Write C program for implementing the Heap sort method to arrange a list of integer divisible by 3 & 5 between 10 to 100 in Ascending order Test Case 1: 15 90 25 45 10 75 Not divisible 3 or 5 15 25 45 75 90 P4: Write C program for implementing the Heap sort method to arrange a list of integer divisible by 5 between 1 to 1000 in Ascending order Test Case 1: 50 90 20 10 14 Not divisible by 5 10 20 50 90 P5: Write C program for implementing the Heap sort method to arrange a list of integer multiple of 3 between 1 to 100 in Ascending order Test Case 1: 56 90 15 10 Not Multiple of 3 15 56 90 P6: Write C program for implementing the Heap sort method to arrange a list of integer less than 100 in Ascending order . Test Case 1: 56 90 20 10 150 greater than 100 10 20 56 90 P7: Write C program for implementing the Heap sort method to arrange a list of integer greater than 100 in Ascending order Test Case 1: 156 190 120 100 90 Less than 100 100 120 156 190 P8: Write C program for implementing the Heap sort method to arrange a list of integer multiples of 7 between 100 to 1000 in Ascending order Test Case 1: 156 190 200 500 1500 Not in range 100 1000 156 190 200 500 P9: Write C program for implementing the Heap sort method to arrange a list of cube integer between 1 t0 500 in Ascending order Test Case 1: 5 4 3 2 10 Not in range 1 - 500 8 27 64 125 72 | P a g e P10: Write C program for implementing the Heap sort method to arrange a list of squares of integers between 1 to 50 in Ascending order . Test Case 1: 5 4 3 2 Not in range 1 - 50 4 9 16 73 | P a g e 10 25 Week 10: Write a C program to implement all the functions of a dictionary (ADT) using hashing. Globally declare int a[10]; void display(); void put(int data,int key); void probing(int key,int data); 1. Algorithm :void set() Step1: Start Step2: Declare i Step3: repeat for(i=0;i<10;i++) Step4: a[i]=0 Step5: stop 2. Algorithm: void accept(int data) Step1: Start Step2:declare int key=data%10 Step3: call sub function put(data,key) Step4: Stop 3.Algorithm:void put(int data,int key) Step1: Start Step2: check if(a[key]==0) got step3 otherwise step4 Step3: a[key]=data Step4: lse Step5:call sub function probing(key,data) Step6: Stop 4. Algorithm :void probing(int key,int data) Step1: start Step2: repeat while(a[key]!=0) step3 Step3: key++ Step4: a[key]=data Step5: Stop 5. Algorithm : void search(int key) Step1: Start Step2: declare int i,index Step3: Assign index=(key%10) Step4: repeat for(i=0;i<10;i++) upto step 7 Step5: Check if(i==index) upto step 7 Step6: print element found at is i, a[i] Step7: exit(0) Step8: print element not found Step9: stop 6. Algorithm : void delete(int key) Step1: Start Step2: Declare int i,index 74 | P a g e Step3: Assign index=(key%10); Step4: repeat for(i=0;i<10;i++) upto step 9 Step5: Check if(i==index) upto step 9 Step6: print deleted element is a[i] Step7: a[i]=0; Step8: call sub function display() Step9: exit(0) Step10: print element not exist Step11: stop 7. Algorithm :void display() Step1: Start Step2: Declare int i; Step3: print display the list Step4: for(i=0;i<10;i++) Step5: print,i,a[i] Step6: Stop 8. Algorithm :void main() Step1: Start Step2: Declare i,b[20],n,ch,key; Step3: repeat do Step4:Read choice 1.create 2.search 3.delete 4.display 5.exit Step5: switch(ch) Step6: case 1: set(); Read no of elements n Read the elements b[i] call subfunction accept(b[i]) and display() break case 2: Read the key to searched call subfunction search(key) break case 3: Read the key to be deleted: call subfunction delete(key) break case 4: call subfunction display() break case 5:call subfunction exit(0) Step7: end of switch Step8: end of do -while(1) Step9: stop 75 | P a g e Week 11: Write a C program for implementing Knuth-Morris- Pratt pattern matching algorithm. 1. Algorithm for kmpSearch (char *str, char *word, int *ptr) Step1: Declare & assign i = 0, j = 0 Step2: Repeat i+j until less than string upto 11 while ((i + j) < strlen(str)) Step3:Checking the match found on the target and pattern string char upto Step6 if (word[j] == str[i + j]) Step:4 Check upto Step5 if (j == (strlen(word) - 1)) Step5: Read the location of the index word, i + 1 return Step6: j = j + 1 Step7: else otherwise check upto step manipulating next indices to compare Step8: i = i + j - ptr[j]; Step 9: check j > -1 if (ptr[j] > -1) Step10: j = ptr[j]; Step11: otherwise else j = 0; Step12 stop 2. Algorithm to find the overlap array for the given pattern findOverlap(char *word, int *ptr) Step1: Start Step2:Declare & assign i = 2, j = 0, len = strlen(word) ptr[0] = -1 ptr[1] = 0 Step3: Repeate upto i less than length while (i < len) Step4: Check upto step7 if (word[i - 1] == word[j]) Step5: j = j + 1 Step6: ptr[i] = j Step7: i = i + 1 Step8: otherwise check else if (j > 0) Step9: j = ptr[j] Step10: otherwise Check upto 12 else Step11: ptr[i] = 0 Step12: i = i + 1 Step13: stop 76 | P a g e 3. Algorithm : main function Step1: Start Step2: Declare word[256], str[1024] *ptr, i Step3: Read the target string from the user Step4: Read the pattern string word from the user Step5: dynamic memory allocation for overlap array ptr = (int *)calloc(1, sizeof(int) * (strlen(word))) Step6: call sub function overlap findOverlap(word, ptr) Step7: call the subfunction find the index of the pattern in target string kmpSearch(str, word, ptr); Step8: Stop 77 | P a g e Week 12: Write C programs for implementing the following graph traversal algorithms: a)Depth first traversal A) DFS(depth-first search) Algorithm Globally declare int stack[20],top=-1,a[20][20],vis[20]; int pop(); void push(int item); 1 .Algorithm void dfs(int s,int n) Step1: Declare integer variable i,k Step2: call subfunction push(s) Step3: Assign vis[s]=1 Step4: call subfunction k=pop() Step5: check k notequal to 0 if(k!=0) Step6: Print k Step7: Repeat while(k!=0) upto Step: 14 Step8: Repeat for(i=1;i<=n;i++) upto Step: 14 Step9: Check upto Step: 11 if((a[k][i]!=0)&&(vis[i]==0)) Step10: call subfunction push(i); Step11: vis[i]=1; Step12: Assign k=pop(); Step13: Check if(k!=0) Step14: print k Step15: Repeat for(i=1;i<=n;i++) Step16: Check if(vis[i]==0) Step17: call subfunction dfs(i,n); Step:18 Stop 2. Algorithm : void push(int item) Step1: Start Step2: Check top equal to 19 if(top==19) Step3: print Stack overflow Step4: otherwise stack[++top]=item; Step5: stop 3.Algorithm : int pop() Step1: Start Step2: Declare int k; Step3: check top equal to -1 if(top==-1) return(0); Step4: otherwise check upto 6 Step5:k=stack[top--]; Step6: return(k); Step7: stop 78 | P a g e 4. Algorithm: int main() Step1: Start Step2: Declare int n,i,s,j; Step3: Read the number vertices's n Step4: Read 1 if has a node else 0 i,j Step5: Print The ADJACENCY MATRIX IS a[i][j] Step6: Repeat for(i=1;i<=n;i++) Step7: Assign vis[i]=0; Step8: Read the source vertex s Step: call sub function bfs(s,n); Step: Stop 79 | P a g e B) BFS(breadth-first search) Algorithm Globally declare int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20]; int delete(); void add(int item); 1. Algorithm: void bfs(int s,int n) Step1:Start Step2: Declare int p,i; Step3: call subfunction add(s); Step4: assign vis[s]=1; Step5: call subfunction p=delete(); Step6: Check if(p!=0) Step7: print p Step8: Repeat while(p!=0) Step9: Repeat for(i=1;i<=n;i++) Step10: Check if((a[p][i]!=0)&&(vis[i]==0)) Step11: call subfunction add(i); Step12: assign vis[i]=1; Step13: call subfunction p=delete(); Step14: Check if(p!=0) Step15: print p Step16: Repeat for(i=1;i<=n;i++) Step17: if(vis[i]==0) Step18: call subfunction bfs(i,n); Step19: Stop 2. Algorithm: void add(int item) Step1: Start Step2: if(rear==19) Step3: print QUEUE FULL Step4: else Step5: if(rear==-1) Step6: q[++rear]=item; Step7: front++; Step8: otherwise else Step9: q[++rear]=item; Step10: Stop 3. Algorithm: int delete() Step1: Start Step2: Declare int k; Step3: Check if((front>rear)||(front==-1)) return(0); Step4: otherwise Step5: k=q[front++]; Step6: return(k); 80 | P a g e 4. Algorithm: int main() Step1: Start Step2: Declare int n,i,s,j; Step3: Read the number vertices's n Step4: Read 1 if has a node else 0 i,j Step5: Print The ADJACENCY MATRIX IS a[i][j] Step6: Repeat for(i=1;i<=n;i++) Step7: Assign vis[i]=0; Step8: Read the source vertex s Step: call sub function bfs(s,n); Step:Stop 81 | P a g e