Last active
August 29, 2015 14:08
-
-
Save JuxhinDB/693a8089d0d792e60351 to your computer and use it in GitHub Desktop.
A Stack class without using arrays
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
/* | |
* This could have indeed been written in a much neater manner.. the direct variable assignment is something I never | |
* enjoyed doing but I just wanted to get things to work without alot of boilerplate code. | |
* | |
* No, I did not approach this challenge using any vector list, would've been easier. Same goes for one-dimensional | |
* arrays.. | |
*/ | |
public class Stack { | |
Element top; | |
int size; | |
//Main method on top just to make it easier to read | |
public static void main(String[] args) { | |
//Stack size may be from 1..10, +1 is there to change the range from 0..9 to the desired 1..10 | |
int n = (int)(Math.random() * 10) + 1; | |
//New instance of my own SmartStackList object | |
Stack stack = new Stack(); | |
//No curly-braces as it's a single-line loop, this applies to all single-line conditional statements | |
// even though they're more often than not frowned upon.. | |
for(int i = 0; i < n; i++) | |
stack.push(new Element(randInt())); | |
//Uses displayInStackOrder method from the SmartStackList class | |
stack.displayInStackOrder(); | |
//Prints out the stack's current size followed by a new lines | |
System.out.println("Size: " + stack.size() + "\n"); | |
} | |
//Just a default constructor.. you know the drill | |
Stack() { | |
top = null; | |
size = 0; | |
} | |
//Never used it in this case, didn't even test but the code seems to make sense so yea, expect it to be buggy | |
Element pop() { | |
Element pop = top; | |
top = pop.next; | |
top.previous = null; | |
size--; | |
return pop; | |
} | |
void push(Element element) { | |
//Meaning TOS isn't 'empty' | |
//The top's previous element is initially null, is now being assigned with the 'element' variable that's passed | |
// as an argument.. | |
if(top != null) | |
top.previous = element; | |
//Therefore, the new TOS' next element(further out of stack) is the previous element, which was 'top' | |
// which is then being assigned to element | |
element.next = top; | |
top = element; | |
size++; | |
} | |
//Display's stack in stack order, righter-most | |
void displayInStackOrder() { | |
System.out.print("Stack order: "); | |
Element element = top; | |
while(element != null) { | |
System.out.print(element); | |
element = element.next; | |
if(element != null) { | |
System.out.print(", "); | |
} | |
} | |
System.out.println("\n"); | |
} | |
//Just a convenience method.. | |
static int randInt() { | |
return (int)(Math.random() * 100); | |
} | |
//Stack's current size | |
int size() { | |
return size; | |
} | |
} | |
//The Element to be be put inside the stack, obviously may only be an int.. | |
class Element { | |
Element next; | |
Element previous; | |
int value; | |
Element(int value) { | |
next = null; | |
previous = null; | |
this.value = value; | |
} | |
@Override | |
public String toString() { | |
return String.valueOf(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment