Modify the class LinkedStack given in class in the following ways. Add the follo
ID: 3588701 • Letter: M
Question
Modify the class LinkedStack given in class in the following ways. Add the following methods:
1. A constructor that loads the stack from an array parameter. The last array element should be at the top of the stack
2. A method named peekNextTop that allows you to retrieve the element just below the one at the top of the stack without removing it. It should return null if the stack has just one element, and it should throw an exception if the stack is empty.
Note: You CAN NOT add or remove data fields to LinkedStack class. You CAN NOT change the definition of Node class.
Modify the class ListQueue2 given in class in the following ways.
Add the following methods:
1. public int size() Return the size of the current queue (the calling ListQueue2 object).
2. public boolean empty() Return true if the queue is empty, false otherwise
Note: You CAN NOT add or remove data fields to ListQueue2 class. You CAN NOT change the definition of Node class.
Write a java application called HiringApp that you will use to hire and fire workers for a company. The application HiringApp must use the LinkedStack and LinkedQueue2 classes that are covered in the class. The rules of hiring and firing are described as follows:
1) If you are asked to fire somebody at a time when the firm has no employees, you should notify your supervisor (print a message).
2) If you are asked to fire somebody when the firm has 1 or more employees, you must fire the most recently hired.
3) You are to keep a list of applicants and the order in which they applied.
4) When you are asked to hire someone, if anybody has been fired, the most recently fired must be re-hired.
5) If there is nobody who has been fired, then the person who applied earliest is to be hired.
6) If there is nobody available for hiring, then you must notify your supervisor (print a message).
The program should have a simple menu that allows you to specify the three actions:
• Accept application
• Hire
• Fire
• Quit
"Accept application" should prompt you for the name of an applicant and add that person's information to an appropriate data structure. "Hire" should choose the appropriate person to hire, print his or her name, and appropriately update the internal data structures. "Fire" should choose the appropriate person to fire, print his or her name, and appropriately update the internal data structures. "Quit" should exit the program.
Hints: 1. The code that are related to this assignment and covered in the lecture section is in the attached Assign3RelatedCode.zip. 2. Before you start programming, you need to decide which data structure you will use to store the people involved in this HiringApp.
Related Code
public class LinkedStack<E> implements StackInt<E> {
// Insert inner class Node<E> here (see Listing 2.1)
/** A Node is the building block for a single-linked list. */
private static class Node<E> {
// Data Fields
/** The reference to the data. */
private E data;
/** The reference to the next node. */
private Node<E> next;
// Constructors
/**
* Creates a new node with a null next field.
* @param dataItem The data stored
*/
private Node(E dataItem) {
data = dataItem;
next = null;
}
/**
* Creates a new node that references another node.
* @param dataItem The data stored
* @param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef) {
data = dataItem;
next = nodeRef;
}
}
//End of inner class Node<E>
// Data Fields
/** The reference to the first stack node. */
private Node<E> topOfStackRef = null;
/**
* Insert a new item on top of the stack.
* @post The new item is the top item on the stack.
* All other items are one position lower.
* @param obj The item to be inserted
* @return The item that was inserted
*/
@Override
public E push(E obj) {
topOfStackRef = new Node<E>(obj, topOfStackRef);
return obj;
}
/**
* Remove and return the top item on the stack.
* @pre The stack is not empty.
* @post The top item on the stack has been
* removed and the stack is one item smaller.
* @return The top item on the stack
* @throws EmptyStackException if the stack is empty
*/
@Override
public E pop() {
if (empty()) {
throw new EmptyStackException();
} else {
E result = topOfStackRef.data;
topOfStackRef = topOfStackRef.next;
return result;
}
}
/**
* Return the top item on the stack.
* @pre The stack is not empty.
* @post The stack remains unchanged.
* @return The top item on the stack
* @throws EmptyStackException if the stack is empty
*/
@Override
public E peek() {
if (empty()) {
throw new EmptyStackException();
} else {
return topOfStackRef.data;
}
}
/**
* See whether the stack is empty.
* @return true if the stack is empty
*/
@Override
public boolean empty() {
return topOfStackRef == null;
}
//End of class content for Students.
}
---------------------------------------------------------------------------
public class ListQueue2<E> {
// Data Fields
/** Reference to front of queue. */
private Node<E> front;
/** Reference to rear of queue. */
private Node<E> rear;
/** Size of queue. */
private int size;
// Insert inner class Node<E> here (see Listing 2.1)
/** A Node is the building block for a single-linked list. */
private static class Node<E> {
// Data Fields
/** The reference to the data. */
private E data;
/** The reference to the next node. */
private Node<E> next;
// Constructors
/**
* Creates a new node with a null next field.
* @param dataItem The data stored
*/
private Node(E dataItem) {
data = dataItem;
next = null;
}
/**
* Creates a new node that references another node.
* @param dataItem The data stored
* @param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef) {
data = dataItem;
next = nodeRef;
}
} //end class Node
// Methods
/**
* Insert an item at the rear of the queue.
* @post item is added to the rear of the queue.
* @param item The element to add
* @return true (always successful)
*/
public boolean offer(E item) {
// Check for empty queue.
if (front == null) {
rear = new Node<E>(item);
front = rear;
} else {
// Allocate a new node at end, store item in it, and
// link it to old end of queue.
rear.next = new Node<E>(item);
rear = rear.next;
}
size++;
return true;
}
/**
* Remove the entry at the front of the queue and return it
* if the queue is not empty.
* @post front references item that was second in the queue.
* @return The item removed if successful, or null if not
*/
public E poll() {
E item = peek(); // Retrieve item at front.
if (item == null) {
return null;
}
// Remove item at front.
front = front.next;
size--;
return item; // Return data at front of queue.
}
/**
* Return the item at the front of the queue without removing it.
* @return The item at the front of the queue if successful;
* return null if the queue is empty
*/
public E peek() {
if (size == 0) {
return null;
} else {
return front.data;
}
}
}
---------------------------------------------------------------------------
public interface StackInt<E> {
/**
* Pushes an item onto the top of the stack and returns
* the item pushed.
* @param obj The object to be inserted
* @return The object inserted
*/
E push(E obj);
/**
* Returns the object at the top of the stack
* without removing it.
* @post The stack remains unchanged.
* @return The object at the top of the stack
* @throws EmptyStackException if stack is empty
*/
E peek();
/**
* Returns the object at the top of the stack
* and removes it.
* @post The stack is one item smaller.
* @return The object at the top of the stack
* @throws EmptyStackException if stack is empty
*/
E pop();
/**
* Returns true if the stack is empty; otherwise,
* returns false.
* @return true if the stack is empty
*/
boolean empty();
}
---------------------------------------------------------------------------
public class TestLinkedStack {
public static void main(String args[]) {
LinkedStack<String> movieStack = new LinkedStack<String>();
System.out.println(movieStack.empty());
movieStack.push("The choice");
movieStack.push("How to be single");
System.out.println(movieStack.empty());
System.out.println(movieStack.peek());
System.out.println(movieStack.pop());
System.out.println(movieStack.pop());
}
}
---------------------------------------------------------------------------
public class TestListQueue2 {
public static void main(String[] args){
ListQueue2<String> customerQueue = new ListQueue2<String>(); //create empty queue
customerQueue.offer("Customer1"); //add items to queue
customerQueue.offer("Customer2");
customerQueue.offer("Customer3");
System.out.println("queue size: " + customerQueue.size()); //check queue size
System.out.println("peek return: " + customerQueue.peek()); //peek first item in queue
System.out.println("queue size: " + customerQueue.size());
String who = customerQueue.poll(); //remove first item in queue
System.out.println("poll return: " + who);
System.out.println("queue size: " + customerQueue.size());
}
}
Explanation / Answer
Stack.h
#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
class Stack
{
public:
Stack();
Stack(const Stack ©);
~Stack();
Stack& operator = (const Stack&);
void clear();
int size()const;
bool empty()const;
int top()const;
void push(int item);
void pop();
friend ostream& operator << (ostream& out,const Stack &s);
private:
int *arra;
int aSize;
int aTop;
static int aCapacity;
};
#endif
Stack.cpp
#include "stdafx.h"
#include "stack.h"
#include <iostream>
using namespace std;
int Stack::aCapacity = 8;
Stack::Stack()
{
arra = new int[aCapacity];
aSize = 0;
aTop = -1;
}
//copy constructor
Stack::Stack(const Stack &obj)
{
if(obj.aSize > 0)
{
arra = new int[obj.aSize];
aSize = obj.aSize;
for(int i = 0;i<aSize;i++)
arra[i]=obj.arra[i];
aTop=obj.aTop;
}
else
arra = NULL;
}
//Destructor
Stack::~Stack()
{
delete[] arra;
}
void Stack::push(int value)
{
if(aTop < aCapacity-1)
{
aTop++;
arra[aTop] = value;
aSize++;
}
else
{
aCapacity = aSize * 2;
int *tmp = new int[aCapacity];
for(int i=0;i<aSize;i++)
tmp[i] = arra[i];
delete []arra;
arra = tmp;
aTop++;
arra[aTop] = value;
aSize++;
}
}
void Stack::pop()
{
if(!empty())
{
aTop--;
aSize--;
}
else
throw out_of_range("stack is empty");
}
int Stack::top()const
{
if(empty())
throw out_of_range("stack is empty");
return arra[aTop];
}
bool Stack::empty()const
{
if(aTop == -1)
return true;
else
return false;
}
int Stack::size()const
{
return aSize;
}
void Stack::clear()
{
aSize = 0;
aTop = -1;
}
Stack& Stack::operator = (const Stack &s)
{
return *this;
}
ostream& operator << (ostream& out, const Stack &s)
{
for(int i=0;i<=s.aTop;i++)
out<<" "<<s.arra[i]<<" ";
return out;
}
Main.cpp
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::out_of_range;
int main()
{
cout << "Testing default constructor ";
Stack s1;
cout << "s1: " << s1 << endl;
cout << "s1 size: " << s1.size() << endl;
cout << "s1 is " << ((s1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Testing push() ";
for (int i = 10; i < 80; i+= 10)
s1.push(i);
cout << "s1: " << s1 << endl;
cout << "s1 size: " << s1.size() << endl;
cout << "s1 is " << ((s1.empty()) ? "empty " : "not empty ");
cout << endl;
for (int i = 15; i < 85; i+= 10)
s1.push(i);
cout << "s1: " << s1 << endl;
cout << "s1 size: " << s1.size() << endl;
cout << "s1 is " << ((s1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Testing copy constructor() ";
Stack s2 = s1;
cout << "s1: " << s1 << endl;
cout << "s1 size: " << s1.size() << endl;
cout << "s1 is " << ((s1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Testing top() ";
cout << "Top item of s1: " << s1.top() << endl << endl;
cout << "Testing pop() Top item of s1: ";
while (!s1.empty())
{
cout << s1.top() << ' ';
s1.pop();
}
cout << endl;
cout << "s1 size: " << s1.size() << endl;
cout << "s1 is " << ((s1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Testing assignment operator ";
Stack s3;
s3 = s2;
cout << "s2 (size " << s2.size() << "): " << s2 << endl;
cout << "s3 (size " << s3.size() << "): " << s3 << endl << endl;
cout << "Testing clear() ";
s2.clear();
cout << "s2 (size " << s2.size() << "): " << s2 << endl;
cout << "s3 (size " << s3.size() << "): " << s3 << endl << endl;
cout << "Testing assignment to self and swap ";
s3 = s3;
s2 = s3;
s3.clear();
cout << "s2 (size " << s2.size() << "): " << s2 << endl;
cout << "s3 (size " << s3.size() << "): " << s3 << endl << endl;
cout << "Testing chained assignment ";
Stack s4;
s4 = s3 = s2;
cout << "s2 (size " << s2.size() << "): " << s2 << endl;
cout << "s3 (size " << s3.size() << "): " << s3 << endl;
cout << "s4 (size " << s4.size() << "): " << s4 << endl << endl;
cout << "Testing const correctness ";
const Stack& r4 = s4;
cout << "s4: " << r4 << endl;
cout << "s4 size: " << r4.size() << endl;
cout << "s4 is " << ((r4.empty()) ? "empty " : "not empty ");
cout << "Top item of s4: " << r4.top() << endl << endl;
s1 = r4;
cout << "s1: " << s1 << endl;
cout << "s1 size: " << s1.size() << endl;
cout << "s1 is " << ((s1.empty()) ? "empty " : "not empty ");
cout << endl;
s1.clear();
cout << "Testing top() with empty stack ";
try
{
cout << s1.top() << endl;
}
catch (out_of_range orex)
{
cerr << "Exception: "<< orex.what() << endl << endl;
}
cout << "Testing pop() with empty stack ";
try
{
s1.pop();
}
catch (out_of_range orex)
{
cerr << "Exception: "<< orex.what() << endl;
}
return 0;
}
View comments (1)
More Answers
Anonymous
Anonymous answered this Was this answer helpful?
0
0
949 answers
Dear User,
//stack class
class Stack
{
private:
int *arraystack;
int size;
int top;
public:
//constructor
Stack(int size);
//copy constructor
Stack(const Stack &);
//Destructor
~Stack();
//stack operations
void push(int);
void pop(int &);
bool top() const;
bool empty() const;
bool operator==(const stack<T, Cont>& l,const stack<T, Cont>& r);
bool operator<(const stack<T, Cont>& l, const stack<T, Cont>& r);
bool Stack::size();
};
#endif
//Header file section
#include<iostream>
#include"Stack.h"
using namespace std;
//constructor
Stack::Stack(int s)
{
arraystack = new int[s];
size = s;
top = -1;
}
//copy constructor
Stack::Stack(const Stack &obj)
{
if(obj.size > 0)
arraystack = new int[obj.size];
else
arraystack = NULL;
size = obj.size;
for(int i = 0;i<size;i++)
arraystack[i]=obj.arraystack[i];
top=obj.top;
}
//Destructor
Stack::~Stack()
{
delete[] arraystack;
}
void Stack::push(int value)
{
if(top == size)
arraystack[top] = value;
top++;
}
void Stack::pop(int &value)
{
if(empty())
top--;
value = arraystack[top];
}
bool Stack::empty()
{
if(top == 0)
return true;
else
return false;
}
bool Stack::size(void)
{
return top >= size-1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.