Advertisement
jackyhuichunkit

LInkedListDemo 1

Feb 24th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. class linkedlistdemo1 {
  2.   static int[] data = new int[12];
  3.   static int[] next = new int[12];
  4.   static int HEAD, FREE;
  5.  
  6.   public static void initList() {
  7.     int i;
  8.     // -1 is used to indicate NULL pointer values
  9.     HEAD = -1; // Empty List
  10.     FREE = 0; // The free list starts with first node
  11.     for (i=0; i<12; i++) {
  12.       // initializes to nodes to 0
  13.       // in fact any values can be used
  14.       data[i] = 0;
  15.       // initialize the free list
  16.       // to include all nodes
  17.       // FREE -> 0 -> 1 -> 2 -> 3 ..... 10 -> 11 -> -1
  18.       next[i] = i+1;
  19.       // The last node in the array
  20.       if (i == 11) {
  21.         next[i] = -1;
  22.       }
  23.     }
  24.   }
  25.  
  26.   public static int getFreeNode() {
  27.     int nodeID;
  28.     // If there are available elements
  29.     if (FREE != -1) {
  30.       nodeID = FREE;
  31.       FREE = next[FREE];
  32.     } else {
  33.       // Return -1
  34.       // No more free nodes
  35.       nodeID = -1;
  36.     }
  37.     return nodeID;
  38.   }
  39.  
  40.   public static void returnFreeNode(int nodeID) {
  41.       next[nodeID] = FREE;
  42.       FREE = nodeID;
  43.   }
  44.  
  45.   public static void removeBegin() {
  46.     int temp;
  47.     if (HEAD != -1) {
  48.       temp = HEAD;
  49.       HEAD = next[HEAD];
  50.       returnFreeNode(temp);
  51.     }
  52.   }
  53.  
  54.   public static boolean insertAfter(int item, int pos){
  55.     int temp;
  56.     int count, prev, current;
  57.     temp = getFreeNode();
  58.     if (temp != -1) {
  59.       if (pos == 0) {
  60.         data[temp] = item;
  61.         next[temp] = HEAD;
  62.         HEAD = temp;
  63.         return true;
  64.       } else {
  65.         count = 0;
  66.         prev = HEAD;
  67.         current = HEAD;
  68.         while (current != -1 && count < pos) {
  69.           prev = current;
  70.           current = next[current];
  71.           count++;
  72.         }
  73.         data[temp] = item;
  74.         next[temp] = current;
  75.         next[prev] = temp;
  76.         return true;
  77.       }
  78.     } else
  79.       return false;
  80.   }
  81.  
  82.   public static boolean insertBegin(int item){
  83.     return insertAfter(item, 0);
  84.   }
  85.  
  86.   public static int countList() {
  87.     int result=0;
  88.     int current;
  89.     current = HEAD;
  90.     while (current != -1) {
  91.       result++;
  92.       current = next[current];
  93.     }
  94.     return result;
  95.   }
  96.  
  97.   public static void printList() {
  98.     int i;
  99.     i = HEAD;
  100.     System.out.print("HEAD -> ");
  101.     while (i != -1) {
  102.       System.out.printf("%d -> ", data[i]);
  103.       i = next[i];
  104.     }
  105.     System.out.println("END");
  106.   }
  107.  
  108.   public static void printFreeList() {
  109.     int i;
  110.     i = FREE;
  111.     System.out.print("FREE -> ");
  112.     while (i != -1) {
  113.       System.out.printf("[%d] -> ", i);
  114.       i = next[i];
  115.     }
  116.     System.out.println("END");
  117.   }
  118.  
  119.   public static void main(String[] args) {
  120.     initList();
  121.     insertBegin(9);
  122.     insertBegin(8);
  123.     insertBegin(7);
  124.     printList();
  125.     System.out.printf("Number of elements: %d\n",countList());
  126.  
  127.   }
  128.  
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement