Wikipedia

Search results

Friday, October 25, 2013

Stack

Stack

What is stack ? 
Lets talk about it in general terms to be most simple its nothing but a pile of objects e.g. pile of plates,boxes,towels,etc . So whenever you want to take out a box you need to pick the boxes above and then reach the your target desired box .
(Example of stack of boxes)

(Example of stack of plates)

Right its so simple you need something from a stack just pick the objects on top and you get to your target destination. 


Same goes for programming Languages we follow the same steps the IT jargon for its operations is LIFO(Last In Last Out) . We make a pile of objects and play with them in order to achieve the required object by our program .


Now I am gonna jump to a program that is gonna help you understand how stack operation is done in JAVA . 


Code Description:-

I have declared an array naming "arr"  and have maintained a counter name "top".

PUSH operation on a stack(Insertion of elements on stack):-

When trying to push or insert an element onto a stack i am going to insert a element at the end in order to do so I need to increase my counter by 1.
For e.g.:-

when top =0
     arr[++top] --> means arr[1] 
     so when an element is inserted it is at arr[1] position.

The increment is done first in order to take our new element at a vacant position for insertion .

Also while pushing/inserting an element on the stack we need to check if the stack is full or not, as it might throw an exception and we need to handle it.Doing it by following statement 

top < DEFAULT_CAPACITY-1  



POP operation on a stack(Deletion of elements on stack):-

When trying to pop or delete an element from a stack i am going to delete an element from  the end in order to do so I need to first decrease my counter by 1.
For e.g.:-

when top =10
     arr[top--] --> means element at arr[10]th position 
this is so because its a post increment operator and in next line the counter becomes 9 i.e. top becomes 9
so when an element is deleted it is deleted from arr[10] position.

The decrement is done latter in order to make the element available for GC(Garbage Collection)

Also while poping/deleting the elements from the stack we need to check if the stack is already empty,as it would again throw an exception when we try to remove an element from a empty stack . So if our counter turns to "-1" it means all the elements are removed and stack is empty .


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 *  Stack using an Array 
 *  @ Author Karan Deep Singh
 * */
public class Stack {
 
 static int arr[];
 public static int top=0;
 public static int DEFAULT_CAPACITY=5;
 Stack()
 {
  arr=new int[DEFAULT_CAPACITY];
  top=-1;
 }
 // insert element on a stack, first increase the pointer then insert so that element is inserted on top of stack 
 // check if stack is full or not 
 private  void push(int data)
 {
  if(top<DEFAULT_CAPACITY-1)
  {
   arr[++top]=data;
   
  }else  
   System.out.println("Stack Overflow");
 }
 //delete element from a stack, after delete then  decrease the pointer so that element is removed from top of stack
 //check if stack is empty 
 private  int pop() throws Exception
 {
  if(top==-1)
  {
   throw new Exception("Stack is Empty");
   
  }
  return arr[top--];
 }
 // traverse or just view the element at a given position 
 private  int peek()
 {
  return arr[top];
 }
 
 public static void main(String[] args) throws Exception {
  
  Stack stack=new Stack();
  stack.push(10);
  stack.push(12);
  stack.push(13);
  stack.push(14);
  stack.push(15);
  stack.push(16);
  
  
  
  System.out.println(stack.pop());
  System.out.println(stack.peek());
  System.out.println(stack.peek());
  System.out.println(stack.pop());
  System.out.println(stack.peek());
  System.out.println(stack.pop());
  System.out.println(stack.pop());
  System.out.println(stack.pop());
  System.out.println(stack.pop());
  
  
 }
}








No comments: