Egor_Vakar

(Java) lab 5.1 List

Mar 8th, 2022 (edited)
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. public class List {
  2.     private static class Node {
  3.         String data;
  4.         Node pPrev;
  5.         Node pNext;
  6.  
  7.         public Node(String data) {
  8.             this.data = data;
  9.             this.pPrev = null;
  10.             this.pNext = null;
  11.         }
  12.     }
  13.  
  14.     private Node head;
  15.     private Node tail;
  16.     private int length = 0;
  17.  
  18.     public List() {
  19.         head = null;
  20.         tail = null;
  21.     }
  22.  
  23.     public int getLength() {
  24.         return this.length;
  25.     }
  26.  
  27.     public void addElem(final String data) {
  28.         Node temp = new Node(data);
  29.         if (head == null) {
  30.             head = temp;
  31.         } else {
  32.             if (tail == null) {
  33.                 head.pNext = temp;
  34.                 tail = temp;
  35.                 tail.pPrev = head;
  36.             } else {
  37.                 tail.pNext = temp;
  38.                 temp.pPrev = tail;
  39.                 tail = temp;
  40.             }
  41.         }
  42.         length++;
  43.     }
  44.  
  45.     public void deleteElem(final int index) {
  46.         boolean isDeleted = false;
  47.  
  48.         if (index == 0) {
  49.             if (head.pNext != null) {
  50.                 head = head.pNext;
  51.             }
  52.             head.pPrev = null;
  53.             isDeleted = true;
  54.         }
  55.         else {
  56.             if (index == length - 1) {
  57.                 tail = tail.pPrev;
  58.                 tail.pNext = null;
  59.                 isDeleted = true;
  60.             }
  61.         }
  62.  
  63.         if (!isDeleted) {
  64.             Node temp;
  65.             int counter;
  66.             if (index <= length - 1 - index) {
  67.                 temp = head;
  68.                 counter = 0;
  69.                 while (counter != index) {
  70.                     temp = temp.pNext;
  71.                     counter++;
  72.                 }
  73.             } else {
  74.                 temp = tail;
  75.                 counter = length - 1;
  76.                 while (counter != index) {
  77.                     temp = temp.pPrev;
  78.                     counter--;
  79.                 }
  80.             }
  81.             temp.pPrev.pNext = temp.pNext;
  82.             temp.pNext.pPrev = temp.pPrev;
  83.         }
  84.         System.out.println("\n|||ЭЛЕМЕНТ УСПЕШНО УДАЛЁН|||\n");
  85.         length--;
  86.     }
  87.  
  88.     public void reverse() {
  89.         Node fromHead = head;
  90.         Node fromTail = tail;
  91.         String temp;
  92.         int counter = 0;
  93.         while (counter != (length / 2)) {
  94.             temp = fromHead.data;
  95.             fromHead.data = fromTail.data;
  96.             fromTail.data = temp;
  97.             fromHead = fromHead.pNext;
  98.             fromTail = fromTail.pPrev;
  99.             counter++;
  100.         }
  101.     }
  102.  
  103.     public String printList() {
  104.         StringBuilder str = new StringBuilder();
  105.         Node temp = head;
  106.         int counter = 0;
  107.         str.append("\n|||СПИСОК|||\n\n");
  108.         while (counter != length) {
  109.             str.append(counter).append(".  ").append(temp.data).append("\n");
  110.             temp = temp.pNext;
  111.             counter++;
  112.         }
  113.         return str.toString();
  114.     }
  115.  
  116.     public boolean isEmpty() {
  117.         return head == null;
  118.     }
  119. }
Add Comment
Please, Sign In to add comment