Professional Documents
Culture Documents
DSA Written Assignment
DSA Written Assignment
Written Assignment:
(to be submitted by 30/11/2017)
1. A palindrome is a string that reads the same from forwards and backwards.
Using only a fixed number of stacks and queues, the stack and queue ADT
functions, and a fixed number of int and char variables, write an algorithm to
determine if a string is a palindrome. Assume that the string is read from an
input stream one character at a time. The algorithm should output TRUE or
FALSE as appropriate.
2. Design an array based data structure for two stacks called a DualStack The
two stacks should share the same array in an efficient manner. If there are
MaxSize entries in the array then the IsFull function should only return true
is all the entries in the array are occupied. Your operations should all be
constant time.
i. Implement Push(S: DualStack pointer, i: integer, p : blob pointer) that
pushes the blob pointed to by p onto the i-th stack in S (i = 0 or 1).
Similarly, implement Pop, IsEmpty, IsFull.
ii. Explain why such a nice data structure would not be possible for 3
stacks.
3. Let x[ ] be an array with size n and key be an integer element.
Explain what the following two algorithms will do. Also give a comparison
of their running time.
Algorithm 1
int doBS (int x[], int n, int key) //x[ ] is a sorted array here
{
int flag = 0; low = 0;
int mid, hi = n-1;
while(low < hi)
{
mid = (low + hi )/2; /* mid is the center position of the
list */
if( x[mid]==key) /* x[mid] is the center element*/
{
flag = 1;
break;
}
if(x[mid] > key)
hi = mid 1;
else
low = mid + 1;
}
if(flag==1)
return (1);
else
return(-1);
}
Algorithm 2
int doLS(int x[],int n, int key)
{
int i,flag = 0;
for(i=0;i < n ; i++)
{
if(x[i]==key)
{
flag=1;
break;
}
}
if(flag==0)
return(-1);
else
return(1);
}