Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Data Structure & Algorithms Code:- TIU-UCS-T201

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);
}

4. Explain linked list representation of polynomials. Write algorithms to add


and subtract two polynomials using linked list.
5. Assume given two stacks, both with operations push and pop in O(1). Give
in this setting an efficient implementation of a queue with operations
enqueue ( ) and dequeue( ).
Analyze the running time of the queue operations in terms of O.
6. Assume given two queues, both with operations enqueue( ) and dequeue( )
in O(1). Give in this setting an implementation of a stack with operations
push and pop.
Analyze the running time of the stack operations in terms of O.
7. Give a non-recursive procedure in O(n) that reverses a singly linked list of n
elements. The procedure should use no more than constant storage space
beyond the space needed for the list itself. (Is your procedure also in (n)?)

You might also like