Advertisement
Guest User

Untitled

a guest
May 27th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.util.ArrayList;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7.  
  8. class Node {
  9.     public int value;
  10.     public Node next;
  11.     public Node prev;
  12. }
  13.  
  14. public class List {
  15.     //Fill in your code in here and the empty procedures
  16.     private int N = 0;    // number of elements on list
  17.     private Node pre;     // sentinel before first item
  18.     private Node post;    // sentinel after last item
  19.    
  20.     public List(){
  21.         pre = new Node();
  22.         post = new Node();
  23.     }
  24.    
  25.    
  26.    
  27.    
  28.  
  29.  
  30.  
  31.     //This function should create a new empty list
  32.     public static List init() {
  33.         return new List();
  34.     }
  35.  
  36.     //This function should insert the value v in the list between the elemts with index i and index i-1
  37.     /*
  38.      * HEAD->FIRST = newNode
  39.  
  40. newNode->PREV = NULL
  41.  
  42. newNode->NEXT = NULL
  43.  
  44. increment(HEAD->LENGTH)
  45.      */
  46.     public static void insert(List l, int i, int v) {
  47.         Node tmp = new Node();
  48.         tmp.value = v;
  49.         Node go = l.pre.next;
  50.        
  51.         if(l.N == 0) {
  52.             tmp.next = l.post;
  53.             l.pre.next = tmp;
  54.             l.N++;
  55.         } else {
  56.             for(int iter = 0; iter < i-1; iter++) {
  57.                 go = go.next;
  58.             }
  59.             tmp.prev = go;
  60.             tmp.next = go.next;
  61.             tmp.prev.next = tmp;
  62.             go.next = tmp;
  63.             l.N = l.N+1;
  64.            
  65.         }
  66.        
  67.     }
  68.  
  69.     //This function should return the number of elemts in the list
  70.     public static int size(List l) {
  71.         return l.N;
  72.     }
  73.  
  74.     //This function should return the value of the element at index i in the list
  75.     public static int get(List l, int i) {
  76.         if(l == null){
  77.             System.out.println("nope. nothing there.");
  78.         } else {
  79.             Node tmp = l.pre;
  80.             for(int iter = 0; iter < l.N; iter++) {
  81.                 tmp = tmp.next;
  82.             }
  83.             return tmp.value;
  84.         }
  85.        
  86.         return -1;
  87.     }
  88.  
  89.     //This function should return the value of the element and als remove the element at index i in the list
  90.     public static int remove(List l, int i) {
  91.         i++;
  92.         Node go = l.pre;
  93.         int value = 0;
  94.         for(int iter = 0; iter < i; iter++) {
  95.             value = go.value;
  96.             go = go.next;  
  97.         }
  98.        
  99.         go.prev.next = go.next;
  100.         go.next.prev = go.prev;
  101.         l.N = l.N - 1;
  102.        
  103.         return value;
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement