-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #832 from nuduja/master
Stack data Structure in Java Language added
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
Program's_Contributed_By_Contributors/Java_Programs/Misc/stackArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class stackjava { | ||
public static void main(String[] args) { | ||
StackX theStack = new StackX(10); // create a stack with max size 10 | ||
|
||
theStack.push(30); // insert given items | ||
theStack.push(80); | ||
theStack.push(100); | ||
theStack.push(25); | ||
|
||
//To peak the the last element pushed | ||
System.out.println(theStack.peek()); | ||
|
||
while(!theStack.isEmpty()) { // until it is empty, delete item from stack | ||
|
||
double val = theStack.pop(); | ||
System.out.print(val); | ||
System.out.print(" "); | ||
} | ||
} | ||
} | ||
|
||
//Implementation of Stack Class | ||
class StackX { | ||
|
||
private int maxSize; // size of stack array | ||
private double[] stackArray; | ||
private int top; //top of the stack | ||
|
||
public StackX(int s) { // constructor | ||
|
||
maxSize = s; // set array size | ||
stackArray = new double[maxSize]; | ||
top = -1; // no items | ||
} | ||
|
||
public void push(double j) { | ||
|
||
// check whether stack is full | ||
if (top == maxSize - 1) | ||
System.out.println("Stack is full"); | ||
else | ||
stackArray[++top] = j; | ||
} | ||
|
||
public double pop() { | ||
if (top == -1) | ||
return -99; | ||
else | ||
return stackArray[top--]; | ||
} | ||
|
||
public double peek() { | ||
if (top == -1) | ||
return -99; | ||
else | ||
return stackArray[top]; | ||
} | ||
|
||
//Stack isEmpty method | ||
public boolean isEmpty(){ | ||
return(top == -1); | ||
} | ||
|
||
//Stack isEmpty method | ||
public boolean isFull(){ | ||
return (top == (maxSize -1)); | ||
} | ||
} |