its answer is :- ( ( ( ) )
i think its answer is ( ( ) ) ) ( )
its answer is :- ( ( ( ) )
i think its answer is ( ( ) ) ) ( )
i think we have to refer this code : -
declare a character stack
while ( more input is available)
{
read a character
if ( the character is a '(' )
push it on the stack
else if ( the character is a ')' and the stack is not empty )
pop a character off the stack
else
print "unbalanced" and exit
}
print "balanced"
@anshulgurawalia the code will print if the sequence is balanced or not how are you saying answer should be this?
i think its answer is ( ( ) ) ) ( )
i got it now i am calculating the answer on the behalf of another code which we learned will implementing stack
please consider one more ques :-
Let Q denote a queue containing sixteen numbers and S be an empty stack. Head(Q) returns the element at the head of the queue Q without removing it from Q. Similarly Top(S) returns the element at the top of S without removing it from S. Consider the algorithm given below.
while Q is not empty do {
if S is empty OR Top(S)<= Head(Q) then {
x = Dequeue(Q)
Push(S, x)
} else {
x = Pop(S)
Enqueue(Q,x)
}
}
16
32
64
256
The maximum possible number of iterations of the while loop in the algorithm is
@anshulgurawalia for this you can check this:
The worst case happens when the queue is sorted in decreasing order. In worst case, loop runs n*n times.
Queue: 4 3 2 1
Stack: Empty
3 2 1
4
3 2 1 4
Empty
2 1 4
3
2 1 4 3
Empty
1 4 3
2
1 4 3 2
Empty
4 3 2
1
3 2
1 4
3 2 4
1
2 4
1 3
2 4 3
1
4 3
1 2
3
1 2 4
3 4
1 2
4
1 2 3
Empty
1 2 3 4