Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.Document and code Stack hierarchy including all 3 ADT levels and relevant clas

ID: 3764053 • Letter: 1

Question

1.Document and code Stack hierarchy including all 3 ADT levels and relevant classes described in Chapter 3 text, diagrams and source code.

2.Create3 new methods for StackInterface and ArrayStack:

1.toString – Ex. 29 Chapter 3

2.popSome – Ex. 34a Chapter 3 as defined in Ex. 20

3.popTop – Ex. 45 Chapter 3 (the exercise is written for LinkedStack, but you will do it for ArrayStack)

4.For extra credit, do the same for ArrayListStack implementation.

3.Test applications to test all new and existing methods

1.Interactive or batch is your choice (both for extra credit)

2.In addition, perform the tasks as described in Ex. 30, Chapter 3 in a separate application class. You may use the methods of ArrayStack that you created in Step 2

Explanation / Answer

import java.util.*;


class Node
{
protected int element;
protected Node link;


public Node()
{
link = null;
element = 0;
}

public Node(int e,Node n)
{
element = e;
link = n;
}
  
public void setLink(Node n)
{
link = n;
}

public void setElement(int e)
{
element = e;
}
  
public Node getLink()
{
return link;
}

public int getElement()
{
return element;
}
}


class linkedStack
{
protected Node top ;
protected int size ;


public linkedStack()
{
top = null;
size = 0;
}

public boolean isEmpty()
{
return top == null;
}

public int getSize()
{
return size;
}
  
public void push(int element)
{
Node nptr = new Node (element, null);
if (top == null)
top = nptr;
else
{
nptr.setLink(top);
top = nptr;
}
size++ ;
}

public int pop()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception") ;
Node ptr = top;
top = ptr.getLink();
size-- ;
return ptr.getElement();
}

public int peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception") ;
return top.getElement();
}

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.getElement()+" ");
ptr = ptr.getLink();
}
System.out.println();
}
}


public class LinkedStackImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);   
  
linkedStack ls = new linkedStack();

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;
}   
  
ls.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);   

} while (ch == 'Y'|| ch == 'y');   
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote