abouttr3

Linkedlist queue

Oct 12th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. public class LinkedQueue{
  2.     private int size;
  3.     private Node head;
  4.    
  5.     public LinkedQueue(){        
  6.         head=new Node(null);
  7.         size=0;
  8.     }
  9.    
  10.     public void add(Object data){
  11.         Node temp=head;
  12.         while(temp.next!=null){
  13.             temp=temp.next;
  14.         }
  15.         temp.next=new Node(data);
  16.         size++;
  17.        
  18.     }
  19.    
  20.     public void delete(){
  21.         if(size==0){
  22.             System.out.println("Stack Under Flow");
  23.         }
  24.         else{
  25.             size--;
  26.             Node temp= head;
  27.             temp.next=temp.next.next;
  28.         }
  29.     }
  30.    
  31.     public Object front(){
  32.         if(isEmpty()){
  33.             return null;
  34.         }
  35.         else{
  36.             return head.next.data;
  37.         }
  38.     }
  39.     public Object rear(){
  40.         if(isEmpty()){
  41.             return null;
  42.         }
  43.         else{
  44.             Node temp=head;
  45.             while(temp.next!=null){
  46.                 temp=temp.next;
  47.             }
  48.             return temp.data;
  49.         }
  50.     }
  51.    
  52.     public boolean isEmpty(){
  53.         return (size==0);
  54.     }
  55.    
  56.     public boolean isFull(){
  57.         return false;
  58.     }
  59.    
  60.     public String toString()
  61.     {
  62.         String output = "";
  63.         if(!isEmpty()){
  64.             Node temp= head.next;
  65.             while(temp != null)
  66.             {
  67.                 output += "[" + temp.data + "]";
  68.                 temp= temp.next;
  69.             }
  70.             return output;
  71.         }
  72.         else{
  73.             return null;
  74.         }
  75.        
  76.     }
  77.    
  78.     private class Node
  79.     {
  80.         protected Node next;
  81.         protected Object data;
  82.        
  83.         public Node(Object _data)
  84.         {
  85.             next = null;
  86.             data = _data;
  87.         }
  88.  
  89.         public Node(Object _data, Node _next)
  90.         {
  91.             next = _next;
  92.             data = _data;
  93.         }
  94.  
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment