Stack.java

// Stack.java
// demonstrates stacks
// to run this program: C>java StackApp
import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class theStack
{
int maxsize;
double arr[];
int top;
public theStack(int s) // constructor
{
arr = new double[s];
top = 0;
}
public void push(double value)
{
arr[top] = value;
top++;
}
public double pop()
{
top – -;
return arr[top];
}
public double peak()
{
return arr[top];
}
public boolean isEmpty()
{
if(top == 0)
{
return true;
}
return false;
}

public boolean isFull()
{
if(top == maxsize)
{
return true;
}
return false;
}

}
class Stack
{
public static void main(String[] args)
{
theStack theStack1 = new theStack(10); // make new stack
theStack1.push(20); // push items onto stack
theStack1.push(40);
theStack1.push(60);
theStack1.push(80);
while( !theStack1.isEmpty() ) // until it’s empty,
{ // delete item from stack
double value = theStack1.pop();
System.out.print(value); // display it
System.out.print(” “);
} // end while
System.out.println(“”);
} // end main()
} // end class Stack

stack

Advertisements

2 thoughts on “Stack.java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s