/** A class of stacks whose entries are stored in a chain of nodes. Implement al
ID: 3759655 • Letter: #
Question
/**
A class of stacks whose entries are stored in a chain of nodes.
Implement all methods in MyStack class
Main Reference : text book or class notes
Do not change or add data fields
*/
package PJ2;
public class MyStack<T> implements StackInterface<T>
{
// Data fields
private Node<T> topNode; // references the first node in the chain
private int numberOfEntries;
public MyStack()
{
// add stataments
} // end default constructor
public void push(T newEntry)
{
// add stataments
} // end push
public T peek()
{
// add stataments
return null;
} // end peek
public T pop()
{
return null;
} // end pop
public boolean empty()
{
// add stataments
return false;
} // end empty
public int size()
{
// add stataments
return 0;
} // end isEmpty
public void clear()
{
// add stataments
} // end clear
public String toString()
{
// add stataments
return "";
}
/****************************************************
Do not modify: Stack test
Expected output:
OK: stack is empty
Push 3 data: 10, 30, 50
Print stack [50,30,10,]
OK: stack size is 3
OK: peek stack top is 50
OK: stack is not empty
Pop 2 data: 50, 30
Print stack [30,10,]
Print stack [10,]
OK: stack pop data is 30
Clear stack
Print stack []
******************************************************/
public static void main (String args[])
{
MyStack<Integer> s = new MyStack<Integer>();
if (s.empty())
System.out.println("OK: stack is empty");
else
System.out.println("Error: stack is not empty");
s.push(10);
s.push(30);
s.push(50);
System.out.println("Push 3 data: 10, 30, 50");
System.out.println("Print stack " + s);
if (s.size() == 3)
System.out.println("OK: stack size is 3");
else
System.out.println("Error: stack size is " + s.size());
if (s.peek() == 50)
System.out.println("OK: peek stack top is 50");
else
System.out.println("Error: peek stack top is " + s.size());
if (!s.empty())
System.out.println("OK: stack is not empty");
else
System.out.println("Error: stack is empty");
System.out.println("Pop 2 data: 50, 30");
s.pop();
System.out.println("Print stack " + s);
int data=s.pop();
System.out.println("Print stack " + s);
if (data == 30)
System.out.println("OK: stack pop data is 30");
else
System.out.println("Error: stack pop data is " + data);
System.out.println("Clear stack");
s.clear();
System.out.println("Print stack " + s);
}
} // end Stack
Explanation / Answer
public class MyStack<T> implements StackInterface<T>
{
// Data fields
private Node<T> topNode; // references the first node in the chain
private int numberOfEntries;
public MyStack()
{
topNode=NULL;
numberOfEntries=0;
// add stataments
} // end default constructor
public void push(T newEntry)
{
newEntry->next=topNode;
topNode=newEntry;
numberOfEntries++;
// add stataments
} // end push
public T peek()
{
if(numberOfEntries!=0)
return topNode;
// add stataments
return null;
} // end peek
public T pop()
{
if(numberOfEntries!=0){
Node<T> tempNode=topNode;
topNode=topNode->next;
return tempNode;
}
return null;
} // end pop
public boolean empty()
{
// add stataments
if(numberOfEntries==0)
return true;
return false;
} // end empty
public int size()
{
if(numberOfEntries!=0)
return numberOfEntries;
// add stataments
return 0;
} // end isEmpty
public void clear()
{
for(int i=0;i<numberOfEntries;i++){
Node<T> tempNode=topNode;
topNode=topNode->next;
delete tempNode;
}
// add stataments
} // end clear
public String toString()
{
String s;
for(int i=0;i<numberOfEntries;i++){
Node<T> tempNode=topNode;
s+=tempNode.toString();
topNode=topNode->next;
}
// add stataments
return "";
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.