CS0213 – DATA STRUCTURES AND ALGORITHM LAB USING C & C++ LABORATORY MANUAL II - CSE III - SEMESTER ACADEMIC YEAR: 2012-2013 SCHOOL OF COMPUTER SCIENCE & ENGINEERING SRM UNIVERSITY, SRM NAGAR, KATTANKULATHUR-603203. KANCHEEPURAM DISTRICT. 1 SCHOOL OF COMPUTER SCIENCE & ENGINEERING SRM UNIVERSITY, SRM NAGAR, KATTANKULATHUR-603203. SUBJECT : DATA STRUCTURES AND ALGORITHM SUBJECT CODE : CS0213 SEMESTER : III CLASS : II CSE HOURS / WEEK : 6 HOURS EX. NO 1 2 3 4 5 6 7 8 9 10 11 12 NAME OF THE EXPERIMENTS PAGE NO LINEAR SEARCH 3 BINARY SEARCH 5 SELECTION SORT 7 BUBBLE SORT 9 INSERTION SORT 11 MERGE SORT 13 QUICK SORT 15 STACKS 17 QUEUES 19 MAXIMUM AND MINIMUM 21 KNAPSACK PROBLEM 23 SINGLY LINKED LIST 25 2 LINEAR SEARCH AIM: To find a given target number using linear search from the list of numbers. ALGORITHM: Step 1: Initialize the integer variables Step 2: Get the target number from user Step 3: Get the list of numbers to be searched Step 4: If the counter is equal to target the print the location Step 5: If it is not equal to then print that the location is not found 3 INPUT: Enter the number to be found: 25 Enter the number of elements in the list: 5 Enter the elements: 12 22 37 25 61 OUTPUT: The number is found in location: 4 RESULT: The target number is found using linear search successfully 4 BINARY SEARCH AIM: To find a given target number using Binary Search from the list of number. ALGORITHM: Step 1: Initialize the integer variables Step 2: Get the values from the list Step 3: Sorted List of numbers is got as input. Step 4: Get the target number from the user. Step 5: Initialize first value as zero and last as n-1; Step 6: The mid value is found. Step 7: If the target is greater than mid then first is mid + 1 if not last is mid -1. Step 8: First is last +1 Step 9: The target is found, if its equal to a(mid) otherwise target is not found. 5 INPUT: Enter the number of elements in the list: 5 Enter the sorted list: 11 22 33 44 55 Enter the target: 33 OUTPUT: The target is found in location: 3 RESULT: The target number is found using Binary Search successfully 6 SELECTION SORT AIM: To sort the given data using selection sort. ALGORITHM: Step 1: Initialize the integer variables. Step 2: Get the total number of values from the user. Step 3: Get the numbers to be sorted. Step 4: Initialize i to current Step 5: If current is less than j ,then initialize j to current. Step 6: Assign j to temp, current to i and temp to current for swapping Step 7: print the sorted data 7 INPUT Enter the number of elements: 5 Enter the Numbers to be sorted: 31 21 77 62 45 OUTPUT Sorted Data: 21 31 45 62 77 RESULT: The given data is sorted and printed using selection sort. 8 BUBBLE SORT AIM: To sort the given data using Bubble sort. ALGORITHM: Step 1: Initialize the variables i,j,n, and temp. Step 2: Get the number of elements. Step 3: Get the numbers to be sorted. Step 4; Assign a[i] to temp Step 5: Assign a[i+1] to a[i] Step 6: Assign Temp to a[i+1] Step 7: The data is sorted. 9 INPUT Enter the number of elements: 6 Enter the numbers to be sorted: 22 66 11 99 33 77 OUTPUT Sorted Data: 11 22 33 66 77 99 RESULT: The given data is sorted and printed using Bubble sort. 10 INSERTION SORT AIM: To sort the given data using Insertion sort. ALGORITHM: Step 1: Initialize the variables i,j,n, curr and temp. Step 2: Get the number of elements. Step 3: Get the numbers to be sorted. Step 4: If the second element is less than the first element then assign it to temp. Step 5: Assign the first element to second element. Step 6: Assign Temp to first element. Step 7: The swapping is done Step 8: The data is sorted. 11 INPUT Enter the number of entries: 5 Enter the list of no to be sorted: 25 15 35 05 45 OUTPUT The sorted list: 05 15 25 35 45 RESULT: The given data is sorted and printed using Insertion Sort. 12 MERGE SORT AIM: To sort the given data using Merge sort. ALGORITHM: Step 1: Initialize the variables i,j,n,h,k,low,high. Step 2: Get the number of elements. Step 3: Get the numbers to be sorted. Step 4: If low is less than high then find the mid value. Step 5: Initialize h and i to low. Step 6: Initialize a[h] to b[i] else a[j] to b[i]. Step 7: If h is greater than mid then a[k] == b[i] else b[k] == a[k] Step 8 The data is sorted and printed. 13 INPUT Enter the number of elements: 7 The elements: 22 66 11 44 99 33 77 OUTPUT Sorted Data: 11 22 33 44 66 77 99 RESULT: The given data is sorted and printed using Merge Sort. 14 QUICK SORT AIM: To sort the given data using Quick Sort. ALGORITHM: Step 1: Initialize the variables i,j,n,r,p,q. Step 2: Get the number of elements. Step 3: Get the numbers to be sorted. Step 4: If p is less than q then the function is called. Step 5: If i is less than j, then interchange them. Step 6: If I is less than pivort interchange them. Step 7: The data is sorted and printed. 15 INPUT Enter the number of elements: 5 Enter the unsorted list: 39 62 57 41 21 OUTPUT The sorted list is: 21 41 39 57 62 RESULT: The given data is sorted and printed using Quick Sort. 16 STACKS AIM: To perform all stacks operation. ALGORITHM: Step 1: Initialize the integer variables. Step 2: In a switch case, in case 1 the number to be pushed is got Step 3:Top is now equal to top+1 Step 4: Else the stack is full Step 5: In case 2 the number to be deleted is got Step 6: Top is now equal to top-1 Step 7: Else the stack is empty Step 8: In case 3 if the top is less than 0 the stack is full Step 9: Else the stack is printed Step 10: Default is no such choice INPUT / OUTPUT Enter the operation to be performed: 1) push 2) pop 3) display 4) exit 17 1 Enter the number to be Added: 11 Enter the operation to be performed: 1) push 2) pop 3) display 4) exit 1 Enter the number to be Added: 22 Enter the operation to be performed: 1) push 2) pop 3) display 4) exit 1 Enter the number to be Added: 33 Enter the operation to be performed: 1) push 2) pop 3) display 4) exit 1 Enter the number to be Added: 44 Enter the operation to be performed: 1) push 2) pop 3) display 4) exit 2 Enter the number to be Added: 44 Enter the operation to be performed: 1) push 2) pop 3) display 4) exit 3 The stack is 11 22 33 Enter the operation to be performed: 1) push 2) pop 3) display 4) exit 4 RESULT: All the stack operations are performed using the switch case 18 QUEUES AIM: To perform all the queue operations ALGORITHM: Step 1:Initialize the integer variables Step 2:In a switch case in case 1 get the number to be added in the queue Step 3: Else the queue is full Step 4: In case 2 get the number to be deleted from the queue Step 5: Else the queue is full Step 6: If tail and head are less than 0 then in case 3 the queue is full Step 7: Else the queue is printed Step 8: The default case is no such choice 19 INPUT / OUTPUT: Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit 1 Enter the number to be added 11 Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit 1 Enter the number to be added 22 Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit 1 Enter the number to be added 33 Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit 1 Enter the number to be added 44 Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit 2 The number to be deleted is 11 Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit 3 The queue is 22 33 44 RESULT: All the queue operations are performed 20 MAXIMUM AND MINIMUM AIM: To find the maximum and minimum value in a given list of numbers ALGORITHM: Step 1:Initialise the variables in the main function Step 2: Get the total number of items Step 3: Get the numbers from the user Step 4: The function is called. If there is only one element then the max and min is the same element Step 5: If there is two element then check if a[x] is less than a[y] then a[x] is min and a[y] is max Step 6: If there is more number of items mid value is found Step 7: Now max is assigned to t1 and min to t2 Step 8: If the max is less than t1 the max is t1,if min is greater than t2 the min t2 Step 9: The min and max values are found and printed 21 INPUT Enter the no of numbers: 6 Enter the numbers: 33 59 11 67 92 05 OUTPUT Maximum Number = 92 Minimum Number = 05 RESULT: The maximum and minimum number is found and printed. 22 KNAPSACK PROBLEM AIM: To find the maximum profit from the items in the bag. ALGORITHM: Step 1: Initialize the variables, profit, weight, p/w and maxprofit. Step 2: Get the number of elements. Step 3: Get the capacity of the bag. Step 4: Get the profit of items in the bag. Step 5: Get the weight of each item in the bag. Step 7: Assign pw[j] to t1, p[j] to t2 and w[j] to t3. Step 8: If w[i] > capacity then find the ratio of capacity to weight of each item. Step 9: Else subtract the weight of the item from the total capacity. Step 10: Solution vector is found and printed. Step 11: The maximum profit is found and printed. 23 INPUT/OUTPUT: Enter the number of elements: 4 Enter the capacity: 4 Enter the profit: 5 9 4 8 Enter the weight: 1 3 2 2 Enter the p/w ratio: 5 3 2 4 Solution vector: 13 15 16 Profit: 16 RESULT: The maximum profit is found and printed on the screen. 24 SINGLY LINKED LIST AIM: To perform all the singly linked list operations ALGORITHM: Step 1: Declare the functions to create,display,count. Step 2: Declare the variables in the main function. Step 3: In a switch case get each functions number. Step 4: To append the list create the memory by using malloc function. Step 5: Assign a variable temp using pointers. Step 6: To delete a node create a dummy variable. Step 7: Check if the list is empty otherwise display the list using for statement. Step 8: To insert a node in middle, allocate memory using malloc function 25 INPUT / OUTPUT: Singly Linked List: 1. Create or Append List 2. Insert in Beginning 3. Insert in Middle 4. Remove from the List 5. Count 6. Display 7. Quit Enter your Choice: 1 Enter any number: 12 Enter more(y/n): y Enter any number: 13 Enter more(y/n): n Singly Linked List: 1. Create or Append List 2. Insert in Beginning 3. Insert in Middle 4. Remove from the List 5. Count 6. Display 7. Quit Enter your Choice: 2 Enter the Data to be inserted at Beginning: 11 Singly Linked List: 1. Create or Append List 2. Insert in Beginning 3. Insert in Middle 4. Remove from the List 5. Count 6. Display 7. Quit 26 Enter your Choice: 3 Enter the Position to be Inserted: 2 Enter the Data: 14 Singly Linked List: 1. Create or Append List 2. Insert in Beginning 3. Insert in Middle 4. Remove from the List 5. Count 6. Display 7. Quit Enter your Choice: 4 Enter the Data to be Deleted: 12 Singly Linked List: 1. Create or Append List 2. Insert in Beginning 3. Insert in Middle 4. Remove from the List 5. Count 6. Display 7. Quit Enter your Choice: 5 The Number of Nodes: 3 27 Singly Linked List: 1. Create or Append List 2. Insert in Beginning 3. Insert in Middle 4. Remove from the List 5. Count 6. Display 7. Quit Enter your Choice: 6 11 14 13 RESULT: All the Singly Linked List operations are performed. 28