- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package stackparser;
- import java.util.ArrayList;
- /**
- *
- * @author tyler
- */
- public class GenericManagerStacks<T>{
- protected MyLinkManager<T> myStack;
- protected int number;
- //constructor
- public GenericManagerStacks() {
- number = 0; // number of indexes in myStack
- myStack = new MyLinkManager<T>; //create initial array list of 100
- }
- //returns the number of indexes in myStack
- public int getNumber() {
- return number;
- }
- //pushes a node on the stack -- adds to the top of the stack
- public int pushNode(T x) {
- System.out.println("in pushnode " + number + "x is " + x);
- myStack.addFront(x);
- number++;
- System.out.println("leaving pushnode");
- return number;
- }
- //returns the first node in the list
- public T popNode() {
- T nodeVal; // value of node to be popped
- nodeVal = myStack.getNode(number - 1);
- myStack.deleteNode(number -1);
- number--;
- return nodeVal;
- }
- //returns the contents of the top of the stack doesn't pop the node
- public T peekNode() {
- T nodeVal;
- nodeVal = myStack.getNode(number - 1);
- return nodeVal;
- }
- //checks if there are not any values in the stack
- public boolean stackEmpty() {
- if (number == 0) {
- return true;
- } else {
- return false;
- }
- }
- //pops evaluates and pushes
- public void popEvalPush(GenericManagerStacks<Operators> x, GenericManagerStacks<Integer> y) {
- int a, b, c;
- char operandx;
- operandx = (x.popNode()).getOperator();
- a = y.popNode();
- b = y.popNode();
- c = IntEval(b, operandx, a);
- }
- //this is an evaluator for binary operators operating integers
- public static int IntEval(int oper1, char oper, int oper2) {
- int result;
- switch (oper) {
- case '+':
- result = oper1 + oper2;
- return result;
- case '-':
- result = oper2 - oper1;
- return result;
- case '*':
- result = oper2 * oper1;
- return result;
- case '/':
- if (oper2 != 0) {
- result = oper2 / oper1;
- return result;
- } else {
- return -99;
- }
- case '^':
- result = (int) Math.pow(oper2, oper1);
- return result;
- default:
- System.out.println("Bad Operator: " + oper);
- return -99;
- }
- }
- //finds the character x in the value table vtab and retruns the integers
- //value table in valtb
- public static int findVal(char x, char [] vtab, int [] valtb, int last){
- int i, vreturn = -99;
- for(i = 0; i <= last; i++){
- if(vtab[i] == x){
- vreturn = valtb[i];
- }
- }
- return vreturn;
- }
- }
- ==============================================================================================================================
- MyLinkManager
- ==============================================================================================================================
- /*
- * This will allow singly linked lists to be added removed and sorted.
- * It will accept any type of object because it is a generic class.
- * The objects must support the compareTo method.
- */
- package stackparser;
- public class MyLinkManager<T extends Comparable> {
- protected MyNode<T> head, tail; //this is the head and tail of the list
- protected int number; //holds number of items in the list
- //constructor
- public MyLinkManager(){
- //set the head and tail of list to null
- MyNode<T> head = null;
- MyNode<T> tail = null;
- //nothing in the list so we set number to 0
- number = 0;
- }
- //this is a class that constructs the nodes in MyLinkManager
- private static class MyNode<T>{
- protected T nodeValue;
- protected MyNode<T> next;
- public MyNode(T x){
- nodeValue = x;
- //sets pointer to next node to null
- next = null;
- }
- }
- //returns the number of nodes in the list
- public int getNumber(){
- return number;
- }
- //this method adds nodes to the front of the list
- public void addFront(T x){
- //create the node
- MyNode<T> newNode = new MyNode<T>(x);
- //add the node to the front of the list
- if(head == null){
- head = newNode;
- tail = newNode;
- }else{
- //there head isn't null we have nodes in the list
- newNode.next = head;
- //now move the new node to the front
- head = newNode;
- }
- //added a node so increment number
- number++;
- }
- //this method returns the node at the desired position if it exists
- public T getNode(int x){
- if((x < 0) || (x > number)){
- System.out.println("Error in getnode "+ x +" while list holds "+number);
- }
- //start iterating through the list looking for the requested node
- int ict = 1;
- MyNode<T> curnode;
- //make the iterator point to the first node
- curnode = head;
- //chain the nodes and iterate until we are at the node we want to return
- while(ict < x){
- curnode = curnode.next;
- ict++;
- }
- return curnode.nodeValue;
- }
- //this method will delete the node at the given position if it exists
- public int deleteNode(int x){
- MyNode<T> curNode, prevNode = null, nextNode = null;
- int ict = 1;
- //first check if there is a node with that position
- if(number < x || x < 0){
- System.out.println("Error in deleteNode "+ x +" only "+number+" in the list.");
- return 0;
- }else{
- //check if the node to be deleted will be the head
- if(x == 1){
- head = head.next;
- number--;
- return number;
- }
- //iterate through getting rid of the node requested
- curNode = head;
- while(ict < x){
- prevNode = curNode;
- curNode = curNode.next;
- nextNode = curNode.next;
- ict++;
- }
- //this points the tail to the right node if the tail was to be removed
- if(x == number){
- tail = nextNode;
- }
- prevNode.next = nextNode;
- number--;
- }
- return number;
- }
- //this will add the nodes in a specific order
- public void addInOrder(T x){
- //create the new node with x information
- MyNode<T> newNode = new MyNode<T>(x);
- //set up points to current position and next position
- MyNode<T> cnode, nnode;
- //see if this is the first node in the list
- if(number == 0){
- addFront(x);
- return;
- }
- //check if this goes in front they are going in ascending order
- if(x.compareTo(head.nodeValue) == -1){
- //put the node in front
- newNode.next = head;
- head = newNode;
- number++;
- return;
- }
- //check if this goes after
- if(x.compareTo(tail.nodeValue) == 1){
- //put the node after
- tail.next = newNode;
- tail = newNode;
- number++;
- return;
- }
- //this node needs to be inserted in the middle of the head and tail
- cnode = head;
- nnode = head.next;
- while(x.compareTo(nnode.nodeValue) != -1){
- cnode = nnode;
- nnode = nnode.next;
- }
- //now that x is smaller than the nnode and less than cnode we make the
- //newnode point to the next node
- cnode.next = newNode;
- newNode.next = nnode;
- number++;
- return;
- }
- }
