Question

Part 1 - MyStack.java

Create a Stack class named MyStack.java that stores data of type T (generics?) . Specifically, implement push(T), pop(), peek(), size(), and isEmpty. You must use a linked list with a sentinel node - include a separate Node.java class with your submission.

Part 2 - MyStackFunctions.java

Create a StackFunctions class that accomplishes the following as static methods:

sumNegative : Given a MyStack of Integers, sums and returns the total of all negative values in the stack as an int. For example, a stack containing 5, -1, -2, 3, and -20 would result in a return -23. The input stack should be in its original state at the end of the method call (you will need to push things back onto the input stack carefully after processing, I suggest using a 2nd stack variable).

clean : Given a MyStack of Strings, splits all Strings in the MyStack with spaces into multiple Strings in the stack. For example, a StackADT of "Bob", "Jim is home", and "Hello World" would be changed to "Bob", "Jim", "is", "home", "Hello", and "World". Order of the items on the stack does not matter but you should alter the MyStack passed as a parameter (you can then return the stack or have a void return type). (maybe use String class's split() method?)

main : Does two example runs of each of sumNegative and clean on different input MyStacks for each example (so a total of 4 MyStacks, 2 MyStack<Integer> and 2 MyStack<String>).

Language: Java
Compiler: Eclipse

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class MyStack<T> {

private Node<T> head;
private int size;

public MyStack()
{
head = null;
size = 0;
}

public void push(T data)
{
Node<T> newNode = new Node<T>();
newNode.data = data;
newNode.next = head;
head = newNode;
size++;
}

public T pop()
{
Node<T> temp = head;
if(head != null)
{
head = head.next;
size--;
return temp.data;
}
return null;
}

public T peek()
{
if(head != null)
return head.data;
else
return null;
}

public int size()
{
return size;
}

public boolean isEmpty()
{
return size==0;
}
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Cylinder Containers in Java
Homework Solution
$38.00
Java
Programming
Codes
Algorithms
Computer Science
Statements
Variables
Loops
Input
Output
Integers
Strings
Geometry
Mathematics
Cylinder
Radius
Finance
Costs
Containers
Companies
Functions
Roman Numerals in Java
Homework Solution
$35.00
Java
Programming
Codes
Algorithms
Computer Science
Statements
Variables
Loops
Roman Numerals
Integers
Messages
Input
Output
Arabic Format
Strings
Integers
Java Problems: Student Details & Fibonacci Series
Homework Solution
$20.00
Java
Programming
OOP
Computer Science
Fibonacci Series
Students
Classes
Instances
Loops
Conditions
Statements
Variables
Integers
Mathematics
Gradebook Using Java Programming
Homework Solution
$20.00
Java
Programming
Computer Science
Random Numbers
Scores
Students
Input
Output
Loops
Statements
Conditions
Average Values
Variables
Mathematics
Class Money in Java
Homework Solution
$20.00
Java
Programming
Coding
Money
UML Diagram
Computer Science
Dollars
Cents
Parameters
Statements
Methods
Strings
Integers
Finance
Arithmetic Operations
Personal Name List in Java
Homework Solution
$8.00
Java
Programming
Computer Science
Codes
Algorithms
Personal Name List
Records
Errors
Arrays
Items
Variables
Statements
Characters
Moving Elements
Input
Output
Get help from a qualified tutor
Live Chats