-->

Stack Program Implementation

Posted by Admin on
A Stack  that supports the following methods:
push(Object): Add object obj at the top of the stack.  //Object can be string,int ,float etc
Input: Object; Output: None.
obj pop(): Delete an item from the top of the stack and returns object obj; an error occurs if the stack is empty.
Input: None; Output: Object.

obj peek(): Returns the top object obj on the stack , without removing it; an error occurs if the stack is empty.
Input: None; Output: Object.
boolean isEmpty(): Returns a boolean indicating if the stack is empty.
Input: None; Output: boolean (true or false).
int size(): Returns the number of items on the stack.
Input: None; Output: integer.

Stack Implementation Using Array :

import java.util.*;
import java.lang.*;
 
interface Stack
{ 
public void push(Object ob);
public Object pop();
public Object peek();
public boolean isEmpty();
public int size();
}
 
class ArrayStack implements Stack
{
private Object a[];
private int top; // stack top
public ArrayStack(int n) // constructor
{ a = new Object[n]; // create stack array
top = -1; // no items in the stack
}
public void push(Object item) // add an item on top of stack
{
if(top == a.length-1)
{ System.out.println("Stack is full");
return;
}
top++; // increment top
a[top] = item; // insert an item
}
public Object pop() // remove an item from top of stack
{
if( isEmpty() )
{ System.out.println("Stack is empty");
return null;
}
Object item = a[top]; // access top item
top--; // decrement top
return item;
}
public Object peek() // get top item of stack
{ if( isEmpty() ) return null;
return a[top];
}
public boolean isEmpty() // true if stack is empty
{ return (top == -1); }
public int size() // returns number of items in the stack
{ return top+1; }
}
 
public class Main 
{
public static void main(String[] args)
{
ArrayStack stk = new ArrayStack(4); // create stack of size 4
Object item;
stk.push('A'); // push 3 items onto stack
stk.push('B');
stk.push('C');
System.out.println("size(): "+ stk.size());
item = stk.pop(); // delete item
System.out.println(item + " is deleted");
stk.push('D'); // add three more items to the stack
stk.push('E');
stk.push('F');
System.out.println(stk.pop() + " is deleted");
stk.push('G'); // push one item
item = stk.peek(); // get top item from the stack
System.out.println(item + " is on top of stack");
}
}


Stack Implementation Using Linked List :

//Stack interface 
interface Stack
{ 
public void push(int d);
public int pop();
public int peek();
public boolean isEmpty();
public int size();
}
 
//to create the node of a class
class Node 
{
    int data ;
    Node next;
    Node(int d)
    {
        data =d;
        next=null;
    }
}
 
class StackLinkedList implements Stack
{
    Node root=null;
    Node newnode;
    Node temp;
    
    /*push function to add th element at the top */
    public void push(int d)
    {
        newnode = new Node(d);
        if(root==null)
        {
            root=newnode;
        }
        else 
        {
          newnode.next=root;
          root=newnode;
        }
    }
    
    //pop function to retrieve the elemet from the top 
    public int pop()
    {
        if(root==null)
        {
            System.out.println("Stack is empty ");
            return -1;
        }
        
        int num=root.data;
        Node temp =root.next;
        root=temp;
        return num;
        
    }
    
    //function to check wether stack is empty
    public boolean isEmpty()
    {
        if(root==null)
        return true;
        else
        return false;
    }
    
    //function to return the toppest element without deleting it 
    public int peek()
    {
        if(root==null)
        {
            System.out.println("Stack is empty ");
            return -1;
        }
        
        int num=root.data;
        return num;
    }
        
    // function to return the size(number of elements) of satck    
    public int size()
    {
        Node temp=root;
        int count=0;
        while(temp!=null)
        {
            count++;
            temp=temp.next;
        }
        return count;
    }
}
 
public class Main {
    public static void main (String[] args)     {
    StackLinkedList S=new StackLinkedList();
    S.push(9);
    S.push(1);
    S.push(8);
    S.push(2);
    System.out.println(S.peek());
    System.out.println(S.pop());
    System.out.println(S.size());
  }
}


Please Comment if you find any mistake.


1 comment: