Jump to content

Generic stack help

- - - - -

  • Please log in to reply
8 replies to this topic

#1
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
I coded a stack template using linked list in C++ 2 years back . At the moment i am trying to do the same with Java , but i keep on getting errors , Can anyone here help me out with it?Here is my code

class Stack<T>
{
    public Stack()
    {
        Size = 0;
        head = new Node<T>();
        head.next = null;
    }
    
    public void push(T item)
    {
        Node<T> n=new Node<T>();
        Size++;    
        
        if(head==null)
            head=n;
        else
        {
            n.setValue(item);
            head=n;
        }    
    
    }
    
    T pop()
    {
        Node<T> n=new Node<T>();
        Node<T> temp=new Node<T>();
        
        if(isEmpty())
            {return 0;} // get error here
        else
        {
            n=head.getValue();
            temp=head;
            head = head.getNext();
            Size--;
            
            return n;  //error here
        }
    }
    
    
    boolean isEmpty()
    {
        if(head==null)
            return true;
        else 
            return false;
    }
                
 
    Node<T> head;
    int Size;
    
    
    
    private class Node<T>
    {
        void setValue(T i)
        {
                value=i;
        }
        
        T getValue()
        {
            return value;
        }
        void setNext(Node n)
        {
                next=n;
        }
        
        Node getNext()
        {
                return next;
        }
    
        T value;
        Node next;
    }
    
    
}

Thanks

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Can you post the errors please.

#3
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
on return 0; i get error "Incompatible types"

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I assume you want "null" instead of "0", "0" is of type int (or Integer), not of type T, hence the error.

#5
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
thanks that fixed the error , but when i do this in the main i get this exception "NoSuchMethodError"
Stack<Integer> in = new Stack<Integer>();
        in.push(10);


#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Works fine with me.

#7
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Can you post the code you compiled?

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
class Stack<T> {
    private Node<T> head;
    private int size;


    public Stack() {
        size = 0;
        head = new Node<T>();
        head.next = null;
    }


    public void push(T item) {
        Node<T> n = new Node<T>();
        size++;


        if (head == null)
            head = n;
        else {
            n.setValue(item);
            head = n;
        }


    }


    public T pop() {
        if (isEmpty()) {
            return null;
        }


        T n = head.getValue();
        size--;
        return n;
    }




    boolean isEmpty() {
        return head == null;
    }




    private class Node<T> {
        void setValue(T i) {
            value = i;
        }


        T getValue() {
            return value;
        }


        void setNext(Node n) {
            next = n;
        }


        Node getNext() {
            return next;
        }


        T value;
        Node next;
    }


    public static void main(String[] args) {
        Stack<Integer> in = new Stack<Integer>();
        in.push(10);
    }


}




#9
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thanks , there was some problem at my end. Okay now i am trying to implement an infix calculator . I have written down all the code but i am having problem , if my input is without any parenthesis then i get a nullPointerException in the switch , and when i give an expression with parenthesis it goes in infinite loop .
class d8
{
    public static int eVal(String s)
    {    int t=0;
        char temp;
        String tStr="";
        
        for(int i=0;i<s.length();i++)
        {
            temp=s.charAt(i);
            
            if( Character.isDigit(temp) )
            {
                tStr +=temp;
                intStack.push( Integer.parseInt(tStr) );
            }
            else
            {
                switch(temp)
                {
                    case '(':
                        charStack.push(temp);
                        break;
                    
                    case '*':
                        if( charStack.isEmpty() )
                            charStack.push(temp);
                        
                        else if (prec(temp) > prec(charStack.top())  )
                        charStack.push(temp) ; 
                        else
                          {
                           while ( !charStack.isEmpty() && prec(temp) <= prec(charStack.top()) )
                           {
                            apply(intStack,charStack);
                            charStack.push(temp) ;
                           }
                          }
                          break;
                          
                    case '+':
                        if( charStack.isEmpty() )
                            charStack.push(temp);
                        
                    else if ( prec(temp) > prec(charStack.top()) ) [B]// NULL POINTER EXCEPTION HERE[/B]
                        charStack.push(temp) ; 
                        else
                          {
                           while ( !charStack.isEmpty() && prec(temp) <= prec(charStack.top()) )
                           {
                            apply(intStack,charStack);
                            charStack.push(temp) ;
                           }
                          }
                          break;
                          
                    case '-':
                        if( charStack.isEmpty() )
                            charStack.push(temp);
                        
                        else if (prec(temp) > prec(charStack.top()) )
                        charStack.push(temp) ; 
                        else
                          {
                           while ( !charStack.isEmpty() && prec(temp) <= prec(charStack.top()) )
                           {
                            apply(intStack,charStack);
                            charStack.push(temp) ;
                           }
                          }
                          break;
                          
                    case '/':
                        if( charStack.isEmpty() )
                            charStack.push(temp);
                        
                        else if (prec(temp) > prec(charStack.top()) )
                        charStack.push(temp) ; 
                        else
                          {
                           while ( !charStack.isEmpty() && prec(temp) <= prec(charStack.top()) )
                           {
                            apply(intStack,charStack);
                            charStack.push(temp) ;
                           }
                          }
                          break;
                          
                          
                          
                    case ')':
                             while (charStack.top() != '(')
                            {
                             apply(intStack,charStack);
                             charStack.pop() ;
                             break;
                            }
    
                }
            }
            
        }
        
        while(!charStack.isEmpty())
             {
               apply(intStack,charStack);
               result = intStack.top();
             }        
    
        return 0;
    }
    
    public static void apply(GenericStack<Integer> v, GenericStack<Character> o)
    {
        int rightVal=0,leftVal=0;
        
        rightVal = v.pop();
        leftVal = v.pop();
        char nextOp = o.pop();

            switch (nextOp) 
            {
                case '+':
                    result =leftVal + rightVal;
                    intStack.push(result);
                    break;
                case '-':
                    result =leftVal - rightVal;
                    intStack.push(result);
                    break;
                case '*':
                    result =leftVal * rightVal;
                    intStack.push(result);
                    break;
                case '/':
                    result =leftVal / rightVal;
                    intStack.push(result);
                    break;
            }
            
        
    }
    
    public static int prec(char op)
    {
        if(op=='+' || op=='-')
            return 1;
        else if(op=='*'|| op=='/'|| op=='%')
            return 2;
        else
            return 0;
    }
    
    public static boolean isOperator(char op)
    {
        if (op=='+' ||op=='-' ||op=='*' ||op=='/' ||op=='(' ||op==')')
            return true;
        else
            return false;
        
    }
    
    

    public static void main(String argv[])
    {
        
    
        String s="(((3 + 2)*6) % 4 ) ";
        s=s.replaceAll(" ","");
        eVal(s);
        System.out.println("Result : "+ result);
    }
    
    static GenericStack<Integer> intStack = new GenericStack<Integer>();
    static GenericStack<Character> charStack = new GenericStack<Character>();
    static int result=0;

}


What exactly is the problem?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users