Professional Documents
Culture Documents
4. Define ADT.
The programmer may have to create & implement his/her own data type to suit the
program requirements, such a data type is called Abstract Data Type.
The programmer has to decide what to store, what operations to perform & what
should be the rules to be followed.
5. Define D.S.
A data structure is a set of
a) Domain D i.e. a designed domain d ∈ D.
b) a set of functions F and
c) a set of Axioms A.
6. Give two examples of each of linear and non linear Data structures.
Linear Data Structures:
Examples:
1. Array, 2. Stack, 3. Queue4. Linked List
Nonlinear Data Structures:
Examples:
1. Tree, 2. Graph.
7. “Choice of good data structures improves program efficiency” Comment.
Data structure is a platform to store data in organized and efficient manner. It
improves efficiency so it is choice of programmer to select appropriate data structure
and use it according to convince.
8. List some common operations that can be performed on D. S
1. Create, 2. Insert, 3. Delete, 4. Display, 5. Sort, 6. Search
9. List advantages of D.S.
1. Localization of change.
2. Encapsulation.
3. Flexibility.
4. Representation independence.
5. Modularity.
6. Extensibility.
10. Define linear and nonlinear D.S.
Linear Data Structure: The Data Structure in which elements are arranged in a line
and form a sequence or a list is called Linear Data Structure.
Nonlinear Data Structure: The Data Structure in which elements have one-to-many
relationship between them and do not form a specific sequence is called Nonlinear
Data Structure.
11. List components of space complexity.
1. Fixed Part, 2. Variable part
12. Define Ω notation.
The function f(n) = Ω(g(n)) iff there exists positive constants c and n0 such that
f(n) ≥ c * g(n) for all n, where n>=n0
13. Which notation is used to denote lower bound?
Ω notation is used to denote lower bounds.
14. Which notation is used to denote upper bound?
O notation is used to denote upper bounds.
15. Define time complexity.
Time Complexity is the the amount of time the program requires to execute.
16. Which notation is used to denote Lower & upper bound?
θ notation is used to denote both lower and upper bounds.
17. What is ADT? What are the advantages of ADT?
The programmer may have to create & implement his/her own data type to suit the
program requirements, such a data type is called Abstract Data Type.
The programmer has to decide what to store, what operations to perform &
what should be the rules to be followed.
18. Arrange the following time complexities in decreasing order.
O(n2) , O(log2n), O(n), O(n3), O(n2), O(nlog2n)
Answers:
1-T 2-F 3-T 4-F 5-F
IV. Short Answers:
1.Define Algorithm? Write essential properties of an algorithm.
An algorithm is a finite set of instructions that, if followed, accomplishes a particular task.
Essential properties of an algorithm are:
Input 2. Output 3. Definiteness 4. Finiteness 5. Effectiveness
1. Which design strategy does quick sort follow?
Divide and Conquer.
2. Justify the following statement “Sorted data makes searching process efficient”.
In sorted data if we want to find out any value which is exact the value in list of data,
then it directly return that value. If the value we are searching is smaller than the
value at present we are accessing, then we need to stop there which reduces our
searches.
3. Why array is called as built in data structures.
Array is called built in data structure because it also helps in organizing and
efficiently using a large number of data of a particular type.
4. Which would be best choice of pivot element in Bubble sort?
Best case: Ο(n2)
Worst case: Ο(n2)
5. What is best case and worst case for Linear search?
Best case: Ο(1)
Worst case: Ο(n)
6. Give Any One example of unstable sorting method?
Ans: Quick sort
7. What is best case and worst case for insertion sort?
Best case: Ο(n)
Worst case: Ο(n)
8. What do you mean by external sorting?
The Sorting is done on data which is stored in main memory is called internal sorting.
9. What do you mean by external sorting?
The Sorting is done on data stored in auxiliary storage device is called external
sorting.
10. What is best case and worst case for Quick sort?
Best case: Ο(log2n)
Worst case: Ο(n2)
11. What is best case and worst case for merge sort?
Best case: Ο(log2n)
Worst case: Ο(log2n)
12. What is best case and worst case for sequential search?
Best case: Ο(1)
Worst case: Ο(n)
13. What is best case and worst case for binary search?
Best case: Ο(log2n)
Worst case: Ο(log2n)
14. Give example of method which is not inplace sort.
Merge Sort
V. Long Questions:
1. Calculate address of element A[2][1] in a character array A[3][4] the row major
representation (assume base address = 100).
Address of a[i][j] = Base address + (i * c + j) * sizeof(element size)
Total No. of columns (c) = 4 which is given in A[3][4]
i=2, j=1 & sizeof(element size) = 1 as char data type
Address of a[2][1] = 100 + (2 * 4 + 1 ) * 1
= 100 + (9) * 1
= 100+9
= 109
2. Calculate address of element A[2][2] in a float array A[3][4] the column major
representation (assume base address = 100).
Address of a[i][j] = Base address + (j * r + i) * sizeof(element size)
Total No. of columns (r) = 4 which is given in A[3][4]
i=2, j=2 & sizeof(element size) = 4 as char data type
Address of a[2][2] = 100 + (2 * 4 + 2 ) * 1
= 100 + (10) * 1
= 100+10
= 110
3. Calculate address of element A[2][3] in a int array A[3][4] the column major
representation (assume base address = 500).
Address Calculation : int a[3][4];
Address of a[i][j] = Base address + (j * r + i) *
sizeof(element size)
Total No. of rows (r) = 3
Address of a[2][3] = 500 + (3 * 3 + 2 ) * 2
= 500 + (11) * 2
= 500 + 22 = 522
4. Write formula for calculating address of any element in row major and column major
representation of two-dimensional array.
Row Major Representation:
Address of a[i][j] = Base address + (i * c + j) * sizeof(element size)
Column Major Representation:
Address of a[i][j] = Base address + (j * r + i) * sizeof(element size)
5. Write formula for calculating address of any element in multi-dimensional array.
To find address of a[i1][i2][i3]…[in] if array a[s1][s2][s3]..[sn] is given, then
Address of a[i1][i2][i3]…[in]=
Base Address+[i1*s2*s3*.....sn+i2*s3*…sn+i3*s48…sn+in-1*sn+in]*element-size
6. Calculate address of element A[6][5] [4][3]in a int array A[7][6][5][4] (assume base
address = 500).
Address of a[i1][i2][i3]…[in]=
Base Address+[i1*s2*s3*.....sn+i2*s3*…sn+i3*s48…sn+in-1*sn+in]*element-size
So, Address of A[6][5][4][3]= Base Address + [i1*s2*s3*s4+i2*s3*s4+i3*s4+i4]*2
= 500 + [6*6*5*4+5*5*4+4*4+3]*2
= 500 + [720+100+16+3]*2
= 500 +[839]*2
= 500 + 1678
= 2178
7. Write function for merge sort, bubble sort and binary search.
Merge Sort:
void mergesort(int a[], int low, int high)
{
int mid;
if(low<high)
{
mid = (low+high)/2;
mergesort(a, low,mid);
mergesort(a, mid+1,high);
merge(a,low,mid,high);
}
}
void merge(int a[], int low, int mid,int high)
{
int I, j, k, b[10];
i=low;
j=mid+1;
k=0;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j])
{
b[k]=a[i];
i++;
k++;
}
while(j<=high)
{
b[k]=a[j];
k++;
j++;
}
for(i=low,k=0;i<=high;i++,k++)
a[i]=b[k];
}
Bubble Sort:
void bubble(int x[], int n)
{
int i,temp,pass;
cc=0;
sc = 0;
for(pass = 1; pass<n; pass++)
{
for(i=0; i<n-pass;i++)
{
cc++;
If(x[i]≥x[i+1])
{
sc++;
temp = x[i];
x[i]=x[i+1];
x[i+1] = temp;
}
}
}
}
Binary Search:
int recbinsearch(int a[ ], int top, int bottom, int key)
{
int mid;
if(top<=bottom)
{
mid = (top +bottom)/2;
If(a[mid]==key)
return mid;
else if(key < a[mid])
return recbinsearch(a, top, mid-1, key);
else
return recbinsearch(a, mid+1, bottom, key);
}
return -1;
}
Bubble Sort:
void bubble(int x[], int n)
{
int i,temp,pass;
cc=0;
sc = 0;
for(pass = 1; pass<n; pass++)
{
for(i=0; i<n-pass;i++)
{
cc++;
If(x[i]≥x[i+1])
{
sc++;
temp = x[i];
x[i]=x[i+1];
x[i+1] = temp;
}
}
}
}
8. Sort the following data in ascending order using bubble sort.
Neena, Ajay, Piyush, Bharti, Leena, Atul
Pass 0:
Ajay, Neena, Piyush, Bharti, Leena, Atul
Ajay, Neena, Piyush, Bharti, Leena, Atul
Ajay, Neena, Bharti, Piyush, Leena, Atul
Ajay, Neena, Bharti, Leena,Piyush, Atul
Ajay, Neena, Bharti, Leena, Atul, Piyush
Pass 1:
Ajay, Neena, Bharti, Leena, Atul, Piyush
Ajay, Neena, Bharti, Leena, Atul, Piyush
Ajay, Bharti, Neena,Leena, Atul, Piyush
Ajay, Bharti, Leena, Neena, Atul, Piyush
Ajay, Bharti, Leena, Atul, Neena, Piyush
Pass 3:
Ajay, Bharti, Leena, Atul, Neena, Piyush
Ajay, Bharti, Leena, Atul, Neena, Piyush
Ajay, Bharti, Atul, Leena, Neena, Piyush
Pass 4:
Ajay, Bharti, Atul, Leena, Neena, Piyush
Ajay, Atul, Bharti, Leena, Neena, Piyush
9. Sort the following data in ascending order using insertion sort.
10. 48, 17, 25, 89, 55, 8
Sorted List Unsorted List
Empty 48, 17, 25, 89, 55, 8
48 17, 25, 89, 55, 8
17, 48 25, 89, 55,8
17,25, 48 89, 55, 8
17, 25, 48, 89 55, 8
17, 25, 48, 55,89 8
8, 17, 25, 48, 55, 89 Empty
11. Sort the following data in ascending order using insertion sort.
27, 9, 7, 39, 35, 2
Sorted List Unsorted List
Empty 27, 9, 7, 39, 35, 2
27, 9,7, 39, 35, 2
9, 27 7, 39, 35, 2
7, 9, 27 39, 35,2
7, 9, 27, 39 35, 2
7, 9, 27, 35 , 39 2
2, 7, 9, 27, 35, 39 Empty
12. Sort the following data in ascending order using merge sort.
14, 2, 99, 34, 23, 67, 100, 107
[14, 2, 99, 34] [23, 67, 100, 107]
[14, 2] [99, 34] [23, 67] [100, 107]
[14] [2] [99] [34] [23] [67] [100] [107]
[2, 14] [34, 99] [23, 67] [100, 107]
[2, 14, 34, 99] [ 23, 67, 100, 107]
[2, 14, 23, 34, 67, 99, 100, 107]
2, 14, 23, 34, 67, 99, 100, 107
25, 45,55, 20, 35, 40, 12, 10
13. Sort the following data in ascending order using quick sort.
89, 14, 10, 25, 99, 75, 12, 77, 97
[14, 10, 25, 75, 12, 77] 89 [ 99,97]
[10, 12] 14 [25, 75, 77] 89 [97] 99
10 [12] 14, 25 [75, 77] 89 97 99
10, 12, 14, 25, 75 [77] 89, 97, 99
10, 12, 14, 25, 75, 77, 89, 97, 99
10, 12, 14, 25, 75, 77, 89, 97, 99
14. Sort the following data in ascending order using merge sort and quick sort.
25, 45,55, 20, 35, 40, 12, 10 and 20, 54, 48, 37, 12, 92, 86, 07
15. Write a note on sequential Search.
int linsearch(int a[], int n, int key)
{
int i,k;
for(i=0;i<n;i++)
{
if(a[i]==key)
return i;
else
k= -1;
}
return k;
}
16. Write recursive and non recursive function for Binary Search.
Non Recursive :
int binsearch(int a[], int n, int key)
{
int mid, top, bottom;
top =0;
bottom = n-1;
while(top<bottom)
{
mid = (top+bottom)/2;
if(a[mid]==key)
return mid;
else
if(key <a[mid])
bottom =mid – 1;
else
top = mid +1;
}
return -1;
}
int recbinsearch(int a[ ], int top, int bottom, int key)
{
int mid;
if(top<=bottom)
{
mid = (top +bottom)/2;
If(a[mid]==key)
return mid;
else if(key < a[mid])
return recbinsearch(a, top, mid-1, key);
else
return recbinsearch(a, mid+1, bottom, key);
}
return -1;
}
17. Write function for Quick Sort.
void quicksort(int a[], int lb, int ub)
{
int j;
if(lb<ub)
{
j=partition(a,lb,ub);
quicksort(a,lb,j-1);
quicksort(a, j+1, ub);
}
}
int partition(int a[], int lb, int ub)
{
int i,j,pivot,temp;
i=lb+1;
j=ub;
pivot=a[lb];
do
{
while((a[i]<pivot)&&(i<=ub))
i++;
while((a[j]>pivot)&&j>lb))
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[lb]=a[j];
a[j]=pivot;
return j;
}
Chapter 3
LINKED LIST
I. Multiple Choice Questions:
1. A doubly linked list has .......... pointers with each node.
a. 0 b.1 c.2 d.3
2. In a linked list, insertion can be done as.........
a. Beginning b. end c. middle d. all of the above
3. The pointer that points to the first node in the list is........
a. HEAD b. AVAIL c. TOP d. REAR
4. What is the time complexity to count the number of elements in the linked list?
a. O(1) b. O(n) c. O(logn) d. None of the mentioned
5. Linked list is considered as an example of ___________ type of memory allocation.
a. Dynamic b. Static c. Compile time d. None of the mentioned
ANSWERS
1.b 2.d 3.a 4.b 5.a
II. Fill in the Blanks
1. The data structures you will use if you want to go to first record from the last and vice
versa______________
2. _________linked list is also called as two way linked list.
3. _________function is used to dynamically allocate memory in C.
4. __________linked list is used to represent polynomial equation with multiple variables.
2) Same happens when we share a printer with many computers. Compiling an HLL code.
3)Using the download manager, for multiple files also uses a queue for ordering the files.
Long Questions:
1. Write a function push and pop using array.
Push:
void push(struct stack *ps, int x)
{
++ps->top;
ps->data[ps->top]=x;
}
Pop:
int pop(struct stack *ps)
{
int x;
x=ps->data[ps->top];
ps->top--;
return x;
}
2. Write a function push and pop using S.L.L.
void dspush() //dynamic stack push
{
nw=(struct node*)malloc(sizeof(struct node));
printf(“\n Enter the value to be pushed\n”);
scanf(“%d”,&nw->data);
nw->next=NULL;
if(top==NULL)
{
top=nw;
}
else
{
nw->next=top;
top=nw;
}
void dspop() //dynamic stack pop
{
int x;
temp=top;
x=temp->data;
printf(“\n The popped element is %d”,x);
top=top->next;
free(temp);
}
3. Write a function to check whether a expression is fully parenthesized?
void wellparen(char infix[])
{
char c;
int k, t=1;
struct stack sp;
ssinit(&sp);
for(k=0;infix[k]!= ‘\0’;k++)
{
If(infix[k]== ‘(’||infix[k]== ‘{’||infix[k]== ‘[’)
{
sspush(&sp,infix[k]);
}
else if(infix[k]== ‘)’)
{
c=sspop(&sp);
if(c!= ‘(’)
t=0;
}
else if(infix[k]== ‘}’)
{
c=sspop(&sp);
if(c!= ‘{’)
t=0;
}
else if(infix[k]== ‘]’)
{
c=sspop(&sp);
if(c!= ‘[’)
t=0;
}
else
continue;
if((!ssisempty(&s))&&(t!=0))
printf(“\n The expression is not well-parenthesized expression”);
else
printf(“\n The expression is well-parenthesized expression”);
}
4. Give the output of the following sample code:
initstack(s);
push(s, 9);
push(s, 4);
i=pop(s);
while(i > 0)
{
Push(s, i*i);
while(i > 0)
{
Push(s, i*i);
i--;
}
while(!stackempty(s))
prinf(“%d\n”,pop(s));
Output: 1, 4, 9, 16, 9
1. Write a function to evaluate postfix expression.
void postfixeval(char postfix[])
{
struct stack sp;
ssinit(&sp);
int k,val,od1,od2,result;
for(k=0;postfix[k]!= ‘\0’;k++)
{
if(isalpha(postfix[k]))
{
printf(“\n Enter value of %c”, postfix[k]);
scanf(“%d”,&val);
sspush(&sp,val);
}
else
{
od2=sspop(&sp);
od1=sspop(&sp);
if(postfix[k]== ‘+’)
{
val=od1+od2;
sspush(&sp,val);
}
else if(postfix[k]== ‘-’)
{
val=od1-od2;
sspush(&sp,val);
}
else if(postfix[k]== ‘*’)
{
val=od1*od2;
sspush(&sp,val);
}
else if(postfix[k]== ‘/’)
{
val==od1/od2;
sspush(&sp,val);
}
else
{
val=od1^od2;
sspush(&sp,val);
}
}
result=sspop(&sp);
printf(“\nThe result is %d”,result);
}
2. Write a function to reverse stack.
void reverse()
{
struct stack s;
initstack(&s);
char a[30],ch;
int i;
printf(“Enter the string”);
gets(a);
printf(“\n The given string is \n”);
puts(a);
printf(“\The reverse string is\n”);
for(i=0;a[i]!=‘\0’;i++)
{
if(a[i]==‘ ‘)
{
while(!isempty(&s))
{
ch=pop(&s));
printf(“%c”,ch);
}
}
else
{
push(&s,a[i]);
}
printf(“ “);
}
while(!isempty(&s))
{
ch=pop(&s);
printf(“%c”,ch);
}
}
ii)Second Method
3. /* C Program To Reverse String using Stack */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20
int top = -1;
char stack[MAX];
char pop();
void push(char);
int main()
{
char str[20];
unsigned int i;
printf("Enter the string : " );
gets(str); /*Push characters of the string str on the stack */
for(i=0;i<strlen(str);i++)
push(str[i]); /*Pop characters from the stack and store in string str */
for(i=0;i<strlen(str);i++)
str[i]=pop(); printf("\nReversed string is : ");
puts(str);
return 0;
}/*End of main()*/
void push(char item)
{ if(top == (MAX-1))
{ printf("\nStack Overflow\n");
return;
}
stack[++top] =item;
}/*End of push()*/
char pop() { if(top == -1)
{
printf("\nStack Underflow\n"); exit(1);
} return stack[top--];
}/*End of pop()*/
V. Long Questions :
1. Write a function to ADD and DELETE functions to implement dynamic
representation of linear queue.
void insert(int item)
{
Node *newnode;
newnode=(Node*)malloc(sizeof(Node));
newnode ->data=item;
newnode ->next=NULL;
if(front==NULL)
front=rear= newnode;
else
{
rear->next= newnode;
rear= newnode;
}}
int del()
{
int item;
Node *temp=front;
item=front->data;
front=front->next;
free(temp);
if(front==NULL)
rear=NULL;
return(item);
}
2. Write a function to ADD and DELETE functions to implement static representation
of linear queue.
int isfull(Queue *q)
{
if(q->rear==MAX-1)
return(1);
else
return(0);
}
void enqueue(Queue *q,int item)
{
q->rear++;
q->que[q->rear]=item;
}
int isempty(Queue *q)
{
if(q->front==q->rear)
return(1);
else
retrun(0);
}
int dequeue(Queue *q)
{
int item;
q->front++;
item=q->que[q->front];
return(item);
}
rear->next=newnode;
rear=newnode;
rear->next=front;
}
}