Advertisement
ivana_andreevska

SLL Layout

Jan 30th, 2022
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. public class Algoritmi {
  2. }
  3.  
  4. class Node{
  5.     int data;
  6.     Node next;
  7.  
  8.     public Node(int x)
  9.     {
  10.         data=x;
  11.         next=null;
  12.     }
  13. }
  14.  
  15. class LinkedList{
  16.     Node head;
  17.  
  18.     public void insertNodeAtStart(int x)
  19.     {
  20.         if(head==null)
  21.         {
  22.             head=new Node(x);
  23.             return;
  24.         }
  25.  
  26.         Node newNode=new Node(x);
  27.         newNode.next=head;
  28.         head=newNode;
  29.     }
  30.  
  31.     public void insertNodeAtEnd(int x)
  32.     {
  33.         if(head==null)
  34.         {
  35.             head=new Node(x);
  36.             return;
  37.         }
  38.         Node newNode=new Node(x);
  39.         Node temporaryNode=head;
  40.         while(temporaryNode!=null)
  41.         {
  42.             temporaryNode=temporaryNode.next;
  43.         }
  44.         temporaryNode.next=newNode;
  45.     }
  46.  
  47.     public void deleteNode(int x)
  48.     {
  49.         Node currentNode=head;
  50.         Node previousNode=null;
  51.  
  52.         while(currentNode!=null && currentNode.data!=x)
  53.         {
  54.             previousNode=currentNode;
  55.             currentNode=currentNode.next;
  56.         }
  57.         previousNode.next=currentNode.next;
  58.     }
  59.     public void print()
  60.     {
  61.         Node temporaryNode=head;
  62.         while(temporaryNode!=null)
  63.         {
  64.             System.out.println(temporaryNode.data+"-->");
  65.             temporaryNode=temporaryNode.next;
  66.         }
  67.         System.out.println();
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement