import java.util.EmptyStackException;
import java.util.Iterator;
public class Lab07P2Wrapper {
private static boolean equals(int[] arr1, int[] arr2) {
if(arr1.length != arr2.length) return false;
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] example = {-1, 1, 3, 9, 11, 11, 11, 15};
int[] input = {9, 11, 15, 11, 1, -1, 3, 11};
int[] result = stackSort(input);
if(equals(example, result)) {
System.out.println(“Test Passed!”);
} else {
System.out.print(“Test Failed, expected: “);
for(int i = 0; i < example.length; i++) {
if(i == 0) System.out.print(“{” + example[i] + “, “);
else if(i == example.length – 1) System.out.print(example[i] + “}”);
else System.out.print(example[i] + “, “);
}
for(int i = 0; i < result.length; i++) {
if(i == 0) System.out.print(“, got {” + result[i] + “, “);
else if(i == result.length – 1) System.out.print(result[i] + “}”);
else System.out.print(result[i] + “, “);
}
}
}
public static interface Stack<E> {
public void push(E newEntry);
public E pop();
public E top();
public boolean isEmpty();
public int size();
public void clear();
}
public static class SinglyLinkedStack<E> implements Stack<E> {
private static class Node<E> {
private E element;
private Node<E> next;
public Node(E elm, Node<E> next) {
this.element = elm;
this.next = next;
}
public Node(E data) {
this(data, null);
}
public Node() {
this(null, null);
}
public E getElement() {
return element;
}
public Node<E> getNext() {
return next;
}
public void setElement(E elm) {
this.element = elm;
}
public void setNext(Node<E> next) {
this.next = next;
}
public void clear() { // no need to return data
element = null;
next = null;
}
}
// instance variables for the stack object
private Node<E> header; //Note that this is NOT a dummy header
private int currentSize;
public SinglyLinkedStack() {
header = new Node<>();
currentSize = 0;
}
@Override
public void push(E newEntry) {
Node<E> nNode = new Node<>(newEntry, header.getNext());
header.setNext(nNode);
currentSize++;
}
@Override
public E pop() {
E etr = top();
Node<E> ntr = header.getNext();
header.setNext(ntr.getNext());
currentSize–;
ntr.clear();
ntr = null;
return etr;
}
@Override
public E top() {
if (isEmpty())
throw new EmptyStackException();
return header.getNext().getElement();
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public int size() {
return currentSize;
}
@Override
public void clear() {
while (size() > 0) pop();
}
}
/**
* Implement a non-member method for the Stack ADT called stackSort.
* The function takes as a parameter an array of integers and returns the array sorted in increasing order.
*
* For example consider we have an array A = {9, 11, 15, 11, 1, -1, 3, 11}
* In order to sort values, we will use two stacks which will be called the left and right stacks.
* The values in the stacks will be sorted (in non-descending order) and the values in the left stack
* will all be less than or equal to the values in the right stack.
*
* The following example illustrates a possible state for our two stacks:
*
* left right
* [ ] [ ]
* [ ] [ 9]
* [ 3] [11]
* [ 1] [11]
* [-1] [15]
*
* Notice that the values in the left stack are sorted so that the smallest value is at the bottom of the stack.
* The values in the right stack are sorted so that the smallest value is at the top of the stack.
* If we read the values up the left stack and then down the right stack, we get:
* A = {-1, 1, 3, 9, 11, 11, 11, 15}
* which is in sorted order.
*
*
* Consider the following cases, using the example shown above as a point of reference, to help you design your algorithm:
* 1) If we were to insert the value 5, it could be added on top of either stack and the collection would remain sorted.
* What other values, besides 5, could be inserted in the example without having to move any values?
*
* 2) If we were to insert the value 0, some values must be moved from the left stack to the right stack before we could actually insert 0.
* How many values must actually be moved?
*
* 3) If we were to insert the value 11, first some values must be moved from the right stack to the left stack.
* How many values must actually be moved?
* What condition should we use to determine if enough values have been moved in either of the previous two cases?
*
* YOU MUST USE TWO STACKS, IMPLEMENTATIONS THAT USE Arrays.sort();
* OR ANY SORTING ALGORITHM (BubbleSort, SelectionSort, etc.) WILL NOT BE GIVEN CREDIT
*
* @param array
* @return Sorted array using two stacks
*/
public static int[] stackSort(int[] array) {
/*ADD YOUR CODE HERE*/
return null;
}
}
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
Custom Writings Help is a Quality-Oriented Company in Online Writing as a result of hiring exceptional professionals to execute clients' tasks.
Essays
At Custom Writings Help,We understand the struggle of research paper writing, and that is why at Custom WritingS Help, we are all out to help you. We pride ourselves on having a team of clinical writers. The stringent and rigorous vetting process ensures that only the 'BEST' Writers are chosen for the job. We have highly qualified Ph.D. and MA writers working with us; we equally offer these experienced writers specific bonuses and incentives to make them deliver highly original, unique, and informative content at reasonably low prices.
Admissions
Thesis Writing Service
Worlwide, Many Masters Students are struggling with Thesis Completion. A thesis is likely to be the longest and most challenging piece of work a student has ever completed. However, unlike essays and other assignments, a student can pick a particular interest topic and work on their initiative. Fortunately, we are there for you. At Custom Writings Help, you are assured of an authentic, imaginative, informative, linguistically great, and advantageous thesis that adheres to all your needs. So, why continue considering different writers when you have discovered the best in the field?
Editing
Custom Papers
Not a single student can avoid writing custom papers. However, a total lack of experience, skills, and time makes it very hard to produce a superb writing piece. Therefore, if you are seeking professional help, turn to us. Our specialized and experienced writers compose a variety of model papers, including custom essays, college term papers, research papers, book reports, MBA essays, executive summaries, dissertations, Ph.D. theses, admission essays, and research proposals for college and university students at any level.
Coursework
Essay Writing
Most of the students disregard the critical principles of essay writing and compose papers below sensible guidelines. Therefore, with Custom Writings Help, one should not worry about his/her essay. Our Writers compose informative and engaging content on all complexities and topics. We write meaningful and smart essays while prioritizing all aspects that bring about a good grade, such as impeccable grammar, proper structure, zero-plagiarism, and conformance to guidelines.
Coursework
Coourse Work Writing
Don't let the seemingly never-ending onslaught of writing assignments get you down. If you are looking where to get course work assistance online, the writers at Custom Writings Help are here to assist you with all of your writing needs. We undertake to unique delivery of papers that meet the professor's requirements. The content is proofread, edited, and checked plagiarism before submission to customers. No matter how big or small your work is, we will deliver on time. Try US Now! !
Coursework
Dissertation Writing Service
High-Quality Dissertation Writing Services are rare. They require Ph.D. academicians – not easily found. However, are an exception. The years, time, and resources we have invested in the dissertation world has given us a competitive advantage over others. Choose to come to Custom Writings Help; You will find perfect Ph.D. consultants who have written hundreds of dissertations theses ready to help you. Let our dissertation-writing services help you craft your dissertation, for you are assured we will give you the results.