Specification Design and implement a class Stack that supports stacks that are i
ID: 3760999 • Letter: S
Question
Specification
Design and implement a class Stack that supports stacks that are implemented using a linked-list.
TestLinkList.java which is below can be modified to provide the linked-list functionality. If this code is not used, then you are required to implement your own linked-list code.
The implementation of class Stack is not allowed use arrays, ArrayList, of Vector objects. In other words, it must be implemented using a linked-list.
class Stack must support the following instance methods.
You must define a class StackEmptyException that extends class Exception.
The linked-list does not have any maximum capacity for class Stack objects.
The main() method of your program should contain code to test class Stack objects. This program can use your Menu and MenuItem classes.
TestLinkList.java
Explanation / Answer
import java.util.*;
class Vertx
{
protected int data;
protected Vertx link;
public Vertx()
{
link = null;
data = 0;
}
public Vertx(int d,Vertx v)
{
data = d;
link = v;
}
public void setLink(Vertx v)
{
link = v;
}
public void setData(int d)
{
data = d;
}
public Vertx getLink()
{
return link;
}
public int getData()
{
return data;
}
}
class linkedStack
{
protected Vertx 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 data)
{
Vertx nptr = new Vertx (data, null);
if (top == null)
top = nptr;
else
{
nptr.setLink(top);
top = nptr;
}
size++ ;
}
public int pop()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception") ;
Vertx ptr = top;
top = ptr.getLink();
size-- ;
return ptr.getData();
}
public int top()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception") ;
return top.getData();
}
public void display()
{
System.out.print(" Stack = ");
if (size == 0)
{
System.out.print("Empty ");
return ;
}
Vertx ptr = top;
while (ptr != null)
{
System.out.print(ptr.getData()+" ");
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. top");
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 operation");
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.top());
}
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("you enter Wrong Entry ");
break;
}
ls.display();
System.out.println(" Want to continue this process Type y or n ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.