What is Stack and Implementation of Stack using Array
In this article we will discuss about stack
Operations
Real Life Example of Stack:
In this article we will discuss about stack
- What is stack?
- What is the principal of stack?
- Some application of stack?
- How to insert data?
- How to remove data and many operations?
What is a stack?
Stack is a linear data structure.
Explaining : Yes, we can say that when we keep a book from the posts of a book, then even if we keep a book above it, it becomes a stack, then this is what we call the stack. If we lift from above, now it is not that we can lift the book from the middle, this all phenomena are stacking.
Principal :
when we insert a value into a stack and removing a value from stack then the principal work last in first out means the principle of stack is LIFOOperations
- Insert a value into Stack (PUSH)
- Removing the value from Stack (POP)
- Peek the value from Stack (PEEK)
Explaining: What is PUSH, POP, PEEK
- The first operation is push simply push means to insert a data into stack this is called a push.
- The second operation is pop simply pop means remove the element from stack.
- Last operation is peak simply pick means return the value of the top of the stack this is called peak.
Implementation:
- Using Array
- Using Dynamic Array
- Using Linked List
And now we will discuss more methods in stack like stack is empty stack is full, the size of stack.
class Stack{
int capacity;
int top;
int arr[];
public Stack(){
top=-1;
capacity=5;
arr=new int[capacity];
}
public boolean isEmpty(){
return top==-1;
} //check stack is empty
public boolean isFull(){
return top==capacity-1;
} //check stack is full
public int push(int data){ // insert data into stack
if (isFull()) {
System.out.println("stack is full");
}
return arr[++top] = data;
}
public int pop(){ //remove data into stack
if (isEmpty()){
System.out.println("stack is empty");
}
return arr[top--];
}
public int peek(){ //return top value of stack
return arr[top];
}
public void printStack(){ // print value of stack using for each loop
for (int a:arr){
System.out.print(a+" ");
}
}
}
public class StackUsingArray { //implementation of stack using array
public static void main(String[] args) {
Stack st=new Stack();
st.push(10);
st.push(20);
st.push(30);
st.push(40);
st.printStack(); //print stack
System.out.println(st.pop()); // remove 40 from top
st.printStack(); // print stack
//System.out.println();
}
}
Output :
// last 40 value show we pop the element from stack
Real Life Example of Stack:
- Reverse a number
- Undo in browser text editors and many more
- Wearing and removing bangles
- And this is also a stack:plates/books in a cupboard
- And many more application of stacks
Recommended Topics for Freshers :
Sorting and Searching
Programs in Java
Start with your
Choice
-Important Programs for Freshers
- Palindrome Number
- Prime
Number
- Swapping
Number
- Leap
Year
- Odd
and Even Number
- Fibonacci
Series
- Armstrong Number
- Factorial
Number
- Print
Table
Learn Pattern Programs
- Alphabet
pattern program in java
- Star Pattern Programs in Java
- Square
pattern programs in java
- Diamond
Star pattern programs in java
- Number
Pattern Programs in java
- Diagonal
pattern program in java
Click to learn Array
Programs
- 4
ways to print Array in Java
- 2-D
Array in Java
- Anonymous
Array in Java
- Single
Dimensional Array in Java
- Find
Second Largest Number in Array
- Delete
an Element in Array in Java
- Common
Element in Array in Java
- Missing
Element in Array
- Insert
Element in Array
- Reverse Array in Java
- Merge Two Array in Java
- Smallest
and Largest Elements in Array
- Find
Odd and Even Number in Array
Click
to Learn MySQL
for any complaint regarding my Blog please
visit contact us page and
write what problem you have!
Learn with us | HTML | CSS | Java Script |
Bootstrap | JAVA | ADV JAVA | MySQL| GIT | Data Structure
No comments:
Post a Comment