Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: None | Size: 8.23 KB | Hits: 56 | Expires: Never
Copy text to clipboard
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package stackparser;
  6.  
  7. import java.util.ArrayList;
  8.  
  9. /**
  10.  *
  11.  * @author tyler
  12.  */
  13. public class GenericManagerStacks<T>{
  14.  
  15.     protected MyLinkManager<T> myStack;
  16.     protected int number;
  17.  
  18.     //constructor
  19.     public GenericManagerStacks() {
  20.         number = 0; // number of indexes in myStack
  21.         myStack = new MyLinkManager<T>; //create initial array list of 100
  22.     }
  23.  
  24.     //returns the number of indexes in myStack
  25.     public int getNumber() {
  26.         return number;
  27.     }
  28.  
  29.     //pushes a node on the stack -- adds to the top of the stack
  30.     public int pushNode(T x) {
  31.         System.out.println("in pushnode " + number + "x is " + x);
  32.         myStack.addFront(x);
  33.         number++;
  34.         System.out.println("leaving pushnode");
  35.         return number;
  36.     }
  37.  
  38.     //returns the first node in the list
  39.     public T popNode() {
  40.         T nodeVal; // value of node to be popped
  41.         nodeVal = myStack.getNode(number - 1);
  42.         myStack.deleteNode(number -1);
  43.         number--;
  44.         return nodeVal;
  45.     }
  46.  
  47.     //returns the contents of the top of the stack doesn't pop the node
  48.     public T peekNode() {
  49.         T nodeVal;
  50.         nodeVal = myStack.getNode(number - 1);
  51.         return nodeVal;
  52.     }
  53.  
  54.     //checks if there are not any values in the stack
  55.     public boolean stackEmpty() {
  56.         if (number == 0) {
  57.             return true;
  58.         } else {
  59.             return false;
  60.         }
  61.     }
  62.  
  63.     //pops evaluates and pushes
  64.     public void popEvalPush(GenericManagerStacks<Operators> x, GenericManagerStacks<Integer> y) {
  65.         int a, b, c;
  66.         char operandx;
  67.         operandx = (x.popNode()).getOperator();
  68.         a = y.popNode();
  69.         b = y.popNode();
  70.         c = IntEval(b, operandx, a);
  71.     }
  72.  
  73.     //this is an evaluator for binary operators operating integers
  74.     public static int IntEval(int oper1, char oper, int oper2) {
  75.         int result;
  76.  
  77.         switch (oper) {
  78.             case '+':
  79.                 result = oper1 + oper2;
  80.                 return result;
  81.  
  82.             case '-':
  83.                 result = oper2 - oper1;
  84.                 return result;
  85.  
  86.             case '*':
  87.                 result = oper2 * oper1;
  88.                 return result;
  89.  
  90.             case '/':
  91.                 if (oper2 != 0) {
  92.                     result = oper2 / oper1;
  93.                     return result;
  94.                 } else {
  95.                     return -99;
  96.                 }
  97.             case '^':
  98.                 result = (int) Math.pow(oper2, oper1);
  99.                 return result;
  100.             default:
  101.                 System.out.println("Bad Operator: " + oper);
  102.                 return -99;
  103.         }
  104.     }
  105.  
  106.     //finds the character x in the value table vtab and retruns the integers
  107.     //value table in valtb
  108.     public static int findVal(char x, char [] vtab, int [] valtb, int last){
  109.         int i, vreturn = -99;
  110.  
  111.         for(i = 0; i <= last; i++){
  112.             if(vtab[i] == x){
  113.                 vreturn = valtb[i];
  114.             }
  115.         }
  116.  
  117.         return vreturn;
  118.     }
  119. }
  120.  
  121.  
  122. ==============================================================================================================================
  123. MyLinkManager
  124. ==============================================================================================================================
  125.  
  126. /*
  127.  * This will allow singly linked lists to be added removed and sorted.
  128.  * It will accept any type of object because it is a generic class.
  129.  * The objects must support the compareTo method.
  130.  */
  131. package stackparser;
  132.  
  133. public class MyLinkManager<T extends Comparable> {
  134.  
  135.     protected MyNode<T> head, tail; //this is the head and tail of the list
  136.     protected int number; //holds number of items in the list
  137.  
  138.     //constructor
  139.     public MyLinkManager(){
  140.         //set the head and tail of list to null
  141.         MyNode<T> head = null;
  142.         MyNode<T> tail = null;
  143.  
  144.         //nothing in the list so we set number to 0
  145.         number = 0;
  146.     }
  147.  
  148.     //this is a class that constructs the nodes in MyLinkManager
  149.     private static class MyNode<T>{
  150.         protected T nodeValue;
  151.         protected MyNode<T> next;
  152.         public MyNode(T x){
  153.             nodeValue = x;
  154.  
  155.             //sets pointer to next node to null
  156.             next = null;
  157.         }
  158.     }
  159.  
  160.     //returns the number of nodes in the list
  161.     public int getNumber(){
  162.         return number;
  163.     }
  164.  
  165.     //this method adds nodes to the front of the list
  166.     public void addFront(T x){
  167.         //create the node
  168.         MyNode<T> newNode = new MyNode<T>(x);
  169.  
  170.         //add the node to the front of the list
  171.         if(head == null){
  172.             head = newNode;
  173.             tail = newNode;
  174.         }else{
  175.             //there head isn't null we have nodes in the list
  176.             newNode.next = head;
  177.             //now move the new node to the front
  178.             head = newNode;
  179.         }
  180.         //added a node so increment number
  181.         number++;
  182.     }
  183.  
  184.     //this method returns the node at the desired position if it exists
  185.     public T getNode(int x){
  186.         if((x < 0) || (x > number)){
  187.             System.out.println("Error in getnode "+ x +" while list holds "+number);
  188.         }
  189.  
  190.         //start iterating through the list looking for the requested node
  191.         int ict = 1;
  192.         MyNode<T> curnode;
  193.  
  194.         //make the iterator point to the first node
  195.         curnode = head;
  196.  
  197.         //chain the nodes and iterate until we are at the node we want to return
  198.         while(ict < x){
  199.             curnode = curnode.next;
  200.             ict++;
  201.         }
  202.  
  203.         return curnode.nodeValue;
  204.     }
  205.  
  206.     //this method will delete the node at the given position if it exists
  207.     public int deleteNode(int x){
  208.  
  209.         MyNode<T> curNode, prevNode = null, nextNode = null;
  210.         int ict = 1;
  211.  
  212.         //first check if there is a node with that position
  213.         if(number < x || x < 0){
  214.             System.out.println("Error in deleteNode "+ x +" only "+number+" in the list.");
  215.             return 0;
  216.         }else{
  217.  
  218.            //check if the node to be deleted will be the head
  219.            if(x == 1){
  220.  
  221.                head = head.next;
  222.                number--;
  223.                return number;
  224.            }
  225.  
  226.            //iterate through getting rid of the node requested
  227.            curNode = head;
  228.            while(ict < x){
  229.                prevNode = curNode;
  230.                curNode = curNode.next;
  231.                nextNode = curNode.next;
  232.                ict++;
  233.            }
  234.  
  235.            //this points the tail to the right node if the tail was to be removed
  236.            if(x == number){
  237.                 tail = nextNode;
  238.            }
  239.  
  240.            prevNode.next = nextNode;
  241.            number--;
  242.         }
  243.         return number;
  244.     }
  245.  
  246.     //this will add the nodes in a specific order
  247.     public void addInOrder(T x){
  248.         //create the new node with x information
  249.         MyNode<T> newNode = new MyNode<T>(x);
  250.  
  251.         //set up points to current position and next position
  252.         MyNode<T> cnode, nnode;
  253.  
  254.         //see if this is the first node in the list
  255.         if(number == 0){
  256.             addFront(x);
  257.             return;
  258.         }
  259.  
  260.         //check if this goes in front they are going in ascending order
  261.         if(x.compareTo(head.nodeValue) == -1){
  262.             //put the node in front
  263.             newNode.next = head;
  264.             head = newNode;
  265.             number++;
  266.             return;
  267.         }
  268.  
  269.         //check if this goes after
  270.         if(x.compareTo(tail.nodeValue) == 1){
  271.             //put the node after
  272.             tail.next = newNode;
  273.             tail = newNode;
  274.             number++;
  275.             return;
  276.         }
  277.  
  278.         //this node needs to be inserted in the middle of the head and tail
  279.         cnode = head;
  280.         nnode = head.next;
  281.  
  282.         while(x.compareTo(nnode.nodeValue) != -1){
  283.             cnode = nnode;
  284.             nnode = nnode.next;
  285.         }
  286.  
  287.         //now that x is smaller than the nnode and less than cnode we make the
  288.         //newnode point to the next node
  289.         cnode.next = newNode;
  290.         newNode.next = nnode;
  291.         number++;
  292.         return;
  293.     }
  294. }