LikedList’ler diziler gibidir , Object yapısını liste şeklinde tutabilirler, Datalar biribirine node yapıları ile bağlıdır, Node’lar bellek adresini işaret eder. buna bağlı olarak ardan bir eleman çıkarıldığı zaman node bir sonraki datayı gösterecektir.
![ll2](https://ozcancitak.wordpress.com/wp-content/uploads/2015/04/ll2.jpg?w=614&h=319)
LinkedList java.lang.Object java.util.AbstractCollection java.util.AbstractList java.util.AbstractSequentialList java.util.LinkedList LinkedList sınıfı List arayüzünü implemant eder.
public class LinkedList
extends AbstractSequentialList
implements List, Deque, Cloneable, Serializable
Dolayısıyla listelerle ilgili bütün metotları almış olur ve null dahil bütün öğelere izin verir. Özellikle get(), remove() ve insert() metotlarını listenin başına, sonuna veya ortasında bir yere uygulayabilir. Böylece bağlı listelerden, isteğe göre, stack (yığın -LIFO), queue (kuyruk -FIFO), deque (çifte sonlanmış kuyruk) yapıları elde edilebilir.
Java’da LinkedList sınıfı hem tek-bağlı hem iki-bağlı yapıdadır. Aşağıdaki örnekler, LinkedList sınıfı ile yapıabilen işlerden bazılarını göstermektedir. LİnkedLİst sınıfının iki tane kurucusu vardır: LinkedList() Boş bir bağlı-liste yaratır. LinkedList(Collection c) Parametrenin belirlediği koleksiyonu içeren bir bağlı-liste yaratır. Öğelerin sırası koleksiyonun iteratör’ünün (tekrarlayıcı) belirlediği sıradır.
LikedList’ler synchronized yapıdalar.
List list = Collections.synchronizedList(new LinkedList(...));
LinkedList metodları :
boolean
add(E e)
Appends the specified element to the end of this list.
void
add(int index, E element)
Inserts the specified element at the specified position in this list.
boolean
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator.
boolean
addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.
void
addFirst(E e)
Inserts the specified element at the beginning of this list.
void
addLast(E e)
Appends the specified element to the end of this list.
void
clear()
Removes all of the elements from this list.
Object
clone()
Returns a shallow copy of this LinkedList
.
boolean
contains(Object o)
Returns true
if this list contains the specified element.
Iterator<E>
descendingIterator()
Returns an iterator over the elements in this deque in reverse sequential order.
E
element()
Retrieves, but does not remove, the head (first element) of this list.
E
get(int index)
Returns the element at the specified position in this list.
E
getFirst()
Returns the first element in this list.
E
getLast()
Returns the last element in this list.
int
indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
int
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
ListIterator<E>
listIterator(int index)
Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
boolean
offer(E e)
Adds the specified element as the tail (last element) of this list.
boolean
offerFirst(E e)
Inserts the specified element at the front of this list.
boolean
offerLast(E e)
Inserts the specified element at the end of this list.
E
peek()
Retrieves, but does not remove, the head (first element) of this list.
E
peekFirst()
Retrieves, but does not remove, the first element of this list, or returns null
if this list is empty.
E
peekLast()
Retrieves, but does not remove, the last element of this list, or returns null
if this list is empty.
E
poll()
Retrieves and removes the head (first element) of this list.
E
pollFirst()
Retrieves and removes the first element of this list, or returns null
if this list is empty.
E
pollLast()
Retrieves and removes the last element of this list, or returns null
if this list is empty.
E
pop()
Pops an element from the stack represented by this list.
void
push(E e)
Pushes an element onto the stack represented by this list.
E
remove()
Retrieves and removes the head (first element) of this list.
E
remove(int index)
Removes the element at the specified position in this list.
boolean
remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.
E
removeFirst()
Removes and returns the first element from this list.
boolean
removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element in this list (when traversing the list from head to tail).
E
removeLast()
Removes and returns the last element from this list.
boolean
removeLastOccurrence(Object o)
Removes the last occurrence of the specified element in this list (when traversing the list from head to tail).
E
set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
int
size()
Returns the number of elements in this list.
Object[]
toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
T[]
toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
Örnek için :
Node sınıfını oluşturup içinde data ve node değişkenlerini tanımladık
package com.ozcan.llist;
public class Node {
public int data;
public Node nextNode;
public Node(int data) {
this.data = data;
}}
Daha sonra bu sınıfı kullanacak MyLinkedList class’ını oluşturup seneryoları ve ekleme (add) metodunu yazdık :
package com.ozcan.llist;
public class MyLinkedListTest {
Node head;
Node tail;
//node(data) -> node(data) -> node(data)
//add
//delete
//find
public void add(Node node){
//scenario 1: linkedList is empty bu durumda gelen data il node'u oluşturacak
//tail aynı zamanda ilk işaretçidir. gelen data (Node(5))
if( tail == null)
{
head = node;//ilk eleman olduğu için hem head hem tail node'u gösterdi.
tail=node;
}
else{
//scenario 2: lindekList is not empty bu durum için gelen data sona eklenip tail'e bağlanacaktır.
//add Node(10) [Node(15)->Node(16)] Node(10)'u linkedliste'ye
//ekleyecek olursak burda Node(16)'nın işaretçisi(tail) Node(10)'u gösterecektir.
//Node(15)->Node(16)->Node(10)->
tail.nextNode=node;
tail = node;
}
}
}
Ardından ne yaptığımızı test etmek için bir test sınıfı oluşturup Debug As ->Junit modunda çalıştırıp node’lar ve gösterdikleri değerleri gördük.
package com.ozcan.llist;
import java.util.Iterator;
import org.junit.Test;
public class LinkedListTest {
@Test
//org.junit.Test; kutuphanesınden bu anotation'u çektik.
public void test(){
MyLinkedListTest linkedListTest= new MyLinkedListTest();
linkedListTest.add(new Node(5));
linkedListTest.add(new Node(32));
linkedListTest.add(new Node(20));
System.out.println("Fist data: " +linkedListTest.head.data +" Last Data: " + linkedListTest.tail.data);
}
}
devamı gelecek…