waltermilner

Untitled

Mar 23rd, 2017
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. public class Test {
  2.  
  3.   public static void main(String args[]) throws Exception {
  4.     XORLinkedList l = new XORLinkedList();
  5.     l.put(9);
  6.     l.put(8);
  7.     l.put(7);
  8.     l.put(6);
  9.     l.debug();
  10.     l.traverseForward();
  11.     l.traverseBack();
  12.   }
  13.  
  14. }
  15.  
  16. public class XORLinkedList {
  17.  
  18.   private final int SIZE = 10;
  19.   private int[] data; // data nodes
  20.   private int[] link; // corresponding links
  21.   private int valCount; // how many items
  22.   private Random random; // rng
  23.   private int head, tail; // pointers to first and last item
  24.  
  25.   XORLinkedList() { // make a new empty list
  26.     data = new int[SIZE];
  27.     for (int i = 0; i < SIZE; i++) {
  28.       data[i] = -1; // codes empty slot
  29.     }
  30.     link = new int[SIZE];
  31.     valCount = 0;
  32.     random = new Random();
  33.  
  34.   }
  35.  
  36.   public void put(int val) {
  37.     // add val to linked list, linked to current tail
  38.     // is it full?
  39.     if (valCount == SIZE) {
  40.       return; // throw exception really
  41.     }
  42.     // not full..
  43.     int where; // where to put it
  44.     do {
  45.       where = random.nextInt(SIZE); // random location
  46.     } while (data[where] != -1); // until find empty location
  47.     data[where] = val;
  48.     if (valCount == 0) // insertion in empty list
  49.     {
  50.       head = where;
  51.       tail = where;
  52.       link[where] = 0 ^ 0;
  53.     } else {// setup link to previous end
  54.       int linkEnd = link[tail]; // previous tail link
  55.       linkEnd = linkEnd ^ 0; // get index
  56.       link[tail] = linkEnd ^ where; // make new link
  57.       int lastTail = tail; // move on
  58.       tail = where; // new tail
  59.       link[tail] = lastTail ^ 0; // set its link
  60.     }
  61.     valCount++;
  62.   }
  63.  
  64.   void traverseForward() {
  65.     int where = head; // start at head node
  66.     int previous = 0; // the previous node visited
  67.     int next;
  68.     do {
  69.       System.out.println(data[where] + " link=" + link[where] + " at:" + where);
  70.       next = previous ^ link[where]; // find next
  71.       previous = where; // prepare previous
  72.       where = next; // move on
  73.     } while (previous != tail); // until we visited tail
  74.  
  75.   }
  76.  
  77.   void traverseBack() {
  78.     int where = tail; // see traverseForward
  79.     int previous = 0;
  80.     int next;
  81.     do {
  82.       System.out.println(data[where] + " link=" + link[where] + " at:" + where);
  83.       next = previous ^ link[where];
  84.       previous = where;
  85.       where = next;
  86.     } while (previous != head);
  87.  
  88.   }
  89.  
  90.   void debug() { // output both arrays for debugging
  91.     System.out.println("Head = " + head);
  92.     System.out.println("Tail = " + tail);
  93.     for (int i = 0; i < SIZE; i++) {
  94.       System.out.println(i + " " + data[i] + " " + link[i]);
  95.     }
  96.  
  97.   }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment