public boolean isEmpty which returns true if and only if the

public boolean isEmpty(), which returns true if and only if the stack is empty;

public T pop() throws StackUnderflowException, which removes and returns the top element of the

stack;

public T peek() throws StackUnderflowException, which returns the top element of the stack;

public void push(T element), which pushes element onto the stack;

Question: Implement the isEmty(), pop(), peek() and push() methods in the LinkedListStack class implementing Stack.

Solution

/* * Java Program to Implement Stack using Linked List */ import java.util.*; /* Class Node */ class Node { protected int data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(int d,Node n) { data = d; link = n; } /* Function to set link to next Node */ public void setLink(Node n) { link = n; } /* Function to set data to current Node */ public void setData(int d) { data = d; } /* Function to get link to next node */ public Node getLink() { return link; } /* Function to get data from current Node */ public int getData() { return data; } } /* Class linkedStack */ class linkedStack { protected Node top ; protected int size ; /* Constructor */ public linkedStack() { top = null; size = 0; } /* Function to check if stack is empty */ public boolean isEmpty() { return top == null; } /* Function to get the size of the stack */ public int getSize() { return size; } /* Function to push an element to the stack */ public void push(int data) { Node nptr = new Node (data, null); if (top == null) top = nptr; else { nptr.setLink(top); top = nptr; } size++ ; } /* Function to pop an element from the stack */ public int pop() { if (isEmpty() ) throw new NoSuchElementException(\"Underflow Exception\") ; Node ptr = top; top = ptr.getLink(); size-- ; return ptr.getData(); } /* Function to check the top element of the stack */ public int peek() { if (isEmpty() ) throw new NoSuchElementException(\"Underflow Exception\") ; return top.getData(); } /* Function to display the status of the stack */ public void display() { System.out.print(\"\ Stack = \"); if (size == 0) { System.out.print(\"Empty\ \"); return ; } Node ptr = top; while (ptr != null) { System.out.print(ptr.getData()+\" \"); ptr = ptr.getLink(); } System.out.println(); } } /* Class LinkedStackImplement */ public class LinkedStackImplement { public static void main(String[] args) { Scanner scan = new Scanner(System.in); /* Creating object of class linkedStack */ linkedStack ls = new linkedStack(); /* Perform Stack Operations */ System.out.println(\"Linked Stack Test\ \"); char ch; do { System.out.println(\"\ Linked Stack Operations\"); System.out.println(\"1. push\"); System.out.println(\"2. pop\"); System.out.println(\"3. peek\"); System.out.println(\"4. check empty\"); System.out.println(\"5. size\"); int choice = scan.nextInt(); switch (choice) { case 1 : System.out.println(\"Enter integer element to push\"); ls.push( scan.nextInt() ); break; case 2 : try { System.out.println(\"Popped Element = \"+ ls.pop()); } catch (Exception e) { System.out.println(\"Error : \" + e.getMessage()); } break; case 3 : try { System.out.println(\"Peek Element = \"+ ls.peek()); } catch (Exception e) { System.out.println(\"Error : \" + e.getMessage()); } break; case 4 : System.out.println(\"Empty status = \"+ ls.isEmpty()); break; case 5 : System.out.println(\"Size = \"+ ls.getSize()); break; case 6 : System.out.println(\"Stack = \"); ls.display(); break; default : System.out.println(\"Wrong Entry \ \"); break; } /* display stack */ ls.display(); System.out.println(\"\ Do you want to continue (Type y or n) \ \"); ch = scan.next().charAt(0); } while (ch == \'Y\'|| ch == \'y\'); } }
public boolean isEmpty(), which returns true if and only if the stack is empty; public T pop() throws StackUnderflowException, which removes and returns the top

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site