Advertisement
MnMWizard

Linked List module 9

May 11th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. //Mason Marnell
  2. //Linked List Class
  3.  
  4. public class LinkedList {
  5.     Node header;
  6.    
  7.     public LinkedList()
  8.     {
  9.         header = null;
  10.     }
  11.     public LinkedList(int data)
  12.     {
  13.         header = new Node();
  14.         header.value = data;
  15.     }
  16.     public void addNode(int data)
  17.     {
  18.         Node temp = header;
  19.         if(header != null)
  20.             addNode(data,temp);
  21.         else
  22.         {
  23.             header = new Node();
  24.             header.value = data;
  25.         }
  26.     }
  27.     public void addNode(int data, Node node)
  28.     {
  29.         if(node.next == null)
  30.         {
  31.             node.next = new Node();
  32.             node = node.next;
  33.             node.value = data;
  34.             return;  // ends addNode method
  35.         }
  36.         // recursively call the next node, this one is not null
  37.         addNode(data,node.next);
  38.     }
  39.    
  40.     public void deleteFirst()
  41.     {
  42.         header = header.next;
  43.     }
  44.     public void deleteValue(int data)
  45.     {
  46.         deleteValue(data,header);
  47.     }
  48.     public void deleteValue(int data, Node node)
  49.     {
  50.         if(node.value == data)
  51.         {
  52.             node = null;
  53.             return;  
  54.         }
  55.         // recursively call the next node, this one is not null
  56.         deleteValue(data,node.next);
  57.         // use recursion to find the value and delete it __ similar to addNode
  58.  
  59.         //I couldn't get the deleteValue to work so I guess I'll just take
  60.         //the partial credit for the addNode
  61.     }
  62.    
  63.     public String toString()
  64.     {
  65.         String data = "";
  66.         if (header != null)
  67.         {
  68.             Node temp = header;
  69.             data = toString(temp);
  70.         }
  71.         return data;
  72.     }
  73.     public String toString(Node node)
  74.     {
  75.         if(node != null)
  76.         {
  77.             return node.value + " " + toString(node.next);
  78.         }
  79.         return "";
  80.     }
  81. }
  82.  
  83. //---------------------------------------------------------------------------------------------------------
  84. //LinkedListRunnerClass class
  85.  
  86.  
  87. public class LinkedListRunnerClass {
  88.  
  89.     /**
  90.      * @param args the command line arguments
  91.      */
  92.     public static void main(String[] args) {
  93.         int data = 3;
  94.         LinkedList info = new LinkedList(data);
  95.         info.addNode(4);
  96.         info.addNode(7);
  97.         info.addNode(9);
  98.         System.out.println(info);
  99.         info.deleteValue(2);
  100.         System.out.println(info);
  101.     }
  102.    
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement