Created
June 6, 2015 00:55
-
-
Save gustsu/66b115ccc87e3fde071e to your computer and use it in GitHub Desktop.
Stack Data Structure with 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
/* | |
A singly linked list-based implementation of the stack data structure. | |
Justin Tew | |
*/ | |
public class ListStack implements Stack{ | |
protected int size; //the number of items in the stack | |
protected Node top; //reference to the head node of the list, which is also viewed as the top item in the stack | |
public ListStack(){ | |
size = 0; | |
top = null; | |
} | |
public int size(){ | |
return size; | |
} | |
public int top(){ | |
if(size == 0) | |
return -9999; //-9999 works like null or a flag | |
return top.getElement(); | |
} | |
public void push(int item){ | |
if(item == -9999) | |
return; | |
Node new_node = new Node(item, top); //create the new node whose 'next' link is set to point to the current top node. | |
top = new_node; | |
size ++; | |
} | |
public int pop(){ | |
if(size == 0) | |
return -9999; | |
Node old_top = top; | |
top = top.getNext(); | |
size --; | |
old_top.setNext(null); | |
return old_top.getElement(); | |
} | |
@Override //I wasn't sure if i should override the toString or just make a print method, i went with the first option | |
public String toString() | |
{ | |
StringBuilder s = new StringBuilder(); | |
Node cur; | |
for (cur = top; cur != null; cur = cur.getNext()) | |
{ | |
s.append(cur.getElement() + " \n"); //print the stack out like a stack, the top node on top the next node below it... etc. | |
} | |
return s.toString(); | |
} | |
} | |
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
/* | |
Node of a singly linked list. | |
Justin Tew | |
*/ | |
public class Node{ | |
private int element; //int element | |
private Node next; //the link referencing to the next node in the link list. | |
public Node(int element, Node next){ | |
this.element = element; | |
this.next = next; | |
} | |
public int getElement() { return element; } | |
public Node getNext() { return next; } | |
public void setElement(int element) { this.element = element; } | |
public void setNext(Node next) { this.next = next; } | |
} | |
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
/* | |
The interface for the stack data structure. | |
Justin Tew | |
*/ | |
public interface Stack{ | |
public int size(); // return the number of items currently in the stack. | |
public int top(); // return the top item in the stack, but do not remove it from the stack. | |
public void push(int item); //push the new item into the top of the stack. | |
public int pop(); // return and remove the top time in the stack. | |
} | |
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
/* | |
Justin Tew | |
2/2/2014 | |
[email protected] | |
Program 4 | |
*/ | |
import java.util.Scanner; | |
import java.io.*; | |
public class Test_Stack | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
Stack stack = new ListStack(); //Creates a new ListStack referance | |
if (args.length == 1) | |
{ | |
Scanner file = new Scanner(new File(args[0])); //Opens the file | |
while (file.hasNextLine() != false) | |
{ | |
if (file.hasNextInt() == true) //if the next line is an int | |
{ | |
int num = file.nextInt(); //add the int to the stack | |
stack.push(num); | |
if (file.hasNextLine()) //move on to the next line if there is one | |
file.nextLine(); | |
} | |
else if (file.hasNextLine() == true && stack.size() >= 2) //if the file has another line and a stack size of 2 or more | |
{ | |
String operator = file.nextLine(); //the next line must be a operator if it is not a number | |
if (operator.equals("+")) //+ PLUS | |
{ | |
int num1 = stack.pop(); | |
int num2 = stack.pop(); | |
int result = add(num1,num2); | |
stack.push(result); | |
} | |
if (operator.equals("-")) //- MINUS | |
{ | |
int num1 = stack.pop(); | |
int num2 = stack.pop(); | |
int result = subtract(num1,num2); | |
stack.push(result); | |
} | |
if (operator.equals("*")) //* TIMES | |
{ | |
int num1 = stack.pop(); | |
int num2 = stack.pop(); | |
int result = multiply(num1,num2); | |
stack.push(result); | |
} | |
if (operator.equals("/")) // / divide | |
{ | |
int num1 = stack.pop(); | |
int num2 = stack.pop(); | |
int result = divide(num1,num2); | |
stack.push(result); | |
} | |
} | |
else if (file.hasNextInt() == false && stack.size() <= 1) //if its an operation and stack size is 1 or less | |
{ | |
System.out.println("Error: Cannot Perform Any Operation On One Or Less Operand..."); | |
System.out.println("Exiting loop... stopping reading file"); | |
break; //i had trouble picking between file.nextLine(); or break; i just went with break so these lines don't print out more than once | |
} | |
} | |
file.close(); | |
System.out.println(stack); //Print the stack using the toString method i wrote | |
} | |
else | |
{ | |
System.out.println("Incorrect number of parameters... Should be 1"); | |
} | |
} | |
public static int add(int num1, int num2) | |
{ | |
return num1 + num2; | |
} | |
public static int subtract(int num1, int num2) | |
{ | |
return num2 - num1; | |
} | |
public static int multiply(int num1, int num2) | |
{ | |
return num1 * num2; | |
} | |
public static int divide(int num1, int num2) | |
{ | |
return num2 / num1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment