The Artima Developer Community
Sponsored Link

Java Answers Forum
Stacks

1 reply on 1 page. Most recent reply: Sep 21, 2003 7:12 PM by Senthoorkumaran Punniamoorthy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Isa

Posts: 6
Nickname: isa
Registered: Sep, 2003

Stacks Posted: Sep 21, 2003 3:14 PM
Reply to this message Reply
Advertisement
Say if i have created two different stacks,and any integer over 10 goes in one stack(bigStack). If less than 10 goes in one stack(theStack). The program is already working where it will resize if its full,but I am trying to put the integers in the right Stack. This is what I have so far.

import java.io.*;
//////////////////////////////////////////////////////////////
class StackX
{
private int maxSize; // size of stack array
private int[] stackArray;
private int top; // top of stack
//--------------------------------------------------------------
public StackX(int s) // constructor
{
maxSize = s; // set array size
stackArray = new int[maxSize]; // create array
top = -1; // no items yet

}
//--------------------------------------------------------------
public void push(int j) // put item on top of stack
{
stackArray[++top] = j; // increment top, insert item
if(top == stackArray.length-1) // stack is full
{
maxSize = maxSize*2;
int [] newArray = new int[maxSize];
for(int i =0; i<= top; i++)newArray = stackArray;

stackArray = newArray;

System.out.println("Big Stack Increased To "+maxSize);
}

}


//---------------------------------------------------------- ----
public int pop() // take item from top of stack
{
return stackArray[top--]; // access item, decrement top
}
//--------------------------------------------------------------
public int peek() // peek at top of stack
{
return stackArray[top];
}
//---------------------------------------------------------- ----
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
//--------------------------------------------------------------
public boolean isFull() // true if stack is full
{
return (top == maxSize-1);

}

//------------------------------------------------------------- -


} // end class StackX

////////////////////////////////////////////////////////////////

class prog1

{

public static void main(String[] args)throws IOException
{


StackX theStack = new StackX(5); // make new stack
StackX bigStack = new StackX(5);

System.out.println("Enter any number of integers," + "one per line:");

BufferedReader keyRead=new BufferedReader(new InputStreamReader(System.in));
int next=Integer.parseInt(keyRead.readLine());

while(next>-1)

{

theStack.push (next);
next=Integer.parseInt(keyRead.readLine());

}

while( !theStack.isEmpty() ) { // until stack is empty,
// delete top item from stack
int value = theStack.pop();
System.out.print(value); // display it
System.out.print(" ");
} // end while


System.out.println("");

} // end main()

} // end class program1


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Stacks Posted: Sep 21, 2003 7:12 PM
Reply to this message Reply
You could have continued your questions in your previous post about Integer Stacks itself. However,

int next= 0;
while(next>-1) {
   next = Integer.parseInt(keyRead.readLine());
   if (next > 10)
     bigStack.push (next);
   else
     theStack.push(next);
}

Flat View: This topic has 1 reply on 1 page
Topic: Applet & Serverlet on Linux Previous Topic   Next Topic Topic: Integer Stacks

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use