Advertisement
ZazoTazo

Single Linked List

Aug 23rd, 2020
2,114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. public class MySingleLinkedList {
  2.     Element head;
  3.     Element tail;
  4.    
  5.     public MySingleLinkedList(){
  6.         this.head = null;
  7.         this.tail = null;
  8.     }
  9.    
  10.     public void append(Element e){
  11.         if (this.head == null){
  12.             this.head = e;
  13.         }
  14.         else{
  15.             this.tail.next = e;
  16.         }
  17.         this.tail = e;
  18.     }
  19.    
  20.     public void prepend(Element e){
  21.         if (this.head == null){
  22.             this.tail = e;
  23.         }
  24.         else{
  25.             e.next = this.head;
  26.         }
  27.         this.head = e;
  28.     }
  29.    
  30.     public void insertBefore(Element e, int n){
  31.         int counter = 0;
  32.         Element previous = head;
  33.         Element current = head.next;
  34.        
  35.         while (counter+1 < n){
  36.             previous = current;
  37.             current = current.next;
  38.             counter++;
  39.         }
  40.         e.next = current;
  41.         previous.next = e;
  42.     }
  43.    
  44.     public void insertAfter(Element e, int n){
  45.         int counter = 0;
  46.         Element previous = this.head;
  47.         Element current = this.head.next;
  48.        
  49.         while (counter < n){
  50.             previous = current;
  51.             current = current.next;
  52.             counter++;
  53.         }
  54.         e.next = current;
  55.         previous.next = e;
  56.     }
  57.    
  58.     public void extractFirst(){
  59.         if(this.head == null){
  60.             System.out.println("Error");
  61.         }
  62.         this.head = this.head.next;
  63.         if(this.head == null){
  64.             System.out.println("Error");
  65.         }
  66.     }
  67.    
  68.     public void extractLast(){
  69.         if (this.tail == null){
  70.             System.out.println("Error");
  71.         }
  72.         if (this.head == this.tail){
  73.             this.head = this.tail = null;
  74.         }
  75.         else{
  76.             Element previous = this.head;
  77.             while(previous.next != null){
  78.                 previous = previous.next;
  79.             }
  80.             previous.next = null;
  81.             this.tail = previous;
  82.         }
  83.     }
  84.    
  85.     public int countNodes(){
  86.         int counter = 0;
  87.         Element first = this.head;
  88.         while(first != null){
  89.             counter++;
  90.             first = first.next;
  91.         }
  92.         return counter;
  93.     }
  94.    
  95.     public int searchNode(Object obj){
  96.         int counter = 0;
  97.         Element first = this.head;
  98.         while(first != null){
  99.             if (first.data.equals(obj)){
  100.                 counter++;
  101.             }
  102.             first = first.next;
  103.         }
  104.         return counter;
  105.     }
  106.    
  107.     public void display(){
  108.         Element start = this.head;
  109.         while (start != null){
  110.             start.print();
  111.             start = start.next;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement