Reverse order of elements on a slack, Data Structure & Algorithms

Q. Reverse the order of the elements on a stack S

   (i) by using two additional stacks

(ii) by using one additional queue.

Ans:      Let S be the stack having n number of elements. Now we need to reverse the elements of S (i) using the two additional stack S1 and S2

while not empty (S)

{       C=pop(S);

push(S1,C);

}

While not empty(S1)

{     C=pop(S1);

push(S2,C);

}

While not empty(S3)

{     C=pop(S3);

push(S,C);

}

(ii) using one additional queue Q

while not empty (S)

{       C=pop(S);

enque(Q,C);

}

While not empty(Q)

{     C=deque(Q);

push(S,C);

}

Posted Date: 7/10/2012 3:11:04 AM | Location : United States







Related Discussions:- Reverse order of elements on a slack, Assignment Help, Ask Question on Reverse order of elements on a slack, Get Answer, Expert's Help, Reverse order of elements on a slack Discussions

Write discussion on Reverse order of elements on a slack
Your posts are moderated
Related Questions

Determine the types of JAVA Java has two parts... 1. Core language -- variables, arrays, objects o Java Virtual Machine (JVM) runs the core language o Core language is

Q. Suggest a method of implementing two stacks in one array such that as long as space is there in an array, you should be capable to add an element in either stack. Using proposed

Ask question #Minimum 1Mark each of the following statements as valid or invalid. If a statement is invalid, explain why. a. current ¼ list; b. temp->link->link ¼ NULL; c. trail->l

Let us assume a sparse matrix from storage view point. Assume that the entire sparse matrix is stored. Then, a significant amount of memory that stores the matrix consists of zeroe


Best - Fit Method: - This method obtains the smallest free block whose  size is greater than or equal to get such a block by traversing the whole free list follows.

The quick sort algorithm exploit design technique Divide and Conquer

What do you mean by hashing? Hashing gives the direct access of record from the file no matter where the record is in the file. This is possible with the help of a hashing func