Created
April 19, 2016 06:48
-
-
Save umuturan/2d1b788615137d6af4444cf3cc3a6f95 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public class SimpleLinkedList { | |
| Node head; | |
| public SimpleLinkedList() { | |
| head = null; | |
| } | |
| public void addToHead(String S) { | |
| head = new Node(S, head); | |
| } | |
| public String removeFromHead() { | |
| String res=""; | |
| if (head != null) { | |
| res=head.getData(); | |
| head = head.getNext(); | |
| } else | |
| res="null"; | |
| return res; | |
| } | |
| public boolean isEmpty() { | |
| return (head == null); | |
| } | |
| public String get(int index) { | |
| String res = ""; // this will be returned | |
| Node temp = head; | |
| if (index == 0) | |
| res = head.getData(); | |
| else { | |
| for (int i = 0; i < index; i++) { | |
| temp = temp.getNext(); | |
| } | |
| res = temp.getData(); | |
| } | |
| return res; | |
| } | |
| public String toString() { | |
| String res = ""; | |
| Node temp = head; | |
| if(temp != null && temp.next== null) | |
| res+= temp+"\n"; | |
| while (temp.getNext() != null) { | |
| res += temp + "\n"; | |
| temp = temp.getNext(); | |
| } | |
| return res; | |
| } | |
| public int getSize() { | |
| Node temp = head; | |
| int sum = 0; | |
| while (temp.getNext() != null) { | |
| sum += 1; | |
| } | |
| return sum; | |
| } | |
| public class Node { | |
| // have the reference to next element in the list --> singly linked list | |
| private String data; | |
| private Node next; | |
| // constructor | |
| public Node(String data, Node next) { | |
| this.data = data; | |
| this.next = next; | |
| } | |
| // methods | |
| public String toString() { | |
| return "This node is " + getData() ; | |
| } | |
| public String getData() { | |
| return data; | |
| } | |
| public Node getNext() { | |
| return next; | |
| } | |
| public void setNext(Node n) { | |
| next = n; | |
| } | |
| } | |
| } |
This file contains hidden or 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
| public class SimpleStack { | |
| // properties | |
| SimpleLinkedList list; | |
| // constructor | |
| public SimpleStack() { | |
| list = new SimpleLinkedList(); | |
| } | |
| // methods | |
| public void push(String s) { | |
| list.addToHead(s); | |
| } | |
| public String pop() { | |
| return list.removeFromHead(); | |
| } | |
| public boolean isEmpty() { | |
| return (list.isEmpty()); | |
| } | |
| } |
This file contains hidden or 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
| import java.util.Scanner; | |
| import javax.sound.midi.SysexMessage; | |
| /* | |
| * The user should be able to enter the expression as a String in which each | |
| * character is either an operator (+ or -) or an operand (a value between 0 and 9.) | |
| * A postfix expression has the form "firstNumber secondNumber operator", e.g. "48+" | |
| */ | |
| public class testCalculator { | |
| public static void main(String[] args) { | |
| final String NUMBERS = "0123456789"; | |
| Scanner scan; | |
| String input; | |
| SimpleStack stack; | |
| boolean check; | |
| stack = new SimpleStack(); | |
| scan = new Scanner(System.in); | |
| input = ""; | |
| do { | |
| check = true; | |
| System.out.println(stack.pop()); | |
| System.out.println("please enter the input :"); | |
| input = scan.nextLine(); | |
| for (int i = 0; i < input.length() && check; i++) { | |
| String taken = input.substring(i, i + 1); | |
| if (NUMBERS.indexOf(taken) != -1) { | |
| System.out.println("number entered index: " + i); | |
| stack.push(taken); | |
| } else if (taken.equals("+") || taken.equals("-")) { | |
| System.out.println("operator entered!"); | |
| int last = Integer.valueOf(stack.pop()); | |
| int previous = Integer.valueOf(stack.pop()); | |
| if (taken.equals("+")) { | |
| System.out.println("+ entered"); | |
| String toPush = String.valueOf(previous + last); | |
| stack.push(toPush); | |
| } else if (taken.equals("-")) { | |
| System.out.println("- entered"); | |
| String toPush = String.valueOf(previous - last); | |
| System.out.println("to push calculated :"+toPush); | |
| stack.push(toPush); | |
| } | |
| } else if (!input.equalsIgnoreCase("quit")) { | |
| check = false; | |
| System.out.println("invalid input!"); | |
| } | |
| } | |
| System.out.println("list is \n "+stack.list); | |
| } while (!input.equalsIgnoreCase("quit")); | |
| System.out.println("thanks for using!"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment