Stack
Stack
It is named
stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc.
A real-world stack allows operations at one end only. For example, we can place or remove a card or
plate from the top of the stack only. Likewise, Stack ADT allows all data operations at one end only. At
any given time, we can only access the top element of a stack.
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is
placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is
called PUSH operation and removal operation is called POP operation.
Stack Representation
A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack can either be a
fixed size one or it may have a sense of dynamic resizing. Here, we are going to implement stack using
arrays, which makes it a fixed size stack implementation.
Basic Operations
Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these
basic stuffs, a stack is used for the following two primary operations −
push() − Pushing (storing) an element on the stack.
pop() − Removing (accessing) an element from the stack.
When data is PUSHed onto stack.
To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the
following functionality is added to stacks −
peek() − get the top data element of the stack, without removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty.
Questions: The five items P,Q,R,S and T are pushed in a stack, one after the other starting from P. The
stack is popped four times and each element is inserted in a queue. Then two elements are deleted from
the queue and pushed back on the stack. now one item is popped from the stack. The popped item is:
2. Which data structure allow deleting data elements front and inserting at Ans. b
rear?[Assistant programmer (ICB)- 2017]
a) Stack b) Queue c) Dequeue d) Binary search tree
4. Which of the following name does not related to stacks?[AP(ICB)- 2017, Ans. a
AP(HBFC, KB)-2018]
a) FIFO List b) LIFO List c)Piles d) Push Down List
Explanation: FIFO is related to Queue.
6. Which of these data types is used by operating system to manage the Ans. b
Recursion in Java?[Combined(IT/ICT-2018)]
a)Array b) Stack c) Queue d) Tree
7. Pushing an element into stack already having five elements and stack Ans. a
size of 5. Result in [Combined (AP)-2018]
a) Overflow b) Crash c) Underflow d) User flow
9. Find the correct arranged data after stack operation? [BB (AP)-2016] Ans. a
push(1), push(2), pop, push(1), push(2), pop, pop, pop, push(2)
(a) 2 2 1 1 2 (b) 2 2 1 2 1 (c) 2 2 2 2 1 (d) 2 2 2 1 2
11. What will be the state of stack after executing following operation? Ans.: d
push(1), push(2), pop(), push(4), push(5), pop()[SBL&JBL(AME)-2020]
a) 2, 5 b) 2, 4 c) 4, 5 d) 1, 4 Ans:d
3. What happens when you push a new node onto a stack? Ans. a
a) The new node is placed at the front of the linked list
b) The new node is placed at the back of the linked list
c) The new node is placed at the middle of the linked list
d) No Changes happens
5. Entries in a stack are “ordered”. What is the meaning of this statement? Ans. d
a) A collection of stacks is sortable
b) Stack entries may be compared with the ‘<‘ operation
c) The entries are stored in a linked list
d) There is a Sequential entry that is one by one
9. Here is an infix expression: 4 + 3*(6*3-12). Suppose that we are using the Ans. d
usual stack algorithm to convert the expression from infix to postfix
notation. The maximum number of symbols that will appear on the stack
AT ONE TIME during the conversion of this expression?
a) 1 b) 2 c) 3 d) 4