Question: Implement this java code in c++ Question for Java -- Implement the following ADT in Java ADT: - A stack that does not allow duplicates

Implement this java code in c++

Question for Java --

Implement the following ADT in Java ADT:

- A stack that does not allow duplicates The ADT allows only a single copy of an object in the stack. Stack is unchanged if a duplicate object is being added to it.

- support the usual peek(), pop(), push(newElement) methods - Also, the following operations:

-- find(x) that finds the location of an element x in the stack. The first element in the stack has location number one. The second pushed element has location number two and so on.

-- peek1() returns the first pushed element

CDT/Data structure:

- Use a circular linked list Specifications:

- write the proper preconditions and postconditions of each method Exceptions handling:

- The stack has a limit. The stack throws a user exception whenever a client push an item to a full stack. The size of the stack is arbitrary.

- Also, throws a user exception whenever peek or pop operations being called on an empty stack Design Decisions:

- Separate the the ADT specification from its implementation using Java Interface

Make the ADT generic Testing:

- write in a main() function sufficient test code to test all the methods that the ADT support

- check corner cases; i.e. pushing to full stack, peeking into an empty stack, pushing a duplicate item, ..etc

(Answer in Java )

MidetermStackInterface.java

public interface MidtermStackInterface { // push an item to the top of the stack // pre condition: the stack should not be full // pre condition: the item should not exist already in the stack // post condition: throws an exception when the stack is full void push(T a) throws FullStackException; // postcondition: remove and return an item from the top of the stack // precondition: the stack should not be empty // post condition: throws an exception when the stack is empty T pop() throws EmptyStackException; // postcondition: return an item from the top of the stack // precondition: the stack should not be empty // post condition: throws an exception when the stack is empty T peek() throws EmptyStackException; // postcondition: return the first item pushed to the stack // precondition: the stack should not be empty // post condition: throws an exception when the stack is empty T peek1() throws EmptyStackException; // postcondition: return the size of the stack int getsize(); //finds the location of an element x in the stack. // The first element in the stack has location number one. The // second pushed element has location number two and so on. // if not exist may be return -1 int find(T x); }

MidtermStackClass.java

class Node{ T data; Node next; } class FullStackException extends Exception{ public FullStackException(){ super("You can't push the item because .."); } } class EmptyStackException extends Exception{ public EmptyStackException(){ super("You can't pop or peek on an Empty stack because .."); } } public class MidtermStackClass implements MidtermStackInterface{ Node front; Node back; int LIMIT = 4; int size; MidtermStackClass(){ front = null; } boolean itemfound(T a){ Node iterartor = front; while(iterartor!= back){ T tmp =(T)iterartor.data; if(tmp == a) return true; iterartor = iterartor.next; } return false; } public void push(T a) throws FullStackException{ if (size == LIMIT) throw new FullStackException(); else { if(!itemfound(a)){ Node newNode = new Node(); newNode.data = a; if (front == null) { front = newNode; back = newNode; newNode.next = front; } else { newNode.next = front; front = newNode; back.next = newNode; } size++; } } } public T pop() throws EmptyStackException{ T tmp = peek(); if(front == back){ front = null; back = null; } else{ front = front.next; back.next = front; size--; } return tmp; } public T peek() throws EmptyStackException{ if (size == 0) throw new EmptyStackException(); T tmp = (T)front.data; return tmp; } public T peek1() throws EmptyStackException{ if (size == 0) throw new EmptyStackException(); T tmp = (T)back.data; return tmp; } public int getsize(){ return size; } public int find (T x){ Node iterartor = front; if(front == back && front.data == x) return 1; int counter = 0; while(iterartor!= back){ T tmp =(T)iterartor.data; if(tmp == x) return size - counter; iterartor = iterartor.next; counter++; } T tmp =(T)iterartor.data; if(tmp == x) return size - counter; return -1; } public static void main(String args[]) { MidtermStackInterface s = new MidtermStackClass(); try { System.out.println(s.peek()); // empty exception } catch (EmptyStackException e){ System.out.println(e.getMessage()); } try{ s.push(7); s.push(6); s.push(1); s.push(100); s.pop(); System.out.println(s.peek()); // should be 1 System.out.println(s.peek1()); // should be 7 s.push(100); // should ignore System.out.println(s.getsize()); // should be 1 s.push(5); // full exception } catch (FullStackException e){ System.out.println(e.getMessage()); } catch (EmptyStackException e){ System.out.println(e.getMessage()); } System.out.println("finding ... "); //2 System.out.println(s.find(66)); //-1 System.out.println(s.find(7)); //1 System.out.println(s.find(6)); //2 System.out.println(s.find(100)); // 4 } }

Implement the above solution in c++

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!