Basic CS [3i Infotech Placement]: Sample Questions 19 - 19 of 243
Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.
Question 19
Explanation
- Stack is a linear data structure for following a particular order in which the operations are performed.
- The order is LIFO (Last in First Out) or FILO (First in Last Out) .
Basic Features of Stack
- Stack is an ordered list of similar data type.
- Stack is a LIFO structure.
- Push () function is used to insert new elements into the stack and pop () function is used to delete an element from the stack.
- Both are and deletion allowed only at one end of stack called Top.
Stack Operations
- Push
- Pop
Push operation:
- Putting a new data element onto stack is known as a push operation. Steps of push operation.
- Check if the stack is full.
- If stack is full, generate an error and exit.
- Stack is not full, increment top to point next empty space.
- Add data element to the stack location, where top is pointing.
- Returns success.
Algorithm for PUSH Operation
begin procedure push: stack, dataif stack is full
return null
endif
top top + 1
stack [top] data
end procedure
Pop Operation:
- Accessing the content while removing it from the stack is known as a pop operation.
- In array implementation of pop () operation, the data element is not actually removed
- Top is decremented to a lower position in the stack to next value.
Pop Steps
- Check if the stack is empty
- Stack is empty, produces an error and exit.
- The stack is not empty, access the data element at which top is pointing.
- Decreases the value of pop by 1.
- Returns accessed element.
Algorithm for Pop Operation
begin procedure pop: stackif stack is empty
return null
endif
data stack [top]
top top -1
return data
end procedure.
Application of stack:
- Balancing of symbol
- Infix to postfix
- Redo-undo features at many places like editors, Photoshop.
- Forward and backward feature in web browsers.