It2EDU

Wednesday, December 14, 2016

Data Structure - Stack









Stack Data Structure:


A stack is a basic data structure or abstract data type or collection. This
data type allows operations of PUSH and POP.






PUSH: Insert elements into the data structure.


POP: Remove elements from the data structure.


PIP: Displaying the elements of the stack.





PUSH and POP are major operations performed in this data
structure. These operations are performed only at one end of the stack that is
known as top of the stack. This means Stack follows LIFO (Last In First Out)
order and this data structure called as LIFO data structure.  Last element removed first and last element
added at the top of the stack. 












This concept can be illustrated by thinking of your data set as a stack of
plates or books where you can only take the top item off the stack in order to
remove things from it. This structure is used all throughout programming.





If a stack is full and does not contain enough space to accept an element to
be pushed then the stack is considered as over flow state. Stack may be
implemented with a bounded capacity over flow state occurs.



If stack is empty it goes to under flow state, this means that the stack has
no elements to be popped.



Efficiency of Stack: In the stack, the elements can be push or pop one at a
time in constant O(1) time. That is, the time is not dependent on how many
items are in the stack and is therefore very quick.

No comparisons or moves are
necessary.





Java Methods for Stack data structure:





Java provides these methods in Util package. if you want to write a program
in java to implement Stack must import the java.util package.






1.     
boolean empty() – It returns the boolean value if the
stack is empty it return true, otherwise false.


2.     
push(element) – Put the element into the stack.


3.     
pop() – Removes the elements from the stack.


4.     
peek() – It returns the top element of stack.


5.     
int search(element) - Searches for element in the
stack. If found, its offset from the top of the stack is returned. Otherwise,
.1 is returned.





Program for Stack Implementation in Java:



import java.util.*;

public class JavaStackExample {
    public static void main(String args[]){
        Stack st = new Stack();
        System.out.println("Check the status of the stack empty  T/F  --->"+st.isEmpty());
       
        st.push("Hi");
        st.push("java");
        st.push("world");
       
        System.out.println("All elements in the stack"+st);
        st.pop(); // It remove first element of the stack."hi" is removed
        System.out.println("After POP All elements in the stack"+st);
        System.out.println("Element to be searched at    "+st.search("hi"));
        System.out.println("First Element in the Stack:"+st.peek());
    }

}



Program Compilation:



D:>javaprograms>javac JavaStackExample.java



D:>javaprograms>java JavaStackExample









output of the above program:


Check the status of the stack empty  T/F  --->true
All elements in the stack[Hi, java, world]
After POP All elements in the stack[Hi, java]
Element to be searched  at   -1
First Element in the Stack:java


























































0 comments:

Post a Comment