Advertisement
Guest User

Untitled

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