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
NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE EXPERIMENT NO.1 Date of conduction:- Date of submission:- Submitted by other members:1. 2. 3. 4. 5. 6. 7. 8. Group no:- Signature Name of Technical Assistant: Page 1 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Objective: -Write a program to implement an Array and find largest element in the array. Apparatus:platform. C or C++ compiler which either support windows Theory: A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array Linked List Stacks Queues Operations on linear Data Structures Traversal : Visit every part of the data structure Search : Traversal through the data structure for a given element Insertion : Adding new elements to the data structure Deletion : Removing an element from the data structure. Sorting : Rearranging the elements in some type of order(e.g Increasing or Decreasing) Merging : Combining two similar data structures into one ALGORITHM : 1. 2. 3. 4. 5. Set i=LB //Initializing the counter variable Repeat step3 and 4 while i<=UB VISIT (A[i]) //process the element i=i+1 //Moving to next location, increment the counter End void insert_at(int arr[], int n, int idx, int val) { Page 2 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE if (idx==-1) arr[n] = val; // append else { for(int i = n; i > idx; i—) arr[i] = arr[i-1]; arr[idx] = val; } } void bsort(int arr[], int n) { int i,j; for(i = 0; i < n; i++) for(j = i+1; j < n; j++) if (arr[i] > arr[j]) { // swap arr[i] and arr[j] show_arr(arr,n); // print out array before swap int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } show_arr(arr,n); // show sorted array } Procedure: #include <stdio.h> #include<conio.h> void main() { int array[50], size, i, largest; cout<<"\n Enter the size of the array:"; cin>>size; cout<<"Enter elements of the array: "; cin>>size; for (i = 0; i < size; i++) scanf("%d", &array[i]); largest = array[0]; for (i = 1; i < size; i++) { Page 3 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE if (largest < array[i]) largest = array[i]; } cout<<"largest element present in the given array is :"; cout<largest; getch(); } Observation Table:-N/A Calculation:- N/A Results: - Page 4 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Conclusion:Precautions:Suggestions:- Lab Quiz:Page 5 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Q-1 Variable which can be accessed by all modules of the program is called as __________. a)Static Variable b) Local Variable c)Global Variable d)Auto Variable Q-2 In what kind of storage we can easily insert, delete, concatenate and rearrange substrings? a)Array b)Queue c)Stack d)Linked List Q-3 Term Data Structure refers to _________ and interrelationship between them. a)Organization of data element b)Coding Standards c)None of these d)Programming Language Statement Q-4 Data is nothing but ____________. a)Programming statement b)Bunch of Information c)None of These d)Piece Of Information Q-5 ADT is called as Abstract because a) Implementation Detail are hidden b)It is a completely independent data type c)It is a collection of different data type d) None of these Q-6 _________________ defines a set of primitive elements which do not involves any other element as its sub-part. a) Linear Data Structure. b) Non-Primitive Data Structure. c) Primitive Data Structure. d) Non Linear Data Structure. Q-7 Primitive data Structures are generally _________ data types in programming language a)Built in Data Type b)User Define Data Type Page 6 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Q-8 In linear data structure each and every element have unique predecessor and unique successor! a)True b)False Q-9 In _________ data structure, data contain hierarchical and network relationship between elements. a)Non Linear b) Linear Q-10 Select non linear data structures from the list of following data structures ! [Select appropriate options] a)Link List b)Stack c) Graph d)Tree Book: Lab experiment related theory available in following books: Book Name Author Web resources: 1. NPTEL lectures notes and their exercise. Page 7 of 77 Page No. NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE EXPERIMENT NO. 2 Date of conduction:- Date of submission:- Submitted by other members:1. 2. 3. 4. 5. 6. 7. 8. Group no:- Signature Name of Technical Assistant: Page 8 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Objective: -Write a program to implement Linear Search algorithm. Apparatus:platform. C or C++ compiler which either support windows Theory: In computer science, linear search or sequential search is a method for finding a particular value in a list that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found. Linear search is the simplest search algorithm; it is a special case of brute-force search. Its worst case cost is proportional to the number of elements in the list; and so is its expected cost, if all list are equally likely to be searched for. elements ALGORITHM : /**<p>Finding an element through linear search --BruteForce method</p> * @param a * @param number * @return searchIndex * Time Complexity : Page 9 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE * Best case : O(1) * Worst case : O(n) */ public static int linearSearch(int[] a,int number){ //length of the array int n=a.length; //Bruteforce method :: loop through n numbers if element found return the index, else return -1 for(int i=0;i<n;i++){ if(a[i]==number){ return i; } } return -1; } Procedure: # include<stdio.h> int linear_search(int arr[],int n,int item) { int i; for(i=0;i < n;i++) { if(item == arr[i]) { return(i+1); } }/*End of for*/ if(i == n) return(0); } main() { int arr[20],n,i,item,pos; printf("How many elements you want to enter in the array : "); scanf("%d",&n); for(i=0; i < n;i++) { printf("Enter element %d : ",i+1); scanf("%d", &arr[i]); } printf("Enter the element to be searched : "); Page 10 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE scanf("%d",&item); pos=linear_search(arr,n,item); if(pos>0) { printf(" %d found at %d position\n",item,pos); } else {printf("%d not found\n",item); } } Observation Table:-N/A Calculation:- N/A Results: - Page 11 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Conclusion:Precautions:Suggestions:- Lab Quiz :1. Which of the following correctly declares an array? A. int anarray[10]; B. int anarray; C. anarray{10}; D. array anarray[10]; 2. What is the index number of the last element of an array with 29 elements? A. 29 B. 28 C. 0 D. Programmer-defined 3. Which of the following is a two-dimensional array? A. array anarray[20][20]; B. int anarray[20][20]; C. int array[20, 20]; D. char array[20]; 4. Which of the following correctly accesses the seventh element stored in foo, an array with 100 elements? A. foo[6]; B. foo[7]; C. foo(7); D. foo; 5. Which of the following gives the memory address of the first element in array foo, an array with 100 elements? Page 12 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE A. foo[0]; B. foo; C. &foo; D. foo[1]; 6. What is the maximum numbers of dimensions an array in c may have? A. 2 B. 8 C. 20 D. Theoretically no limit . 7. An array elements are always stored in___________memory locations? A. Sequential B. Random C. Sequential Random D. None of these 8. What is the right way to initialize an array? A. int num[6]={2,4,12,5,45,5}; B. int num{6}={2,4,12,5,45,5}; C. int num{6}={2,4,12}; D. int num(6)={2,4,12,5,45,5}; 9. Size of the array need not be specified ,when A. initialization is a part of definition B. It is a declaration C. it is a formal parameter D. All of these 10. What will be printed after execution of the following code? Void main() { int a[10]={1,2,3,4,5}; Printf(“%d”,a[5]); } A. 5 B. 6 Page 13 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE C. 0 D. Garbage value E None of these Book: Lab experiment related theory available in following books: Book Name Author Web resources: 1. NPTEL lectures notes and their exercise. Page 14 of 77 Page No. NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE EXPERIMENT NO. 3 Date of conduction:- Date of submission:- Submitted by other members:1. 2. 3. 4. 5. 6. 7. 8. Group no:- Signature Name of Technical Assistant: Objective: -Write a program to implement Stack. Page 15 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Apparatus:platform. C or C++ compiler which either support windows Theory: Introduction: 1. 2. Stack is basically a data object The operational semantic (meaning) of stack is LIFO i.e. last in first out Definition : It is an ordered list of elements n , such that n>0 in which all insertions and deletions are made at one end called the top. Primary operations defined on a stack: ALGORITHM : 1. PUSH : add an element at the top of the list. 2. POP : remove the at the top of the list. Push , n,"IsEmpty()" top) Pop (item,array,top) 3. (item,array Also and IsFull" function, which tests whether a stack is empty or full respectively. { { Examplexample: if ( box,dishes top<= 0) kept one on top of = top) daily life : a pile of heavy books kept in a vertical 1.If ( n> Practical another Then print " stack is empty". Then print "Stack is full" ; 2. In computer world : In processing of subroutine calls andElse returns ; there is an explicit use of Else of return addresses. stack { in evaluation of arithmetic expressions , stack is used. { Also item = array[top]; top = top + 1; Large number of stacks can be expressed using a single one dimensional stack only. Such an array is top = top – 1; array[top] = itemstack ; array. called a multiple } } Procedure: #include<stdio.h> #include<conio.h> #define max 5 Page 16 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE int top=-1; int stack_arr[max]; void push() { int pushed_item; if(top==(max-1)) { printf ("stack overflow\n"); } else { printf("enter the item to be pushed in stack :"); scanf ("%d",& pushed_item); top=top+1; stack_arr[top]=pushed_item; } } void pop() { if(top==-1) { printf ("stack is underflow\n"); } else { Page 17 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE printf ("poped element is : %d\n",stack_arr[top]); top=top-1; } } void display() { int i; if(top==-1) { printf ("stack is empty \n"); } else { printf ("stack element: \n");} for(i=top; i>=0; i--) { printf ("%d \n",stack_arr[i]); } } main() { int choice; clrscr(); Page 18 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE while(1) { printf ("1: push\n"); printf ("2: pop\n"); printf ("3: display\n"); printf ("4: quit\n"); printf ("enter ur choice : "); scanf ("%d",&choice); switch (choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(1); default: printf ("wrong choice"); Page 19 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE } } } Observation Table:-N/A Calculation:- N/A Results: - Page 20 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Conclusion :Precautions:Suggestions:Page 21 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Lab Quiz :Q-1 In a stack ,If user try to remove element from the empty stack then it is called________ a)Garbage Collection b)Underflow of Stack c)Empty Collection d)Over Flow of Stack Q- 2 User push one element in the stack having already five element having stack size as 5 then stack become _____________ a)Garbage Collection b)Underflow of Stack c)Empty Collection d)Over Flow of Stack Q-3 User perform following operations on stack of size 5 then push(1); pop(); push(2); push(3); pop(); push(4); pop(); pop(); push(5); at the end of last operation, total number of elements present in the stack are a)2 b)1 c)3 d)4 Q-4 In order to keep track of current topmost element of the stack we need to maintain one variable. a) True b) False Q-5 Consider Stack is implemented using the array. #define MAX 10 struct STACK { int arr[MAX] int top = ___________; } Page 22 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE What will be the initial value with which top is initialized. a)Garbage b)0 c)1 d)-1 Q-6 Consider Stack is implemented using the array. #define MAX 10 struct STACK { int arr[MAX] int top = -1; } In this implementation of stack maximum value of top which cannot cause overflow will _________. a)10 b)11 c)Any Other d)9 Q-7 User perform following operations on stack of size 5 then push(1); pop(); push(2); push(3); pop(); push(2); pop(); pop(); push(4); pop(); pop(); push(5); Which of the following is correct statement for stack ? a)Stack Operation will perform Smoothly b)Underflow Occur c)Overflow Occur d)None Of these Q-8 stack in data structure is_______ a)FIFO b)LIFO c)LILO Page 23 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE d)None of these Q- 9 In the stack process of inserting an element in the stack is called as _____________ a)Create b)Push c)Evaluation d)Pop Q-10 Process of removing element from the stack is called as________ a) Push b) Postfix Expression c) Pop d) Create Book: Lab experiment related theory available in following books: Book Name Author Web resources: 1. NPTEL lectures notes and their exercise. Page 24 of 77 Page No. NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE EXPERIMENT NO. 4 Date of conduction:- Date of submission:- Submitted by other members:1. 2. 3. 4. 5. 6. 7. 8. Group no:- Signature Name of Technical Assistant: Objective: -Write a program to implement Queue. Page 25 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Apparatus:platform. C or C++ compiler which either support windows Theory: Introduction: 1. It is basically a data object 2. Theoperational semantic of queue is FIFO i.e. first in first out Definition : It is an ordered list of elements n , such that n>0 in which all deletions are made at one end called the front end and all insertions at the other end called the rear end . Primary operations defined on a Queue: 1. EnQueue : This is used to add elements into the queue at the back end. 2. DeQueue : This is used to delete elements from a queue from the front end. 3. Also "IsEmpty()" and "IsFull()" can be defined to test whether the queue is Empty or full. Example : 1. PRACTICAL EXAMPLE : A line at a ticket counter for buying tickets operates on above rules 2. IN COMPUTER WORLD : In a batch processing system, jobs are queued up for processing. ALGORITHM : #include<stdio.h> #include<conio.h> # define max 5 int queue_arr[max]; int rear=-1; int front=-1; void insert(int item) { if(rear==max-1) printf("Queue overflow\n"); Page 26 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE else { if(front==-1) { front=0; } rear=rear+1; queue_arr[rear]=item; } } void del() { if((front==-1)||(front>rear)) { printf("queue is underflow"); return; } else { printf("elment is deleted from queue is:%d\n",queue_arr[front]); front=front+1; } } void display() { int i; if(front==-1) printf("queue is empty\n"); else { printf("queue is :\n"); for(i=front;i<=rear;i++) printf("%d",queue_arr[i]); printf("\n"); } } void main() { int choice, item; do { printf("1.insert\n"); printf("2.delete\n"); printf("3.display\n"); Page 27 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE printf("4.quit\n"); printf("enter ur choice"); scanf("%d",&choice); switch(choice) { case 1: printf(" input the element for adding in queue"); scanf("%d",&item); insert(item); break; case 2: del(); break; case 3: display(); break; case 4: break; default: printf("wrong choice\n"); } } while(choice!=4); } Procedure: Observation Table:-N/A Calculation:- N/A Results: - Page 28 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Conclusion :Precautions:Suggestions:Page 29 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Lab Quiz :1.Which data structure allows deleting data elements from front and inserting at rear? a. Stacks b. Queues c. Deques d. Binary search tree 2. Identify the data structure which allows deletions at both ends of the list but insertion at only one end. a. Input-restricted deque b. Output-restricted deque c. Priority queues d. None of above 3. Which of the following data structure is non-linear type? a. Strings b. Lists c. Stacks d. None of above 4. Which of the following data structure is linear type? a. Strings b. Lists c. Queues d. All of above 5. To represent hierarchical relationship between elements, which data structure is suitable? a. Deque b. Priority c. Tree d. All of above 6. Which of the following data structure is used in BFS algorithm? a. Strings b. Lists c.Stacks d. None of above 7. Which of the following data structure is can be used for implementing queue? a. Strings b. Graph c. Array d. None of above 8. Which of the following data structure is can be used for implementing queue? a. Strings b. Stack c. linked List Page 30 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE d. None of above Book: Lab experiment related theory available in following books: Book Name Author Web resources: 1. NPTEL lectures notes and their exercise. Page 31 of 77 Page No. NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE EXPERIMENT NO. 5 Date of conduction:- Date of submission:- Submitted by other members:1. 2. 3. 4. 5. 6. 7. 8. Group no:- Signature Name of Technical Assistant: Objective: -Write a program to implement Binary search. Page 32 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Apparatus:platform. C or C++ compiler which either support windows Theory: In computer science, a binary search or half-interval search algorithm locates the position of an item in a sorted array. Binary search works by comparing an input value to the middle element of the array. The comparison determines whether the element equals the input, less than the input or greater. When the element being compared to equals the input the search stops and typically returns the position of the element. If the element is not equal to the input then a comparison is made to determine whether the input is less than or greater than the element. Depending on which it is the algorithm then starts over but only searching the top or bottom subset of the array's elements. If the input is not located within the array the algorithm will usually output a unique value indicating this. Binary search algorithms typically halve the number of items to check with each successive iteration, thus locating the given item (or determining its absence) in logarithmic time. ALGORITHM : int binary_search(int A[], int key, int imin, int imax) { // test if array is empty if (imax < imin) // set is empty, so return value showing not found return KEY_NOT_FOUND; else { // calculate midpoint to cut set in half int imid = midpoint(imin, imax); // three-way comparison if (A[imid] > key) // key is in lower subset return binary_search(A, key, imin, imid-1); else if (A[imid] < key) // key is in upper subset return binary_search(A, key, imid+1, imax); else // key has been found return imid; } } Procedure: #include <stdio.h> Page 33 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE int binary_search(int arr[],int n,int item) { int start,end,middle,i,pos; start=0; end=n-1; middle=(start+end)/2; while(start <= end) { if(item==arr[middle]) return(middle+1); else if(item > arr[middle]) start=middle+1; else if(item <arr[middle]) end=middle-1; middle=(start+end)/2; } return(0); } main() { int arr[20],n,i,item,pos; printf("How many elements you want to enter in the array : "); scanf("%d",&n); for(i=0; i < n; i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr[i]); } printf("Enter the element to be searched : "); scanf("%d",&item); pos=binary_search(arr,n,item); if(pos>0) { printf(" %d found at %d position\n",item,pos); } else {printf("%d not found\n",item); } }/*End of main()*/ Page 34 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Observation Table:-N/A Calculation:- N/A Results: - Conclusion :Precautions:Suggestions:- Page 35 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Lab Quiz :1. Which of the following is not the required condition for binary search algorithm? a. The list must be sorted b. there should be the direct access to the middle element in any sublist c. There must be mechanism to delete and/or insert elements in list d. none of above 2. Which of the following is not a limitation of binary search algorithm? a. must use a sorted array b. requirement of sorted array is expensive when a lot of insertion and deletions are needed c. there must be a mechanism to access middle element directly d. binary search algorithm is not efficient when the data elements are more than 1000. 3. Two dimensional arrays are also called a. tables arrays b. matrix arrays c. both of above d. none of above 4. A variable P is called pointer if a. P contains the address of an element in DATA. b. P points to the address of first element in DATA c. P can store only memory addresses d. P contain the DATA and the address of DATA 5. Which of the following data structure can't store the non-homogeneous data elements? a. Arrays b. Records c. Pointers d. None 6. Binary search algorithm can not be applied to a. sorted linked list b. sorted binary trees c. sorted linear array d. pointer array 7.The complexity of Binary search algorithm is a. O(n) b. O(logn) c. O(n2) d. O(nlogn) 8.The complexity of Linear search algorithm is a. O(n) b. O(logn) c. O(n2) Page 36 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE d. O(nlogn) 9.Which algorithm is used to search element fast a. Linear search b. Binary search algorithm c. Binary tree d. None of them Book: Lab experiment related theory available in following books: Book Name Author Web resources: 1. NPTEL lectures notes and their exercise. Page 37 of 77 Page No. NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE EXPERIMENT NO. 6 Date of conduction:- Date of submission:- Submitted by other members:1. 2. 3. 4. 5. 6. 7. 8. Group no:- Signature Name of Technical Assistant: Page 38 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Objective: -Write a program to implement bubble sort algorithm. Apparatus:platform. C or C++ compiler which either support windows Theory: - Bubble sort has many of the same properties as insertion sort, but has slightly higher overhead. In the case of nearly sorted data, bubble sort takes O(n) time, but requires at least 2 passes through the data (whereas insertion sort requires something more like 1 pass). Properties Stable O(1) extra space O(n2) comparisons and swaps Adaptive: O(n) when nearly sorted ALGORITHM : Algorithm for i = 1:n, swapped = false for j = n:i+1, if a[j] < a[j-1], swap a[j,j-1] swapped = true → invariant: a[1..i] in final position break if not swapped end Procedure: #include<stdio.h> #include<conio.h> #define MAX 20 void bubble_sort(int arr[],int size) Page 39 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE { int i,j,temp,n; for(i=0;i<size-1;i++) { for(j=0;j<size-1-i;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } } main() { int arr[MAX],i,j,n; clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); Page 40 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE for(i=0;i<n;i++) { printf("Enter element %d:",i+1); scanf("\n %d",&arr[i]); } printf("\n unsorted list is "); for(i=0;i<n;i++) { printf("%d",arr[i]); printf("\n"); } bubble_sort(arr,n); printf("sorted listis:\n"); for(i=0;i<n;i++) { printf("%d",arr[i]); printf("\n"); } getch(); } Page 41 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Observation Table:-N/A Calculation:- N/A Results: - Conclusion :Precautions:Suggestions:- Lab Quiz :1.The complexity of Bubble sort algorithm is A. O(n) B. O(log n) Page 42 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE C. O(n2) D. O(n log n) 2.Which of the following case does not exist in complexity theory A. Best case B. Worst case C. Average case D. Null case 3. How many number of passess are required in bubble sort. A. N- K B. N-1 C. N D. All 4. How many number of comparisons are required in bubble sort. A. N- K B. N-1 C. N D. All 5. How we can perform sorting using 1. bubble sort 2. sort sort 3. deep sort 4.All of the above 6. 1. 2. 3. 4. ..................is a method of arranging data in ascending or descending order insertion Deletion updating Sorting 7. 1. 2. 3. 4. ..................is a method of applying logical arrangement of data insertion Deletion updating Sorting 8. 1. 2. 3. 4. ..................is a method of arranging data in ascending or descending order Quick sort Bubble sort Insertion sort All of the above Page 43 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Book: Lab experiment related theory available in following books: Book Name Author Web resources: 1. NPTEL lectures notes and their exercise. Page 44 of 77 Page No. NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE EXPERIMENT NO. 7 Date of conduction:- Date of submission:- Submitted by other members:1. 2. 3. 4. 5. 6. 7. 8. Group no:- Signature Name of Technical Assistant: Page 45 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Objective: - Write a program to implement recursion. Apparatus:platform. C or C++ compiler which either support windows Theory: Recursion is the process of repeating items in a self-similar way. Same applies in programming languages as well where if a programming allows you to call a function inside the same function that is called recursive call of the function as follows. void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); } The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go in infinite loop. Recursive function are very useful to solve many mathematical problems like to calculate factorial of a number, generating Fibonacci series, etc. Number Factorial Following is an example, which calculates factorial for a given number using a recursive function: #include <stdio.h> int factorial(unsigned int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); } int main() Page 46 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE { int i = 15; printf("Factorial of %d is %d\n", i, factorial(i)); return 0; } When the above code is compiled and executed, it produces the following result: Factorial of 15 is 2004310016 Fibonacci Series Following is another example, which generates Fibonacci series for a given number using a recursive function: #include <stdio.h> int fibonaci(int i) { if(i == 0) { return 0; } if(i == 1) { return 1; } return fibonaci(i-1) + fibonaci(i-2); } int { main() int i; for (i = 0; i < 10; i++) { printf("%d\t%n", fibonaci(i)); } return 0; } When the above code is compiled and executed, it produces the following result: 0 1 1 2 3 5 ALGORITHM : Page 47 of 77 8 13 21 34 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Recursion is the process of repeating items in a self-similar way. Same applies in programming languages as well where if a programming allows you to call a function inside the same function that is called recursive call of the function as follows. void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); } The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go in infinite loop. Recursive function are very useful to solve many mathematical problems like to calculate factorial of a number, generating Fibonacci series, etc. Procedure: #include<stdio.h> #include<conio.h> int fact(int); void main() { int n,result; clrscr(); printf("Enter the no:"); scanf("%d",&n); result=fact(n); printf ("\nThe factorial of %d is %d",n,result); getch (); Page 48 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE } int fact(int x) { if(x==1) { return(x); } else { return(x*fact(x-1)); } } Observation Table:-N/A Calculation:- N/A Results: - Page 49 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Conclusion:Precautions:Suggestions:- Lab Quiz :1. Recursive problems are implemented by 1.queues 2.stacks 3.linked lists 4.strings 2.Function calls it self is called 1. Recursion Page 50 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE 2. function 3. Argument 4. All of the above 3.Which is the example of recursion 1. 2. 3. 4. Factorial Fibbnosi series Argument All of the above 4. n a single function declaration, what is the maximum number of statements that may be recursive calls? A. 1 B. 2 C. n (where n is the argument) D. There is no fixed maximum 5. What is the maximum depth of recursive calls a function may make? A. 1 B. 2 C. n (where n is the argument) D. There is no fixed maximum 6. Consider the following function: void super_write_vertical(int number) // Postcondition: The digits of the number have been written, // stacked vertically. If number is negative, then a negative // sign appears on top. // Library facilities used: iostream.h, math.h { if (number < 0) { cout << '-' << endl; super_write_vertical(abs(number)); } else if (number < 10) cout << number << endl; else { super_write_vertical(number/10); Page 51 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE cout << number % 10 << endl; } } What values of number are directly handled by the stopping case? A. number < 0 B. number < 10 C. number >= 0 && number < 10 D. number > 10 7. Consider this function declaration: void quiz(int i) { if (i > 1) { quiz(i / 2); quiz(i / 2); } cout << "*"; } How many asterisks are printed by the function call quiz(5)? A. 3 B. 4 C. 7 D. 8 E. Some other number 8. In a real computer, what will happen if you make a recursive call without making the problem smaller? A. The operating system detects the infinite recursion because of the "repeated state" B. The program keeps running until you press Ctrl-C C. The results are nondeterministic D. The run-time stack overflows, halting the program 9. When the compiler compiles your program, how is a recursive call treated differently than a non-recursive function call? Page 52 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE A. Parameters are all treated as reference arguments B. Parameters are all treated as value arguments C. There is no duplication of local variables D. None of the above 10. When a function call is executed, which information is not saved in the activation record? A. Current depth of recursion. B. Formal parameters. C. Location where the function should return when done. D. Local variables. 11. Consider the following function: void test_a(int n) { cout << n << " "; if (n>0) test_a(n-2); } What is printed by the call test_a(4)? A. 0 2 4 B. 0 2 C. 2 4 D. 4 2 E. 4 2 0 Book: Lab experiment related theory available in following books: Book Name Author Page 53 of 77 Page No. NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Web resources: 1. NPTEL lectures notes and their exercise. Page 54 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE EXPERIMENT NO. 8 Date of conduction:- Date of submission:- Submitted by other members:1. 2. 3. 4. 5. 6. 7. 8. Group no:- Signature Name of Technical Assistant: Page 55 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Objective: - Write a program to implement insertion sort algorithm. Apparatus:platform. C or C++ compiler which either support windows Theory: ALGORITHM : Procedure: #include <stdio.h> #define MAX 20 void insertion(int arr[],int n) { int j,tmp,i; /*Insertion sort*/ for(i=1;i<n;i++) { tmp=arr[i]; /*tmp is to be inserted at proper place*/ for(j=i-1;i>=0 && tmp<arr[j];j--) { arr[j+1]=arr[j];} arr[j+1]=tmp; }} main() { int arr[MAX],i,n; printf("Enter the number of elements : "); scanf("%d",&n); for (i = 0; i < n; i++) {printf("Enter element %d : ",i+1); scanf("%d", &arr[i]);} Page 56 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE printf("Unsorted list is :\n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); insertion(arr,n); printf("Sorted list is :\n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); }/*End of main()*/ Observation Table:-N/A Calculation:- N/A Results: - Page 57 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Conclusion:Precautions:Suggestions:- Lab Quiz :1. 1. 2. 3. 4. How we can perform sorting using Insertion sort sort sort deep sort All of the above Page 58 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE 2. 1. 2. 3. 4. ..................is a method of arranging data in ascending or descending order insertion Deletion updating Sorting 3. 1. 2. 3. 4. ..................is a operation of applying logical arrangement of data insertion Deletion updating Sorting 4. 1. 2. 3. 4. ..................is a method of arranging data in ascending or descending order Quick sort Bubble sort Insertion sort All of the above 5.the running time of insertion sort is...... 1.O(n^2) 2. O(n) 3 O(log n) 4. O(n log n) 6. 1. 2. 3. 4. Which is not the type of complexity. Averege case Best case Worst Case All of the above 7. Which kind of complexity we can create for insertion sort. 1. Averege case 2. Best case 3. Worst Case 4. All of the above 8. Which is not the kind of complexity. 1. Time 2. Space 3. Worst 4. 1 & 2 9. Which is main kind of complexity calculated for any algorithm. 1. Time 2. Space 3. Worst 4. 1 & 2 10. How many number of element can be sort using. Page 59 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE 1. 2. 3. 4. 10 20 30 no limit Book: Lab experiment related theory available in following books: Book Name Author Web resources: 1. NPTEL lectures notes and their exercise. Page 60 of 77 Page No. NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE EXPERIMENT NO. 9 Date of conduction:- Date of submission:- Submitted by other members:1. 2. 3. 4. 5. 6. 7. 8. Group no:- Signature Name of Technical Assistant: Page 61 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Objective: - Write a program to implement Circular Queue. Apparatus:platform. C or C++ compiler which either support windows Theory: ALGORITHM : Procedure: #include<stdio.h> #include<conio.h> #define MAXSIZE 5 int cq[MAXSIZE]={0}; int front,rear; void main() { void add(int); void del(); void display(); int ch,i,num; front=-1; rear=-1; clrscr(); printf("\nprogram for circular queue demonstration through array"); while(1) Page 62 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE { printf("\n\n mian menu\n 1.insertion\n 2. deletion\n 3.display\n 4.exit"); printf("\n\n enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\n\n enter the queue element:"); scanf("%d",&num); add(num); break; case 2: del(); break; case 3: display(); break; case 4: exit(0); default: printf("\n\n invalid choice"); } } } void add(int item) { Page 63 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE if (front==(rear+1)%MAXSIZE) { printf("\n\n circular queue is overflow"); } else { if(front==-1) front=rear=0; else rear=(rear+1)%MAXSIZE; cq[rear]=item; printf("\n\n rear=%d front=%d",rear,front); }} void del() { int a; if(front==-1) { printf("\n\n circular queue is underflow"); } else { a=cq[front]; cq[front]=0; if(front==rear) Page 64 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE front=rear=-1; else front=(front+1)%MAXSIZE; printf("\n\n deleted element from queue is:%d",a); printf("\n\n rear=%d front=%d",rear,front); }} void display() { int i; for(i=0;i<MAXSIZE;i++) printf("%d/t",cq[i]); } Observation Table:-N/A Calculation:- N/A Results: - Page 65 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Page 66 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Conclusion:Precautions:Suggestions:Page 67 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Lab Quiz :1.Which data structure allows deleting data elements from front and inserting at rear? a. Stacks b. Queues c. Deques d. Binary search tree 2. Identify the data structure which allows deletions at both ends of the list but insertion at only one end. a. Input-restricted deque b. Output-restricted deque c. Priority queues d. None of above 3. Which of the following data structure is non-linear type? a. Strings b. Lists c. Stacks d. None of above 4. Which of the following data structure is linear type? a. Strings b. Lists c. Queues d. All of above 5. To represent hierarchical relationship between elements, which data structure is suitable? a. Deque b. Priority c. Tree d. All of above 6. Which of the following data structure is used in BFS algorithm? a. Strings b. Lists c.Stacks d. None of above 7. Which of the following data structure is can be used for implementing queue? a. Strings b. Graph c. Array d. None of above 8. Which of the following data structure is can be used for implementing queue? a. Strings Page 68 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE b. Stack c. linked List d. None of above 9. 1. 2. 3. 4. Circular queue can be implemented using Stack Linked List Graph All of the above 10. Circular queue can be implemented using 1. Stack 2. Array 3. Graph 4. All of the above Book: Lab experiment related theory available in following books: Book Name Author Web resources: 1. NPTEL lectures notes and their exercise. Page 69 of 77 Page No. NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE EXPERIMENT NO. 10 Date of conduction:- Date of submission:- Submitted by other members:1. 2. 3. 4. 5. 6. 7. 8. Group no:- Signature Name of Technical Assistant: Objective: -Write a program to delete an element from an array. Page 70 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Apparatus:platform. C or C++ compiler which either support windows Theory: A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array Linked List Stacks Queues Operations on linear Data Structures Traversal : Visit every part of the data structure Search : Traversal through the data structure for a given element Insertion : Adding new elements to the data structure Deletion : Removing an element from the data structure. Sorting : Rearranging the elements in some type of order( Increasing or Decreasing) Merging : Combining two similar data structures into one ALGORITHM : Assumptions: Let, the array A is unordered and we will delete all occurrence of the given value. Algorithm Name: DeleteAllArray(A, N, X) Input: A is any numeric array with N number of elements, we have to delete all the occurrences of the given element X in the given array A. Output:The array A without the element X. Page 71 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Step 1: IF ( N = 0 ) Return. Step 2: R = 0 Step 3: WHILE ( R != -1 ) Call LinearSearchArray(A, N, R, N-1, X, R) IF ( R = -1 ) Return [ X not found ] A[R] = A[N] N=N-1 EndWhile Step 4:Return. Procedure: #include <stdio.h> void main() { int vectorx[10]; int i, n, pos, element, found = 0; printf("Enter how many elements\n"); scanf("%d", &n); printf("Enter the elements\n"); for (i = 0; i < n; i++) { Page 72 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE scanf("%d", &vectorx[i]); } printf("Input array elements are\n"); for (i = 0; i < n; i++) { printf("%d\n", vectorx[i]); } printf("Enter the element to be deleted\n"); scanf("%d", &element); for (i = 0; i < n; i++) { if (vectorx[i] == element) { found = 1; pos = i; break; } } if (found == 1) { for (i = pos; i < n - 1; i++) { vectorx[i] = vectorx[i + 1]; Page 73 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE } printf("The resultant vector is \n"); for (i = 0; i < n - 1; i++) { printf("%d\n", vectorx[i]); } } else printf("Element %d is not found in the vector\n", element); } Observation Table:-N/A Calculation:- N/A Results: - Page 74 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Conclusion:Precautions:Suggestions:- Page 75 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE Lab Quiz :1. 1. 2. 3. 4. To remove an element from the array.................... insert delete update none 2. 1. 2. 3. 4. Element deletion is performed................at a time one two any number none of these 3. 1. 2. 3. 4. Array element can be deleted sequentialy randomly 1 and 2 none of these 4. 1. 2. 3. 4. Memory location of deleted element is taken in account by sequentially method randomly method 1 and 2 Garbage collection 5. 1. 2. 3. 4. Array is a sequential .............. data structure character Structure none of these 6.The data structure array can perform 1.insertion 2.deletion 3.updation 4.All of the above 7. 1. 2. 3. 4. Array is ...............data structure sequentialy randomly 1 and 2 none of these 8. 1. 2. 3. 4. Array element has following operations Insertion Deletion Updation All of the above Page 76 of 77 NAME OF LABORATORY: LAB SUBJECT CODE:CS-305 (DATA STRUCTURE) NAME OF DEPARTMENT:- CSE 9. Adding to sorted data in to array is 1. Merging 2. Deletion 3. Updation 4.All of the above 10. Recursive problems are implemented by 1. queues 2. stacks 3. linked lists 4. strings Book: Lab experiment related theory available in following books: Book Name Author Web resources: 1. NPTEL lectures notes and their exercise. Page 77 of 77 Page No.