Int empty stack s return s.top 1 năm 2024

A stack is a linear data structure in which an element is inserted or deleted only from the top position. Stack is based on last in last out (LIFO) property i.e. element inserted first will be the last one to be deleted.

For example, assume a stack of plates kept in a rack. Now if someone wants to utilize a plate, he will pull the topmost plate kept in the rack. Also, if someone wants to add a washed plate in the rack, he will be only adding it to the top.

Let us consider a static implementation of stack which is implemented using an array. As arrays only stores similar type of elements, stack will also store homogenous elements. Figure below shows an array implementation of a stack. Assume name of the array is stack with maximum size (MaxSize) 5.

Insertion into stack (PUSH)

Initially the stack is empty and value of top is -1. When an element is inserted the value of top will be incremented by 1 and the element would be inserted at the array index specified by the top variable. Same process is followed when more elements are inserted into stack until the stack is completely full.

Overflow condition: When stack is completely full (i.e. TOP= MaxSize -1 ) and we try to insert more element onto stack then this condition is called overflow condition and no further element could be inserted now until any element is deleted.

Deletion from stack (POP)

Whenever any element is deleted from stack, it is always done from top position. Element pointed out by top variable is deleted (or left as it) and value of top is decremented by 1 until the stack is empty.

Underflow Condition: When a stack is empty (i.e. TOP= -1) and we try to delete more element from it, then this condition is called underflow condition.

Int empty stack s return s.top 1 năm 2024

Int empty stack s return s.top 1 năm 2024

Function for PUSH operation

void push (int data) { if (TOP == MaxSize -1) {

cout<<” Stack Overflow ”;  
} else { TOP= TOP +1 } stack[TOP]= data; }

Function for POP operation

void pop () { int deleted_data,; if (TOP ==-1) { cout<<" Stack Underflow"; } else { deleted_data= stack[TOP]; TOP=TOP -1 }

Function for traversing a stack

void display() { if(TOP==-1) { cout<<"Stack Underflow"; } else{ cout<< " Elements are :"; for (int i=TOP; i>=0;i--) { cout<

Application of Stacks

  • Expression evaluation
  • Expression conversion (infix to postfix, infix to prefix etc.)
  • Parsing
  • Memory management
  • Recursion
  • Function call
  • Reversing a string

Logic for reversing a string using stacks

for(int i=0;i

Logic for finding that a string has balanced parenthesis or not

int exp_valid=1; char ele; for(int i=0; i=0) { ele_valid=0; } if(ele_valid==1) { cout<<"Balanced Parenthesis"; } else { cout<<”Opening and closing parenthesis does not match; }

Question

Assume an array Stack[Size] with starting index 1 and ending index size. Suppose this array is used to implement two stacks. The two stacks grow from opposite ends of the array. There are two variables TOP1 and TOP2 where TOP1< TOP2. These two variables point to topmost element in each of the stacks respectively. If the space is to be used efficiently, what will be the condition of full stack.

  1. (TOP1 =SIZE/2) and (TOP2 = SIZE/2+1)
  1. TOP1 + TOP2 = SIZE
  1. (TOP1= SIZE/2) or (TOP2 = SIZE)
  1. TOP1= TOP2 -1
Explanation:

Assume an array of size 5

Initially TOP1=1, TOP=Size=5

Int empty stack s return s.top 1 năm 2024

After stack is full TOP1=3, TOP2=4 which means TOP1=TOP2-1

Answer: d

Nainital is a Himalayan resort town in the Kumaon region of India s Uttarakhand state, at an elevation of roughly 2,000m. A group of students from a reputed college went there for an educational tour. They went there for performing some experiment regarding their project. Each student is placed on each different hill point of each hill with a modulation machine.

Suppose all machines are aligned in a straight horizontal line from left to right and each machine transmits a signal in the right to left direction. Machine A shall block the signal of machine B if machine A is present to the left of machine B and machine A is taller than machine B. So, the range of a signal of a given machine can be defined as : the number of contiguous machines just to the left of the given machine whose height is less than or equal to the height of the given machine + 1. You need to find the range of each machine.

Input Format

Input 1: It will be an integer N specifying the number of machines where 2 <= N <= 10^6

Input 2: It will be a string denoting N comma separated integers(H[i]) denoting the height of each machine where 1 <= H[i] <= 10^8.

What does top =

Underflow Condition: When a stack is empty (i.e. TOP= -1) and we try to delete more element from it, then this condition is called underflow condition.

What does top () return if stack is empty?

The first version of the top function returns a reference to the element of the top of the stack, allowing you to modify the value. The second function returns a constant reference, ensuring that you don't accidentally modify the stack. The empty function returns true if there are no elements in the stack.

How do you return the top of a stack?

top() The . top() method returns the top element on the stack . This will be the most recently added element on the stack since they follow a last-in first-out (LIFO) insertion order.

What does stack empty () return?

empty() method returns true if the stack has no elements. Otherwise, it returns false .