Sunteți pe pagina 1din 7

DATA STRUCTURE QUESTIONS Submitted by: Mohit Singh Q 1: Write down the case when binary search cannot

be implemented? Ans.: searching is the process of finding an ITEM in the collection of elements. The search is said to be successful if it (ITEM) appears in the collection otherwise the search is unsuccessful. Types of searching algorithm: linear search / Sequential search Binary search Linear search is the process of finding the location of an ITEM in the collection by comparing it with all the elements sequentially from start to end. Major disadvantage of linear search is that it is inefficient for large no of elements as the no. of comparisons increases. Another efficient method of searching can be implemented when the data are arranged in some order (increasing /decreasing). And hence as the list of data are arranged in increasing order the ITEM to search is compared directly with the middle element instead of comparing with the very first element to last. If the search is successful then return the middle element location otherwise if ITEM is less than the middle, search ITEM in the first half of the list and if ITEM is greater than middle then search in the upper half of the list. Hence, the case where the binary search cannot be implemented is on unsorted list. Q 2: Draw a graph and represent it through its adjacency matrix. Ans.: A graph G consists of 2 things: A set of elements /nodes A set of edges such that each edge is identified with a unique pair of edges.

(undirected graph)

(Directed graph)

Adjacency matrix /path matrix is the sequential way to represent the graph. Suppose G be a graph with m nodes , then the adjacency matrix A=(aij) of G is m X m matrix such that.

1, if Vi is adjacent to Vj. Aij= 0, if otherwise. For undirected graph adjacency matrix A of graph G will be a symmetric matrix where aij=aji (for each i and j) Adjacency matrix : For undirected graph: A=

For directed graph: A=

Q 3: Describe the linked implementation of the stack. Ans.: stack is the collection of data items that can be accessed at only one end (from the top). It is called a last in first out (LIFO) data structure, as the element which is pushed at last is deleted first.

TOP

Linked list is a flexible data structure that provides a convenient way to store data. New nodes are added dynamically (i.e. when they are required). Linked list is a chain of node in which each node consists of data as well as a link to the next node/element. START

Linked implementation of stack: Stack can be implemented using linked list by adding or deleting the elements from the linked list only from the beginning. Hence START pointer will act as TOP pointer of stack and will point to the top element. Example: In the linked list implementation the new node is inserted before the first node using the following code. LINK [NEW] = START START = NEW

(Stack)

(Linked implementation) Q 4: Given an array with 2n+1 integer elements, n elements appears twice in arbitrary place in the array and a single integer appears only once some where inside. Find the lonely integer? Write program and flowchart. Ans.: //Program to find the lonely integer # include<stdio.h> #include<conio.h> void main() { int arr[5],count[10]={0,0,0,0,0,0,0,0,0,0}; printf(Enter array elements(please enter single digit integers)); for( int i=0; i<5; i++) { scanf(%d, &arr[i]); } for( int j=0; j<5; j++) { Switch(arr[i]) { Case 0: count[0]++; Break; Case 1: count[1]++; Break; Case 2: count[2]++; Break; Case 3: count[3]++; Break; Case 4: count[4]++; Break; Case 5: count[5]++; Break; Case 6: count[6]++;

Break; Case 7: count[7]++; Break; Case 8: count[8]++; Break; Case 9: count[9]++; Break; }} For( int i=0; i<10;i++) { If (count[i]%2!=0) { Printf(single element is %d, i); } } Getch(); } Q 5: Write a program with flowchart that sorts the elements of a 2D array. a) Row Wise b) Column Wise. Ans: # include<stdio.h> #include<conio.h> void main() { Int arr[3][3]; Printf(\nenter arr elements); For(int i=0;i<3;i++) { For(int j=0;j<3;j++) { Scanf(\n%d,&arr[i][j]); } } For(int k=0;i<3;k++) { For(int l=0;j<3-1;k++) { If (arr[k][l]>arr[k][l+1] { Int temp; Temp=arr[k][l]; Arr[k][l]=arr[k][l+1]; Arr[k][l+1]=temp; } } }

Printf(\n arr elements are); For( i=0;i<3;i++) { For(j=0;j<3;j++) { printf(\n%d,arr[i][j]); } }

30

TOP

20

10

NULL

START

10

NULL

START

20

10

NULL

START

30

20

10

NULL

S-ar putea să vă placă și