JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Explain about Stack?

Stack

  1. Stack is an ordered list in which, insertion and deletion can be performed only at one end that is called top.
  2. Stack is a recursive data structure having pointer to its top element.
  3. Stacks are sometimes called as Last-In-First-Out (LIFO) lists i.e. the element which is inserted first in the stack, will be deleted last from the stack.

Applications of Stack

  1. Recursion
  2. Expression evaluations and conversions
  3. Parsing
  4. Browsers
  5. Editors
  6. Tree Traversals

Operations on Stack

There are various operations which can be performed on stack.

DS Stack Introduction 
1. Push : Adding an element onto the stack

DS Stack Introduction 
2. Pop : Removing an element from the stack

DS Stack Introduction 
3. Peek : Look all the elements of stack without removing them.

How the stack grows?

Scenario 1 : Stack is empty
The stack is called empty if it doesn't contain any element inside it. At this stage, the value of variable top is -1.

DS Stack Introduction 
Scenario 2 : Stack is not empty
Value of top will get increased by 1 every time when we add any element to the stack. In the following stack, After adding first element, top = 2.

DS Stack Introduction 
Scenario 3 : Deletion of an element
Value of top will get decreased by 1 whenever an element is deleted from the stack.
In the following stack, after deleting 10 from the stack, top = 1.

DS Stack Introduction 
Top and its value :
Top positionStatus of stack
-1Empty
0Only one element in the stack
N-1Stack is full
NOverflow

.