Advertisement
Kostiggig

RingQueue

May 6th, 2023
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. package ring_queue;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Set;
  5.  
  6. public class RingQueue<T> {
  7.  
  8.     private Node<T> head;
  9.     private Node<T> tail;
  10.     private int size;
  11.  
  12.     public void enqueue(T val, int id) {
  13.         Node<T> node = new Node<T>(val, id);
  14.         if(head == null) {
  15.             head = node;
  16.         } else {
  17.             tail.next = node;
  18.         }
  19.         tail = node;
  20.         tail.next = head;
  21.         size++;
  22.     }
  23.  
  24.     public int remove(Node<T> node) {
  25.         int id = node.id;
  26.         if(head == null) throw new IllegalStateException("head is null");
  27.         if(head.id == id) {
  28.             int toRemoveVal = (int) head.val;
  29.             head = head.next;
  30.             size--;
  31.             return toRemoveVal;
  32.         } else {
  33.             Node<T> curr = head;
  34.             while(true) {
  35.                 if(curr.next.id == id) {
  36.                     int toRemoveVal = (int) curr.next.val;
  37.                     curr.next = curr.next.next;
  38.                     size--;
  39.                     return toRemoveVal;
  40.                 }
  41.                 curr = curr.next;
  42.             }
  43.         }
  44.     }
  45.  
  46.     public int size() {
  47.         return size;
  48.     }
  49.  
  50.     Set<Integer> seen = new HashSet<>();
  51.  
  52.     public void removeEvery(int k) {
  53.         int position = 1;
  54.         Node<T> curr = head;
  55.         while(size > k) {
  56.             if(position % k == 0) {
  57.                 if(!seen.contains(curr.id)) {
  58.                     remove(curr);
  59.                     System.out.println(this);
  60.                 }
  61.             }
  62.             position++;
  63.             curr = curr.next;
  64.         }
  65.     }
  66.  
  67.     @Override
  68.     public String toString() {
  69.         StringBuilder result = new StringBuilder();
  70.         int index = 0;
  71.         Node<T> curr = head;
  72.         while(true) {
  73.             result.append(curr.val);
  74.             if(index >= size) break; else result.append("->");
  75.             index++;
  76.             curr = curr.next;
  77.         }
  78.  
  79.         return result.toString();
  80.     }
  81.  
  82.     static class Node<T> {
  83.         Node<T> next;
  84.         final T val;
  85.         final int id;
  86.  
  87.         public Node(T val, int id) {
  88.             this.val = val;
  89.             this.id = id;
  90.         }
  91.  
  92.         public Node(T val, int id, Node<T> next) {
  93.             this.next = next;
  94.             this.val = val;
  95.             this.id = id;
  96.         }
  97.  
  98.         @Override
  99.         public String toString() {
  100.             return "Node{" +
  101.                     "next=" + next +
  102.                     ", id=" + id +
  103.                     ", val=" + val +
  104.                     '}';
  105.         }
  106.     }
  107. }
  108.  
  109. package ring_queue;
  110.  
  111. public class Client {
  112.  
  113.     public static void main(String[] args) {
  114.         RingQueue<Integer> queue = new RingQueue<>();
  115.         for (int i = 0; i < 20; i++) {
  116.             queue.enqueue(i, i);
  117.         }
  118.  
  119.         System.out.println(queue);
  120.         queue.removeEvery(5);
  121.     }
  122.  
  123. }
  124.  
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement