Saturday, 20 October 2012

STACK AND QUEUE

IMPLEMENTING  STACK

class stack{
             Node top;
             Node pop(){
                        if(top!=null){
                             object item=top.data;
                             top=top.next;
                             return item;
                        }
                        return null;
            }
void push(object item){
                    Node t=new Node(item);
                    t.next=top;
                    top=t;
             }
}

IMPLEMENTING A QUEUE

class Queue{
               Node first,last;
              void enqueue(object item){
                      if(!=first){
                      back=new Node(item);
                       first=back;
                      }
                      else{
                       back.next=new NOde(item);
                       back=back.next;
                       }   
           }

Node dequeue(Node n){
                           if(front!=null)
                           {
                                  object item=front data;
                                   front=front.next;
                                   return next;
                            }
                            return null;
}

No comments: