Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1.  
  2. class Linkedlist{
  3.    
  4.     Node head;
  5.    
  6.     class Node{
  7.        
  8.         int val;
  9.         Node next;
  10.        
  11.         Node(int v)
  12.         {
  13.             val=v;
  14.             next=null;
  15.         }
  16.        
  17.     }
  18.    
  19.     public void enqueue(int x)
  20.     {
  21.         Node newnode =new Node(x);
  22.        
  23.         if(head==null)
  24.         {
  25.             head=new Node(x);
  26.             return;
  27.         }
  28.        
  29.         newnode.next=null;
  30.        
  31.         Node cur=head;
  32.         while(cur!=null)
  33.         {
  34.             cur=cur.next;
  35.         }
  36.         cur.next=newnode;
  37.         return;
  38.     }
  39.    
  40.     public int dequeue()
  41.     {
  42.        
  43.         Node temp=head;
  44.        
  45.         head=head.next;
  46.         temp.next=null;
  47.         return temp.val;
  48.          
  49.     }
  50.    
  51.     public void printlist()
  52.     {
  53.         Node current=head;
  54.        
  55.         while(current!=null)
  56.         {
  57.             System.out.println(current.val);
  58.             current=current.next;
  59.         }
  60.        
  61.    }
  62.    
  63.     void insertAfterHead(int x)
  64.     {
  65.        Node newnode=new Node(x);
  66.        
  67.        if(head==null){
  68.            head=newnode;
  69.            return;
  70.        }
  71.        
  72.        if(head.next==null){
  73.            
  74.            head.next=newnode;
  75.            return;
  76.        }
  77.        
  78.        Node current =head.next;
  79.        head.next=newnode;
  80.        
  81.        while(current!=null)
  82.        {
  83.            current=current.next;
  84.            
  85.          
  86.        }
  87.        current.next=null;
  88.        
  89.     }
  90.    
  91.     void insertBeforeTail(int x)
  92.     {
  93.         Node newnode=new Node(x);
  94.         Node cur=head;
  95.        
  96.         while(cur.next.next!=null)
  97.         {
  98.             cur=cur.next;
  99.            
  100.         }
  101.        
  102.         Node last=cur.next;
  103.        
  104.         newnode=cur.next;
  105.        
  106.         newnode.next=last;
  107.            
  108.        
  109.     }
  110.     void insertBeforeVal(int toFind, int toAdd)
  111.     {
  112.        
  113.        Node newnode =new Node(toAdd);
  114.        Node cur=head;
  115.        
  116.        while(cur!=null)
  117.        {
  118.            if(cur.next.val==toFind)
  119.            {
  120.                Node temp=cur;
  121.                cur.next=newnode;
  122.                newnode.next=temp;
  123.                return;
  124.            }
  125.            cur=cur.next;
  126.        }      
  127.     }
  128.    
  129.    void insertAfterVal(int toFind, int toAdd)
  130.     {
  131.        Node newnode =new Node(toAdd);
  132.        Node cur=head;
  133.        
  134.        while(cur!=null)
  135.        {
  136.            if(cur.next.val==toFind)
  137.            {
  138.                Node temp=cur.next.next;
  139.                cur.next.next=newnode;
  140.                temp=newnode.next;
  141.                return;
  142.            }  
  143.            
  144.            cur=cur.next;
  145.        }    
  146.     }
  147.  
  148.     int deleteFromPos(int x)
  149.     {
  150.         Node cur=head;
  151.        
  152.         int i=0;
  153.        
  154.         while((i<x)&&(cur!=null))
  155.         {
  156.            
  157.               cur=cur.next;
  158.             i++;
  159.         }
  160.        
  161.         Node temp=cur.next;
  162.         cur.next=cur.next.next;
  163.         return temp.val;
  164.        
  165.         }
  166.    
  167.     void  deleteVal(int x)
  168.     {
  169.         Node cur=head;
  170.         while(cur!=null)
  171.         {
  172.             if(cur.next.val==x)
  173.             {
  174.                
  175.               cur.next=cur.next.next;  
  176.                
  177.             }
  178.            
  179.           }
  180.            
  181.     }
  182.    
  183.     int deleteHead()
  184.     {
  185.         Node temp=head;
  186.        
  187.         head=head.next;
  188.        
  189.         return temp.val;
  190.     }
  191.    
  192.      int deleteTail(){
  193.          
  194.          Node cur=head;
  195.          
  196.          while(cur.next.next!=null)
  197.          {
  198.              
  199.              cur=cur.next;
  200.          }
  201.          
  202.          Node temp=cur.next;
  203.          
  204.          cur.next=null;
  205.          
  206.          return temp.val;
  207.      }
  208.      
  209.      int deleteValAfterHead()
  210.      {
  211.          if(head==null)
  212.              System.out.println("Underflow");
  213.          
  214.          if(head.next==null)
  215.              head=null;
  216.              
  217.          
  218.          Node temp=head.next;
  219.          head.next=head.next.next;
  220.          return temp.val;
  221.          
  222.          
  223.          
  224.      }
  225.      
  226.    
  227.     int deleteValBeforeTail()
  228.     {
  229.         if(head==null)
  230.             System.out.println("Underflow");
  231.         if(head.next==null)
  232.             head=null;
  233.        
  234.        
  235.        
  236.        
  237.        
  238.        
  239.         Node cur=head;
  240.        
  241.         while(cur.next.next.next!=null)
  242.         {
  243.            
  244.             cur=cur.next;
  245.        
  246.         }
  247.        
  248.         Node temp=cur.next;
  249.         cur.next=cur.next.next;
  250.         return temp.val;  
  251.     }
  252.    
  253.    
  254.    
  255.    
  256.    
  257.        
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement