I need a help in my method equalStack in my class StackClass. - public class StackClass
-
{
-
private int maxStackSize; //variable to store the maximum
-
//stack size
-
private int stackTop; //variable to point to the top
-
//of the stack
-
private DataElement[] list; //array of reference variables
-
-
//default constructor
-
//Create an array of size 100 to implement the stack.
-
//Postcondition: The variable list contains the base
-
// address of the array, stackTop = 0, and
-
// maxStackSize = 100.
-
public StackClass()
-
{
-
maxStackSize = 100;
-
stackTop = 0; //set stackTop to 0
-
list = new DataElement[maxStackSize]; //create the array
-
}//end default constructor
-
-
//constructor with a parameter
-
//Create an array of size stackSize to implement the stack.
-
//Postcondition: The variable list contains the base
-
// address of the array, stackTop = 0, and
-
// maxStackSize = stackSize.
-
public StackClass(int stackSize)
-
{
-
if(stackSize <= 0)
-
{
-
System.err.println("The size of the array to implement "
-
+ "the stack must be positive.");
-
System.err.println("Creating an array of size 100.");
-
-
maxStackSize = 100;
-
}
-
else
-
maxStackSize = stackSize; //set the stack size to
-
//the value specified by
-
//the parameter stackSize
-
stackTop = 0; //set stackTop to 0
-
list = new DataElement[maxStackSize]; //create the array
-
}//end constructor
-
-
//Method to initialize the stack to an empty state.
-
//Postcondition: stackTop = 0
-
public void initializeStack()
-
{
-
for(int i = 0; i < stackTop; i++)
-
list[i] = null;
-
stackTop = 0;
-
}//end initializeStack
-
-
-
//Method to determine whether the stack is empty.
-
//Postcondition: Returns true if the stack is empty;
-
// otherwise, returns false.
-
public boolean isEmptyStack()
-
{
-
return (stackTop == 0);
-
}//end isEmptyStack
-
-
//Method to determine whether the stack is full.
-
//Postcondition: Returns true if the stack is full;
-
// otherwise, returns false.
-
public boolean isFullStack()
-
{
-
return (stackTop == maxStackSize);
-
}//end isFullStack
-
-
//Method to add newItem to the stack.
-
//Precondition: The stack exists and is not full.
-
//Postcondition: The stack is changed and newItem
-
// is added to the top of stack.
-
// If the stack is full, the method throws
-
// StackOverflowException
-
public void push(DataElement newItem) throws StackOverflowException
-
{
-
if(isFullStack())
-
throw new StackOverflowException();
-
-
list[stackTop] = newItem.getCopy(); //add newItem at the
-
//top of the stack
-
stackTop++; //increment stackTop
-
}//end push
-
-
-
//Method to return the top element of the stack.
-
//Precondition: The stack exists and is not empty.
-
//Postcondition: If the stack is empty, the method throws
-
// StackUnderflowException; otherwise, a
-
// reference to a copy of the top element
-
// of the stack is returned.
-
public DataElement top() throws StackUnderflowException
-
{
-
if(isEmptyStack())
-
throw new StackUnderflowException();
-
-
DataElement temp = list[stackTop - 1].getCopy();
-
-
return temp;
-
}//end top
-
-
//Method to remove the top element of the stack.
-
//Precondition: The stack exists and is not empty.
-
//Postcondition: The stack is changed and the top
-
// element is removed from the stack.
-
// If the stack is empty, the method throws
-
// StackUnderflowException
-
public void pop() throws StackUnderflowException
-
{
-
if(isEmptyStack())
-
throw new StackUnderflowException();
-
-
stackTop--; //decrement stackTop
-
list[stackTop] = null;
-
}//end pop
-
-
-
//Method to make a copy of otherStack.
-
//This method is used only to implement the methods
-
//copyStack and copy constructor
-
//Postcondition: A copy of otherStack is created and
-
// assigned to this stack.
-
private void copy(StackClass otherStack)
-
{
-
list = null;
-
System.gc();
-
-
maxStackSize = otherStack.maxStackSize;
-
stackTop = otherStack.stackTop;
-
-
list = new DataElement[maxStackSize];
-
-
//copy otherStack into this stack
-
for(int i = 0; i < stackTop; i++)
-
list[i] = otherStack.list[i].getCopy();
-
}//end copy
-
-
-
//copy constructor
-
public StackClass(StackClass otherStack)
-
{
-
copy(otherStack);
-
}//end constructor
-
-
//Method to make a copy of otherStack.
-
//Postcondition: A copy of otherStack is created and
-
// assigned to this stack.
-
public void copyStack(StackClass otherStack)
-
{
-
if(this != otherStack) //avoid self-copy
-
copy(otherStack);
-
}//end copyStack
-
-
-
//Method to determine if a given stack has the
-
//same content as the current one
-
//Postcondition: Returns true if the two stacks have
-
// same content, false otherwise.
-
public boolean equalStack(StackClass otherStack)
-
{
-
if (this.isEmptyStack() == otherStack.isEmptyStack())
-
{
-
try
-
{
-
while(top().compareTo( otherStack.top())==0)
-
{
-
pop();
-
otherStack.pop();
-
}
-
return false ;
-
}
-
catch (StackUnderflowException e)
-
{
-
return true;
-
}
-
}
-
else
-
return false;
-
}
-
}
- import java.util.*;
-
-
public class TestProgStack
-
{
-
public static void main(String[] args)
-
{
-
StackClass stack1 = new StackClass();
-
StackClass stack2 = new StackClass();
-
-
try
-
{
-
stack1.push(new IntElement(5));
-
stack1.push(new IntElement(7));
-
stack1.push(new IntElement(1));
-
stack1.push(new IntElement(3));
-
stack1.push(new IntElement(2));
-
}
-
catch(StackOverflowException sofe)
-
{
-
System.out.println(sofe.toString());
-
System.exit(0);
-
}
-
-
stack1.copyStack(stack1);
-
-
System.out.print("stack1 elements are: ");
-
-
while (!stack1.isEmptyStack())
-
{
-
System.out.print(stack1.top() + " ");
-
stack1.pop();
-
}
-
System.out.println();
-
-
try
-
{
-
stack2.push(new IntElement(1));
-
stack2.push(new IntElement(3));
-
stack2.push(new IntElement(2));
-
stack2.push(new IntElement(5));
-
stack2.push(new IntElement(7));
-
stack2.push(new IntElement(0));
-
-
}
-
catch(StackOverflowException sofe)
-
{
-
System.out.println(sofe.toString());
-
System.exit(0);
-
}
-
-
stack2.copyStack(stack2);
-
-
System.out.print("stack2 elements are: ");
-
-
while (!stack2.isEmptyStack())
-
{
-
System.out.print(stack2.top() + " ");
-
stack2.pop();
-
}
-
System.out.println();
-
-
if(stack1.equalStack(stack2))
-
System.out.println("The two Stacks have the same content");
-
else
-
System.out.println("The two Stacks have NOT the same content");
-
-
}
-
}
5 1987 - //How about making use of the size of the stack
-
public boolean equalStack(StackClass otherStack) {
-
if (getSize() != otherStack.getSize()) { //if stack lengths are not equal return false
-
return false;
-
}
-
else {
-
while(!isEmpty()) {
-
if(top().compareTo(otherStack.top()) != 0) {
-
return false;
-
}
-
else {
-
try {
-
pop();
-
otherStack.pop();
-
}
-
catch (StackUnderflowException e) {//This will no longer be neccessary?
-
return true;
-
}
-
-
}
-
}
-
}
-
return true;
-
}
-
[quote=r035198x] - //How about making use of the size of the stack
-
public boolean equalStack(StackClass otherStack) {
-
if (getSize() != otherStack.getSize()) { //if stack lengths are not equal return false
-
return false;
-
}
-
else {
-
while(!isEmpty()) {
-
if(top().compareTo(otherStack.top()) != 0) {
-
return false;
-
}
-
else {
-
try {
-
pop();
-
otherStack.pop();
-
}
-
catch (StackUnderflowException e) {//This will no longer be neccessary?
-
return true;
-
}
-
-
}
-
}
-
}
-
return true;
-
}
-
[/QUOTE
I've tried iy but it's not working.
[quote=satan]
- //How about making use of the size of the stack
-
public boolean equalStack(StackClass otherStack) {
-
if (getSize() != otherStack.getSize()) { //if stack lengths are not equal return false
-
return false;
-
}
-
else {
-
while(!isEmpty()) {
-
if(top().compareTo(otherStack.top()) != 0) {
-
return false;
-
}
-
else {
-
try {
-
pop();
-
otherStack.pop();
-
}
-
catch (StackUnderflowException e) {//This will no longer be neccessary?
-
return true;
-
}
-
-
}
-
}
-
}
-
return true;
-
}
-
[/QUOTE
I've tried iy but it's not working.
Where is it not working? Is it not compiling? Please explain the problem more clearly.
[quote=r035198x]
Where is it not working? Is it not compiling? Please explain the problem more clearly.
Yes, it's not compiling. it can not find the symbol getSize in my method StackClass
[quote=satan]
Yes, it's not compiling. it can not find the symbol getSize in my method StackClass
Ah now surely you should have realised that you need that method for your stack and put it in. Write a method that returns the number of items in the stack and call it getSize, if you already have one use it in place of getSize.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: mike |
last post by:
Hello,
After trying to validate this page for a couple of days now I was
wondering if someone might be able to help me out. Below is a list of
snippets where I am having the errors.
1. Line 334,...
|
by: John Flynn |
last post by:
hi all
i'm going to be quick i have an assignment due which i have no idea how to
do. i work full time so i dont have the time to learn it and its due date
has crept up on me ..
As follows:...
|
by: xunling |
last post by:
i have a question about answering ..... this topic is "need help"
what do i have to write at te topic line, !after i have klicked the
"answer message" button
ive tried many possibilities,...
|
by: sk |
last post by:
I have an applicaton in which I collect data for different parameters
for a set of devices. The data are entered into a single table, each
set of name, value pairs time-stamped and associated with...
|
by: Timothy Shih |
last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I
wrote a simple function which takes in 2 buffers (one a byte buffer, one a
char buffer) and copies the contents of the byte...
|
by: Cheryl Langdon |
last post by:
Hello everyone,
This is my first attempt at getting help in this manner. Please
forgive me if this is an inappropriate request.
I suddenly find myself in urgent need of instruction on how to...
|
by: pamelafluente |
last post by:
I am still working with no success on that client/server
problem. I need your help. I will submit simplified versions
of my problem so we can see clearly what is going on.
My model:
A client...
|
by: skumar434 |
last post by:
i need to store the data from a data base in to structure
.............the problem is like this ....suppose there is a data base
which stores the sequence no and item type etc ...but i need only...
|
by: U S Contractors Offering Service A Non-profit |
last post by:
Brilliant technology helping those most in need Inbox
Reply
U S Contractors Offering Service A Non-profit
show details
10:37 pm (1 hour ago)
Brilliant technology helping those most in need ...
|
by: mike |
last post by:
I help manage a large web site, one that has over
600 html pages... It's a reference site for ham radio
folks and as an example, one page indexes over
1.8 gb of on-line PDF documents.
The site...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |