Advertisement
gelita

LinkedList Class

Feb 21st, 2020
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.91 KB | None | 0 0
  1. /**
  2.  *  Homework 09 - Linked List
  3.  *
  4.  *   *
  5.  *  Prompt:    Create a LinkedList class
  6.  *
  7.  *             The LinkedList class should contain the following public
  8.  *             properties:
  9.  *
  10.  *                length:   {Integer}
  11.  *                  head:   {ListNode}
  12.  *                  tail:   {ListNode}
  13.  *
  14.  *              The LinkedList class should also contain the following public
  15.  *              methods:
  16.  *
  17.  *                append:   A method that appends a value to the end of the
  18.  *                          LinkedList.
  19.  *
  20.  *                          Input:     {Integer}
  21.  *                          Output:    {Void}
  22.  *
  23.  *                insert:   A method that inserts an integer value at a set
  24.  *                          index in the LinkedList (assume head index is 0).
  25.  *
  26.  *                          Input:     value {Integer}
  27.  *                          Input:     index {Integer}
  28.  *                          Output:    {Void}
  29.  *
  30.  *                delete:   A method that removes a node at a specified index.
  31.  *
  32.  *                          Input:     index {Integer}
  33.  *                          Output:    {Void}
  34.  *
  35.  *              contains:   A method that checks to see if a value is contained
  36.  *                          in the list.
  37.  *
  38.  *                          Input:     value {Integer}
  39.  *                          Output:    {Boolean}
  40.  */
  41.  
  42. import java.util.*;
  43.  
  44. class ListNode {
  45.   public int value;
  46.   public ListNode next;
  47.  
  48.   public ListNode(int value){
  49.     this.value = value;
  50.     this.next = null;
  51.   }
  52. }
  53.  
  54.  
  55. class LinkedList {
  56.   public int length = 0;
  57.   public ListNode head;
  58.   public ListNode tail;
  59.  
  60.   // Time Complexity:
  61.   // Auxiliary Space Complexity:
  62.   public void append(int value){
  63.     insert(value, length);
  64.   }
  65.  
  66.  
  67.   // Time Complexity:
  68.   // Auxiliary Space Complexity:
  69.   public void insert(int value, int index){
  70.     //check to see if index for insertion is out of bounds of the LinkedList
  71.     if(index > length || index < 0){
  72.       return;
  73.     }
  74.     ListNode newNode = new ListNode(value);
  75.     //if no nodes exist, new node is head AND tail
  76.     if(length == 0){      
  77.       head = newNode;
  78.       tail = newNode;      
  79.     }else if(index == 0){
  80.       //set new node to pt to curr head next
  81.       //set head to pt to new node
  82.       newNode.next = head;
  83.       head = newNode;
  84.     }else if(index == length){
  85.       tail.next = newNode;
  86.       tail = newNode;
  87.     }else{
  88.       //if inserting somewhere in the middle->
  89.       //make iterator node variable and set to head of LinkedList
  90.       ListNode previousNode = head;
  91.       for(int i = 0; i< index-1; i++){
  92.         previousNode = previousNode.next;
  93.       }
  94.       //once node previous to index, insert newNode
  95.       newNode.next = previousNode.next;
  96.       previousNode.next = newNode;
  97.     }
  98.     //increase length to acct for inserted node
  99.     length++;    
  100.   }
  101.  
  102.  
  103.    // Time Complexity: O(N)
  104.   // Auxiliary Space Complexity: O(1)
  105.    public void delete(int index){
  106.    
  107.     //out of bounds? return
  108.     if(index < 0 || index >= length){
  109.       return;
  110.     }
  111.     //if there is only 1 item in LinkedList
  112.     if(length == 1){
  113.       head = null;
  114.       tail = null;
  115.     }else if(index == 0){
  116.       head = head.next;
  117.     }else {
  118.       //create iterator
  119.       ListNode previousNode = head;
  120.       for(int i = 0 ; i <index-1; i++){
  121.         previousNode = previousNode.next;
  122.       }
  123.       previousNode.next = previousNode.next.next;      
  124.       if(index == length -1){
  125.         tail = previousNode;
  126.       }
  127.     }
  128.     length--;
  129.   }
  130.  
  131.  
  132.   // Time Complexity: O(N)
  133.   // Auxiliary Space Complexity:O(1)
  134.   public boolean contains(int value){
  135.       //create iterator for search
  136.       ListNode currNode = head;
  137.       for(int i = 0; i < length; i++){
  138.         if(currNode.value == value){
  139.           return true;
  140.         }else{
  141.           currNode = currNode.next;
  142.         }
  143.       }
  144.       return false;
  145.    }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement