Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SLLNode<E>
- {
- E element;
- SLLNode <E>succ;
- public SLLNode(E el,SLLNode<E>s)
- {
- element=el;
- succ=s;
- }
- }
- class Queue<E>
- {
- SLLNode<E>head;
- SLLNode<E>rear;
- int length;
- public Queue()
- {
- head=null;
- rear=null;
- length=0;
- }
- public void enque(E el)
- {
- if(head==null)
- rear=head=new SLLNode<E>(el,null);
- else
- {
- SLLNode<E> pom=new SLLNode<E>(el,null);
- rear.succ=pom;
- rear=pom;}
- length++;
- }
- public E peek()
- {
- return head.element;
- }
- public E deque()
- {
- E element=null;
- if(head!=null)
- {
- element=head.element;
- head=head.succ;
- length--;
- }
- return element;
- }
- public int size()
- {return length; }
- public String toString()
- {
- SLLNode<E> tmp=head;
- String s="";
- while(tmp!=null)
- {s+=tmp.element.toString()+" ";
- tmp=tmp.succ;}
- return s;
- }
- }
- class Stack<E>
- {
- SLLNode<E>top;
- int length;
- public Stack()
- {
- top=null;
- length=0;
- }
- public void push(E el)
- {
- if(top==null)
- top=new SLLNode<E>(el,null);
- else
- {
- SLLNode<E> pom=new SLLNode<E>(el,top);
- top=pom;}
- length++;
- }
- public E peek()
- {
- return top.element;
- }
- public E pop()
- {
- E element=null;
- if(top!=null)
- {
- element=top.element;
- top=top.succ;
- length--;
- }
- return element;
- }
- public int size()
- {return length; }
- public String toString()
- {
- SLLNode<E> tmp=top;
- String s="";
- while(tmp!=null)
- {s+=tmp.element.toString()+" ";
- tmp=tmp.succ;}
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement