Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- DATA STRUCTURES by bca2010 67602 views
- Data structure and its types by Navtar Sidhu Brar 120190 views
- Lecture 1 data structures and algor... by Aakash deep Singhal 14993 views
- Data structures (introduction) by Arvind Devaraj 14676 views
- Introduction of data structure by eShikshak 8596 views
- Fundamentals of data structures by Niraj Agarwal 40882 views

9,456 views

Published on

ICT Training

No Downloads

Total views

9,456

On SlideShare

0

From Embeds

0

Number of Embeds

32

Shares

0

Downloads

1,243

Comments

0

Likes

19

No embeds

No notes for slide

- 1. Data Structures<br />
- 2. Which of the following is faster?<br />A binary search of an ordered set of elements in an array or a sequential search of the elements? <br />
- 3. The binary search is faster than the sequential search. <br />The complexity of binary search is 'log n' whereas the complexity of a sequential search is 'n'. <br />In a binary search, each time we proceed, we have to deal with only half of the elements of the array compared to the previous one. So the search is faster.<br />
- 4. List out the areas in which data structures are applied extensively? <br />
- 5.
- 6. Which data structure is used to perform recursion? <br />
- 7. Stack.<br />Because of its LIFO (Last In First Out) property it remembers its caller and hence knows where to return to when the function has to return. <br />Recursion makes use of system stack for storing the return addresses of the function calls. Every recursive function has its equivalent iterative (non-recursive) function. <br />Even when such equivalent iterative procedures are written, explicit stack is to be used. <br />
- 8. Tree can have duplicate values :<br /> True (or) False?<br />
- 9. True<br />Tree defines the structure of an acyclic graph but does not disallow duplicates.<br />
- 10. The size of a Tree is the number of nodes in the Tree : True (or) False?<br />
- 11. True<br />The size denotes the number of nodes, height denotes the longest path from leaf node to root node. <br />
- 12. Ram is told to sort a set of Data using Data structure. He has been told to use one of the following Methods <br />Insertion<br />Selection<br />Exchange<br />Linear<br />Now Ram says a Method from the above can not be used to sort. Which is the method?<br />
- 13. Linear<br />Using insertion we can perform insertion sort, using selection we can perform selection sort, and using exchange we can perform bubble sort.<br /> But no sorting method is possible using linear method; <br />Linear is a searching method<br />
- 14. Ashok is told to manipulate an Arithmetic Expression. What is the data structure he will use? <br />Linked List<br />Tree<br />Graph<br />Stack<br />
- 15. Stack<br />Stacks are used to evaluate the algebraic or arithmetic expressions using prefix or postfix notations<br />
- 16. There are 8,15,13,14 nodes in 4 different trees. Which of them could form a full binary tree? <br />8<br />15<br />13<br />14<br />
- 17. In general, there are 2n – 1 nodes in a full binary tree. <br />By the method of elimination: Full binary tree contains odd number of nodes. <br />So there cannot be a full binary tree with 8 or 14 nodes. With 13 nodes, you can form a complete binary tree but not a full binary tree.<br />Full and complete binary trees are different<br />All full binary trees are complete binary trees but not vice versa<br />
- 18. A<br />B<br />C<br />D<br />E<br />F<br />G<br />Full binary Tree:<br />A binary tree is a full binary tree if and only if:<br />Each non leaf node has exactly two child nodes<br />All leaf nodes have identical path length<br />It is called full since all possible node slots are occupied<br />
- 19. A<br />B<br />C<br />G<br />D<br />E<br />F<br />H<br />I<br />J<br />K<br />Complete binary Tree:<br />A complete binary tree (of height h) satisfies the following conditions:<br />Level 0 to h-1 represent a full binary tree of height h-1<br />One or more nodes in level h-1 may have 0, or 1 child nodes<br />
- 20. How many null branches are there in a binary tree with 20 nodes? <br />
- 21. 21 (null branches)<br />Let’s consider a tree with 5 nodes<br />So the total number of null nodes in a binary tree of n nodes is n+1<br />Null branches<br />
- 22. Write an algorithm to detect loop in a linked list.<br /> You are presented with a linked list, which may have a "loop" in it. That is, an element of the linked list may incorrectly point to a previously encountered element, which can cause an infinite loop when traversing the list. Devise an algorithm to detect whether a loop exists in a linked list. How does your answer change if you cannot change the structure of the list elements?<br />
- 23. One possible answer is to add a flag to each element of the list. <br />You could then traverse the list, starting at the head and tagging each element as you encounter it.<br /> If you ever encountered an element that was already tagged, you would know that you had already visited it <br /> and that there existed a loop in the linked list. <br />What if you are not allowed to alter the structure of the elements of the linked list? <br />
- 24. The following algorithm will find the loop:<br />Start with two pointers ptr1 and ptr2.<br />Set ptr1 and ptr2 to the head of the linked list.<br />Traverse the linked list with ptr1 moving twice as fast as ptr2 (for every two elements that ptr1 advances within the list, advance ptr2 by one element).<br />Stop when ptr1 reaches the end of the list, or when ptr1 = ptr2.<br />If ptr1 and ptr2 are ever equal, then there must be a loop in the linked list. If the linked list has no loops, ptr1 should reach the end of the linked list ahead of ptr2 <br />
- 25. The Operation that is not allowed in a binary search tree is<br />Location Change<br />Search<br />Deletion<br />Insertion<br />
- 26. Location Change<br />
- 27. Array is a type of ________________ data structure.<br />Non Homogenous<br />Non Linear<br />Homogenous but not Linear<br />Both Homogenous and Linear<br />
- 28. Both Homogenous and Linear.<br />
- 29. The address of a node in a data structure is called<br />Pointer<br />Referencer<br />Link<br />All the above<br />
- 30. All the above<br />
- 31. The minimum number of edges in a connected cycle graph on n vertices is ________<br />n<br />n + 1<br />n – 1<br />2n<br />
- 32. n<br />
- 33. The total number of passes required in a selection sort is<br />n + 1<br />n – 1<br />n<br />n * n<br />
- 34. n – 1<br />
- 35. The node that does not have any sub trees is called ___________<br />Null Node<br />Zero Node<br />Leaf Node<br />Empty Node<br />
- 36. Leaf Node<br />
- 37. Linked List is a<br />Static Data Structure<br />Primitive Data Structure<br />Dynamic Data Structure<br />None of the above<br />
- 38. Dynamic Data Structure<br />
- 39. Which data structure is needed to convert infix notation to postfix notation.<br />Tree<br />Linear Linked List<br />Stack<br />Queue<br />
- 40. Stack<br />
- 41. If every node u in Graph (G) is adjacent to every other node v in G, it is called as _____ graph.<br />Directed Graph<br />Complete Graph<br />Connected Graph<br />Multi Graph<br />
- 42. Complete Graph<br />
- 43. Bubble sort is an example of<br />Selection sort technique<br />Exchange sort technique<br />Quick sort technique<br />None of the options<br />
- 44. Exchange sort technique<br />
- 45. How do you chose the best algorithm among available algorithms to solve a problem<br />Based on space complexity<br />Based on programming requirements<br />Based on time complexity<br />All the above<br />
- 46. All the above<br />
- 47. Which of the following are called descendants?<br />All the leaf nodes<br />Parents, grandparents<br />Root node<br />Children, grandchildren<br />
- 48. Children, grandchildren<br />
- 49. Choose the limitation of an array from the below options.<br />Memory Management is very poor<br />Searching is slower<br />Insertion and deletion are costlier <br />Insertion and Deletion is not possible<br />
- 50. Insertion and deletion are costlier <br />(It involves shifting rest of the elements)<br />
- 51. Areas where stacks are popularly used are.<br />Subroutines<br />Expression Handling<br />Recursion<br />All the above<br />
- 52. All the above<br />
- 53. How would you implement queue using stack(s)?<br />
- 54. Use a temp stack<br />Data In into queue<br />Push the element into the original stack<br />Data Out from queue<br />Pop all the elements from stack into a temp stackpop out the first element from the temp stack<br />
- 55. Write a C program to compare two linked lists. <br />
- 56. int compare_linked_lists(struct node *q, struct node *r){<br /> static int flag;<br /> if((q==NULL ) && (r==NULL)){<br /> flag=1;<br /> }<br /> else{<br /> if(q==NULL || r==NULL){<br /> flag=0;<br /> }<br /> if(q->data!=r->data){<br /> flag=0;<br /> }<br /> else{<br />compare_linked_lists(q->link,r->link);<br /> }<br /> }<br /> return(flag);<br />} <br />
- 57. Write a C program to return the nth node from the end of a linked list. <br />
- 58. Suppose one needs to get to the 6th node from the end in the LL. First, just keep on incrementing the first pointer (ptr1) till the number of increments cross n (which is 6 in this case) <br />STEP 1 : 1(ptr1,ptr2) -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10<br />STEP 2 : 1(ptr2) -> 2 -> 3 -> 4 -> 5 -> 6(ptr1) -> 7 -> 8 -> 9 -> 10 <br />Now, start the second pointer (ptr2) and keep on incrementing it till the first pointer (ptr1) reaches the end of the LL. <br />STEP 3 : 1 -> 2 -> 3 -> 4(ptr2) -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 (ptr1) <br /> So here you have the 6th node from the end pointed to by ptr2! <br />
- 59. struct node {<br />int data;<br />struct node *next;<br />}mynode;<br />mynode * nthNode(mynode *head, int n /*pass 0 for last node*/) {<br />mynode *ptr1,*ptr2;<br />int count;<br />if(!head) {<br />return(NULL);<br />}<br />ptr1 = head;<br />ptr2 = head;<br />count = 0;<br />
- 60. while(count < n) {<br />count++;<br />if((ptr1=ptr1->next)==NULL) {<br />//Length of the linked list less than n. Error.<br />return(NULL);<br />} }<br />while((ptr1=ptr1->next)!=NULL) {<br />ptr2=ptr2->next;<br />}<br />return(ptr2);<br />}<br />
- 61. Write a C program to insert nodes into a linked list in a sorted fashion? <br />
- 62. The solution is to iterate down the list looking for the correct place to insert the new node. That could be the end of the list, or a point just before a node which is larger than the new node. <br />Let us assume the memory for the new node has already been allocated and a pointer to that memory is being passed to this function.<br />// Special case code for the head end<br />void linkedListInsertSorted(struct node** headReference, struct node* newNode)<br />{<br />// Special case for the head end<br />if (*headReference == NULL || (*headReference)->data >= newNode->data){<br />newNode->next = *headReference;<br />
- 63. *headReference = newNode;<br />}<br />else {<br />// Locate the node before which the insertion is to happen!<br />struct node* current = *headReference;<br />while (current->next!=NULL && current->next->data < newNode->data){<br />current = current->next;<br />}<br />newNode->next = current->next;<br />current->next = newNode;<br />}<br />} <br />
- 64. Write a C program to remove duplicates from a sorted linked list? <br />
- 65. As the linked list is sorted, we can start from the beginning of the list and compare adjacent nodes. <br />When adjacent nodes are the same, remove the second one. There's a tricky case where the node after the next node needs to be noted before the deletion.<br />// Remove duplicates from a sorted list<br />void RemoveDuplicates(struct node* head) {<br />struct node* current = head;<br /> if (current == NULL) return; // do nothing if the list is empty<br /> // Compare current node with next node<br /> while(current->next!=NULL)<br /> {<br />
- 66. if (current->data == current->next->data)<br /> {<br />struct node* nextNext = current->next->next;<br /> free(current->next);<br /> current->next = nextNext;<br /> }<br /> else<br /> {<br /> current = current->next; // only advance if no deletion<br /> }<br /> }<br />}<br />
- 67. Write a C program to find the depth or height of a tree. <br />
- 68. #define max(x,y) ((x)>(y)?(x):(y))<br />structBintree {<br /> int element;<br />structBintree *left;<br />structBintree *right;<br />};<br />typedefstructBintree* Tree;<br />int height(Tree T) {<br /> if(!T) <br /> return -1;<br /> else<br /> return (1 + max(height(T->left), height(T->right)))<br />}<br />
- 69. Write C code to determine if two trees are identical <br />
- 70. structBintree {<br /> int element;<br />structBintree *left;<br />structBintree *right;<br />};<br />typedefstructBintree* Tree;<br />int CheckIdentical( Tree T1, Tree T2 )<br />{<br /> if(!T1 && !T2) // If both tree are NULL then return true<br /> return 1;<br />
- 71. else if((!T1 && T2) || (T1 && !T2)) //If either of one is NULL, return false<br /> return 0;<br />else<br /> return ((T1->element == T2->element) && CheckIdentical(T1->left, T2-i>left)<br />&& CheckIdentical(T1->right, T2->right)); <br /> // if element of both tree are same and left and right tree is also same then both<br />trees are same <br />}<br />
- 72. Write a C code to create a copy of a Tree <br />
- 73. mynode *copy(mynode *root)<br />{<br />mynode *temp;<br /> if(root==NULL)return(NULL);<br /> temp = (mynode *) malloc(sizeof(mynode));<br /> temp->value = root->value;<br /> temp->left = copy(root->left);<br /> temp->right = copy(root->right);<br /> return(temp);<br />}<br />
- 74. Which of the following are called siblings<br />Children of the same parent<br />All nodes in the given path upto leaf node<br />All nodes in a sub tree<br />Children, Grand Children<br />
- 75. Children of the same parent<br />
- 76. Linked List can grow and shrink in size dynamically at __________ .<br />Runtime<br />Compile time<br />
- 77. Runtime <br />
- 78. The postfix of A+(B*C) is <br />ABC*+<br />AB+C*<br />ABC+*<br />+A*BC<br />
- 79. ABC*+<br />
- 80. Data structure using sequential allocation is called <br />Linear Data Structure<br />Non-Linear Data Structure<br />Non-primitive Data Structure<br />Sequence Data Structure<br />
- 81. Linear Data Structure<br />
- 82. A linear list in which elements can be added or removed at either end but not in the middle is known as<br />Tree<br />Queue<br />Dequeue<br />Stack<br />
- 83. Dequeue<br />
- 84. The average number of key comparisons done in a successful sequential search in list of length n is <br />n+1/2<br />n-1/2<br />n/2<br />log n<br />
- 85. n+1/2<br />
- 86. A full binary tree with n leaves contains __________<br />nlog2n nodes<br />2^n nodes<br />(2n-1) nodes<br />n nodes<br />
- 87. (2n-1) nodes<br />
- 88. If a node has positive outdegree and zero indegree, it is called a __________.<br />Source<br />Sink<br />outdegreenode<br />indegreenode<br />
- 89. Source<br />
- 90. The postfix notation for ((A+B)^C-(D-E)^(F+G)) is <br />AB + C*DE—FG+^<br />^-*+ABC –DE + FG<br />^+AB*C—DE^+FG<br />ABC + CDE *-- FG +^<br />
- 91. AB + C*DE—FG+^ <br />
- 92. If you are using C language to implement the heterogeneous linked list, what pointer type will you use? <br />
- 93. The heterogeneous linked list contains different data types in its nodes and we need a pointer to connect them.<br /> It is not possible to use ordinary pointers for this. <br />So we use void pointer. <br />Void pointer is capable of storing pointer to anytype of data (eg., integer or character) as it is a generic pointer type. <br />
- 94. What is heap sort? <br />
- 95. A Heap is an almost complete binary tree. In this tree, if the maximum level is i, then, upto the (i-1)th level should be complete. At level i, the number of nodes can be less than or equal to 2^i. If the number of nodes is less than 2^i, then the nodes in that level should be completely filled, only from left to right<br />The property of an ascending heap is that, the root is the lowest and given any other node i, that node should be less than its left child and its right child. In a descending heap, the root should be the highest and given any other node i, that node should be greater than its left child and right child.<br />
- 96. To sort the elements, one should create the heap first. Once the heap is created, the root has the highest value. Now we need to sort the elements in ascending order. The root can not be exchanged with the nth element so that the item in the nth position is sorted. Now, sort the remaining (n-1) elements. This can be achieved by reconstructing the heap for (n-1) elements. <br />
- 97. heapsort() {<br /> n = array(); // Convert the tree into an array.<br />makeheap(n); // Construct the initial heap.<br /> for(i=n; i>=2; i--) {<br /> swap(s[1],s[i]);<br />heapsize--;<br />keepheap(i);<br /> }<br />}<br />makeheap(n) {<br />heapsize=n;<br /> for(i=n/2; i>=1; i--)<br />keepheap(i);<br />}<br />keepheap(i) {<br /> l = 2*i;<br /> r = 2*i + 1;<br /> p = s[l];<br /> q = s[r];<br /> t = s[i];<br />
- 98. if(l<=heapsize && p->value > t->value)<br />largest = l;<br /> else<br /> largest = i;<br /> m = s[largest];<br /> if(r<=heapsize && q->value > m->value)<br /> largest = r;<br /> if(largest != i) {<br /> swap(s[i], s[largest]);<br />keepheap(largest);<br /> }<br />}<br />
- 99. Implement the bubble sort algorithm. How can it be improved? Write the code for selection sort, quick sort, insertion sort. <br />
- 100. Bubble sort algorithm<br />void bubble_sort(int a[], int n)<br />{<br /> int i, j, temp;<br /> for(j = 1; j < n; j++)<br /> {<br /> for(i = 0; i < (n - j); i++)<br /> {<br /> if(a[i] >= a[i + 1])<br /> {<br /> //Swap a[i], a[i+1]<br /> }<br /> }<br /> }<br />}<br />
- 101. To improvise this basic algorithm, keep track of whether a particular pass results in any swap or not. <br /> If not, you can break out without wasting more cycles. <br />void bubble_sort(int a[], int n)<br />{<br /> int i, j, temp;<br /> int flag;<br /> for(j = 1; j < n; j++) {<br />flag = 0;<br /> for(i = 0; i < (n - j); i++) {<br /> if(a[i] >= a[i + 1])<br /> {<br />//Swap a[i], a[i+1]<br /> flag = 1;<br />}<br /> }<br /> if(flag==0)break;<br /> }<br />}<br />
- 102. Selection Sort Algorithm<br />void selection_sort(int a[], int n) {<br /> int i, j, small, pos, temp;<br /> for(i = 0; i < (n - 1); i++)<br /> {<br /> small = a[i];<br /> pos = i;<br /> for(j = i + 1; j < n; j++)<br /> {<br /> if(a[j] < small)<br /> {<br /> small = a[j];<br /> pos = j;<br /> }<br /> }<br /> temp = a[pos];<br /> a[pos] = a[i];<br /> a[i] = temp;<br /> }<br />}<br />
- 103. Quick Sort Algorithm<br />int partition(int a[], int low, int high)<br />{<br /> int i, j, temp, key;<br /> key = a[low];<br /> i = low + 1;<br /> j = high;<br /> while(1) {<br /> while(i < high && key >= a[i])i++;<br /> while(key < a[j])j--;<br /> if(i < j) {<br /> temp = a[i];<br /> a[i] = a[j];<br /> a[j] = temp;<br /> }<br /> else {<br /> temp = a[low];<br /> a[low] = a[j];<br /> a[j] = temp;<br /> return(j); <br /> }<br /> }<br />}<br />
- 104. void quicksort(int a[], int low, int high) {<br /> int j;<br /> if(low < high) {<br /> j = partition(a, low, high);<br />quicksort(a, low, j - 1);<br />quicksort(a, j + 1, high);<br /> }<br />}<br />int main() { <br /> // Populate the array a<br />quicksort(a, 0, n - 1);<br />}<br />
- 105. Insertion Sort Algorithm<br />void insertion_sort(int a[], int n)<br />{<br /> int i, j, item;<br /> for(i = 0; i < n; i++)<br /> {<br /> item = a[i];<br /> j = i - 1;<br /> while(j >=0 && item < a[j])<br /> {<br /> a[j + 1] = a[j];<br /> j--;<br /> }<br /> a[j + 1] = item;<br /> }<br />}<br />

No public clipboards found for this slide

Be the first to comment