// Please do the following.
// 1. void push(Object o);-- add element to the top
// 2. Object pop(); -- remove element from the top
// 3. boolean isEmpty(); -- true/false if stack is empty
// Queue q;
// void enqueue(Object o); -- add to the end of the queue
// Object dequeue(); -- remove from the front of the queue
// boolean isEmpty(); -- true/false if the queue is empty
queue 3 2 1 3 queue 2 1 size-1
stact pop 3
class stack{
queue firstq = new queue();
queue secq = new queue();
public void push(Object o)
{
firstq.enqueue(o);
}
public Object dequeue(){
if(firstq.isEmpty())
throw /*new */RuntimeException("size == 0");
object result = null;
while(1)
{
Object o = firstq.pop();
if(firstq.isEmpty())
{ result = o;
break;
}
else{
secq.enqueue(o);
}
}
// optimize below
queue buf = firstq;
firstq = secq;
secq = firstq;
// till here
return result;
}
public boolean isEmpty(){
return firstq.isEmpty();
}
}
// 2. "Hi, good morning"
// -> "morning good Hi,"
gninorom doog ,ih
public void revserAllwords(char[] str, int i , int j)
{
while(i<j)
{
char buf = str[j];
str[j] = str[i];
str[i] = buf;
i++;
j--;
}
return;
}
reverswor
public void revserfuction(char[] str)
{
int wordStart = 0;
int wordEnd = 0;
revserAllwords(str,0,str.length-1);
for(int i =0; i < str.length ; i++){
if(char[i]==' '){
wordEnd = i;
revserAllwords(str,wordStart,wordEnd);
wordStart = wordEnd+1;
}else{
continue;
}
}
//duel with last word
revserAllowords(str,wordStart,str.length-1)
return;
}
// option 3
// static memory
int bufferForDecompression[256*1024]; // ??
static memory
// can't take longer than 15 ms
void realTimeFoo( struct message ) {
int x = 1024;
// Option 1
// Option 2
int profileTimeStart = GetTime();
// the heap is fragmented....
int threshold = max ;// max < 10ms buf[8000] 10ms
if(x> 8000/256){
return new RuntimeExcepiton("reset x");
}
int[] bufferForDecompression = new bufferForDecompression( 256 * x ); // on the heap but how long does this take ???
int profileTimeStop = GetTime();
int delta = profileTimeStop - profileTimeStart; // measured 100x and it took 10 ms which is less than 16 ms
bool isOk = false;
isOk = decompress( message, bufferForDecompression );
return isOk;
stack store some:
return addreess
}
stack:
Given an positive integer array of size N, and you are given the standard rand function. Ex. you can choose each number with a probability 1/N
Can you write a custom rand function such that you can choose each number in the array so that it is proportional to the value of the integer.
Ex: {1, 2, 3} . If probability of choosing 1 is x, probability of choosing 2 should be 2x.
int rand(arr[]){
// return an integer in the array with probabilityu of 1/N (arr size)
/ can be 1 or 2 or 3
}
arr = {1,2,3}
rand 1/3 2/3 3/3
int myrand(arr[]){
//return with probability proportional to integer
// you should return either 1 or 2 or 3 .... but with probability proportional to the number (not 1/N)
1 1/N 2 : 2/N 3:3/N
1,2,3 1,2,2,3,3,3
bufArry = new
}
If N is very large, it's not a good method (because you use lot of memory)
1 , 2 , 5
1/8 2/8 5/8
1*rand() + 2*rand() + **** + ......5* rand() / N.
define your own sizeof:
#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)