Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. package linkedlist;
  2.  
  3. public class LinkList {
  4.     private Node head;
  5.     private int listCount;
  6.  
  7.     // LinkedList constructor
  8.     public LinkList() {
  9.         // this is an empty list, so the reference to the head node
  10.         // is set to a new node with no data
  11.         head = new Node(null);
  12.         listCount = 0;
  13.     }
  14.  
  15.     public void add(Object data) // appends the specified element to the end of this list.
  16.     {
  17.         Node temp = new Node(data);
  18.         Node current = head;
  19.         // starting at the head node, crawl to the end of the list
  20.         System.out.println("belep");
  21.         while (current.getNext() != null) {
  22.             System.out.println("bent van");
  23.             current = current.getNext();
  24.            
  25.         }
  26.         // the last node's "next" reference set to our new node
  27.         current.setNext(temp);
  28.         listCount++;// increment the number of elements variable
  29.     }
  30.  
  31.     public void add(Object data, int index) // inserts the specified element at the specified position in this list
  32.     {
  33.         Node temp = new Node(data);
  34.         Node current = head;
  35.         // crawl to the requested index or the last element in the list,
  36.         // whichever comes first
  37.         for (int i = 1; i < index && current.getNext() != null; i++) {
  38.             current = current.getNext();
  39.         }
  40.         // set the new node's next-node reference to this node's next-node
  41.         // reference
  42.         temp.setNext(current.getNext());
  43.         // now set this node's next-node reference to the new node
  44.         current.setNext(temp);
  45.         listCount++;// increment the number of elements variable
  46.     }
  47.  
  48.     public Object get(int index) // returns the element at the specified position in this list.
  49.     {
  50.         // index must be 1 or higher
  51.         if (index <= 0) {
  52.             return null;
  53.         }
  54.  
  55.         Node current = head.getNext();
  56.         for (int i = 1; i < index; i++) {
  57.             if (current.getNext() == null) {
  58.                 return null;
  59.             }
  60.  
  61.             current = current.getNext();
  62.         }
  63.         return current.getData();
  64.     }
  65.  
  66.     public boolean remove(int index) // removes the element at the specified position in this list.
  67.     {
  68.         // if the index is out of range, exit
  69.         if (index < 1 || index > size()) {
  70.             return false;
  71.         }
  72.  
  73.         Node current = head;
  74.         for (int i = 1; i < index; i++) {
  75.             if (current.getNext() == null) {
  76.                 return false;
  77.             }
  78.  
  79.             current = current.getNext();
  80.         }
  81.         current.setNext(current.getNext().getNext());
  82.         listCount--; // decrement the number of elements variable
  83.         return true;
  84.     }
  85.  
  86.     public int size() // returns the number of elements in this list.
  87.     {
  88.         return listCount;
  89.     }
  90.  
  91.     @Override
  92.     public String toString() {
  93.         Node current = head.getNext();
  94.         String output = "";
  95.         while (current != null) {
  96.             output += "[" + current.getData().toString() + "]";
  97.             current = current.getNext();
  98.         }
  99.         return output;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement