Created
June 15, 2017 11:58
-
-
Save MericBERBER/725fae876de22ce66a9631c4b5fe407d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
int data; | |
Node next; | |
Node previus; | |
public Node(int data){ | |
this.data = data; | |
this.next = null; | |
this.previus = null; | |
} | |
} | |
class DoublyLinkedList{ | |
Node head = null; | |
Node tail = null; | |
int size = 0; | |
public Node basaEkle(int n){ | |
Node yeniNode = new Node(n); | |
if(size == 0){ | |
tail = yeniNode; | |
head = yeniNode; | |
} | |
else{ | |
yeniNode.next = head; | |
head.previus = yeniNode; | |
head = yeniNode; | |
} | |
size ++; | |
return yeniNode; | |
} | |
public Node sonaEkle(int n){ | |
Node yeniNode = new Node(n); | |
if(size == 0){ | |
yeniNode = head; | |
yeniNode = tail; | |
}else{ | |
tail.next = yeniNode; | |
yeniNode.previus = tail; | |
tail = yeniNode; | |
} | |
size++; | |
return yeniNode; | |
} | |
public Node arayaEkle(int veri,int sıra){ | |
if(sıra > (size+1)){ | |
System.out.println("Listenin boyutu : " + size); | |
return null; | |
}else if(sıra == (size+1)){ | |
size++; | |
return sonaEkle(veri); | |
}else if(sıra == 1){ | |
size++; | |
return basaEkle(veri); | |
}else{ | |
Node yeniNode = new Node(veri); | |
Node dolasici = head; | |
for(int i = 1; i < sıra; i++){ | |
dolasici = dolasici.next; | |
} | |
yeniNode.previus = dolasici.previus; | |
dolasici.previus.next = yeniNode; | |
dolasici.previus = yeniNode; | |
yeniNode.next = dolasici; | |
size++; | |
return yeniNode; | |
} | |
} | |
public void bastanYazdir(){ | |
Node dolasici = head; | |
while(dolasici != null){ | |
System.out.print(dolasici.data + " - "); | |
dolasici = dolasici.next; | |
} | |
System.out.println(""); | |
} | |
public void terstenYazdir(){ | |
Node dolasici = tail; | |
while(dolasici != null){ | |
System.out.print(dolasici.data + " - "); | |
dolasici = dolasici.previus; | |
} | |
System.out.println(""); | |
} | |
public void bastanSil(){ | |
if(size == 0){ | |
System.out.println("Liste boş"); | |
return; | |
}else{ | |
head = head.next; | |
size--; | |
} | |
} | |
public void sondanSil(){ | |
if(size == 0){ | |
System.out.println("Liste boş"); | |
return; | |
}else if(size == 1){ | |
bastanSil(); | |
}else{ | |
Node prevTail = tail.previus; | |
tail = prevTail; | |
tail.next = null; | |
size--; | |
} | |
} | |
public void aradanSil(int sıra){ | |
if(sıra == 1){bastanSil();} | |
else if(sıra == size){sondanSil();} | |
else{ | |
Node dolasici = head; | |
for(int i = 1; i < sıra; i++){ | |
dolasici = dolasici.next; | |
} | |
dolasici.previus.next = dolasici.next; | |
dolasici.next.previus = dolasici.previus; | |
dolasici.previus = null; | |
dolasici.next = null; | |
size--; | |
} | |
} | |
} | |
class Main{ | |
public static void main(String Args[]){ | |
DoublyLinkedList liste = new DoublyLinkedList(); | |
liste.basaEkle(10); | |
liste.basaEkle(20); | |
liste.basaEkle(30); | |
System.out.println(" ---- Başa eklemeden sonra liste ----- " ); | |
liste.bastanYazdir(); | |
liste.sonaEkle(40); | |
liste.sonaEkle(50); | |
liste.sonaEkle(60); | |
System.out.println(" ---- Sona eklemeden sonra liste ----"); | |
liste.bastanYazdir(); | |
liste.arayaEkle(15,3); | |
System.out.println(" ---- Araya (3. Düğüme) eklemeden sonra liste ----"); | |
liste.bastanYazdir(); | |
liste.aradanSil(4); | |
System.out.println(" ----- Aradan (4. Düğüm) silmeden sonra liste ----"); | |
liste.bastanYazdir(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment