Created
June 15, 2017 11:53
-
-
Save MericBERBER/f89d6ead2984ce1f47d690b98e1c2558 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 { | |
public int num1; // veriyi tutar. | |
public Node nextLink; // bir sonraki düğümü tutar. | |
//Node constructor | |
public Node(int d1) { | |
num1 = d1; | |
} | |
} | |
class LinkList { | |
private Node head; | |
//LinkList constructor | |
public LinkList() { | |
head = null; | |
} | |
//Listenin boş olup olmadığını kontrol eder. | |
public boolean isEmpty() { | |
return head == null; | |
} | |
// Listenin sonuna eleman ekleme | |
public void add(int d1) { | |
Node link = new Node(d1); | |
if(head == null){ | |
link.nextLink = head; | |
head=link; | |
} | |
else{ | |
Node current = head; | |
Node previus = null; | |
while(current!=null){ | |
previus =current; | |
current = current.nextLink; | |
} | |
current = link; | |
previus.nextLink = link; | |
} | |
} | |
// n. pozisyona eleman ekleme. | |
public void add(int d1, int n) { | |
Node current = head; | |
Node previus = null; | |
Node temp = null; | |
int counter = 0; | |
if(n==0){ | |
Node link = new Node(d1); | |
link.nextLink = head; | |
head = link; | |
} | |
else{ | |
Node link = new Node(d1); | |
while(counter < n){ | |
previus = current; | |
current = current.nextLink; | |
counter ++; | |
} | |
temp=link; | |
previus.nextLink = temp; | |
temp.nextLink = current; | |
} | |
} | |
//Listenin ilk düğümünü silme | |
public Node delete() { | |
Node temp = head; | |
head = head.nextLink; | |
return temp; | |
} | |
//Listenin n. düğümünü silme | |
public Node delete(int n) { | |
Node current = head; | |
Node previus = null; | |
int counter =0; | |
if(n==0){ | |
head=current.nextLink; | |
current=null; | |
} | |
else{ | |
while(counter < n){ | |
previus = current; | |
current = current.nextLink; | |
counter++; | |
} | |
previus.nextLink = current.nextLink; | |
current.nextLink = null; | |
} | |
return head; | |
} | |
//Son düğümün tuttuğu veriyi dönderir. | |
public int last() { | |
Node current = head; | |
if(head==null){ | |
System.out.print("List is empty. "); | |
return 0; | |
} | |
else { | |
while (current.nextLink != null) { | |
current = current.nextLink; | |
} | |
} | |
return current.num1; | |
} | |
// n int verisi tutan düğümün hangi sırada olduğunu dönderir. | |
public int searchPosition(int n) { | |
Node current = head; | |
int counter = 0; | |
int c2 = -1; | |
while(current!=null){ | |
if(current.num1==n){ | |
c2++; | |
return counter; | |
} | |
counter++; | |
current = current.nextLink; | |
} | |
if(c2==-1){ | |
System.out.println(n +" is absend"); | |
} | |
return 0; | |
} | |
// Listedeki düğüm sayısını dönderir. | |
int count() { | |
int counter = 1; | |
Node current = head; | |
while(current.nextLink!=null){ | |
current = current.nextLink; | |
counter++; | |
} | |
return counter; | |
} | |
//Listeyi yazdırır. | |
public void printList() { | |
Node currentLink = head; | |
System.out.print("List: "); | |
while(currentLink != null) { | |
System.out.print("{" + currentLink.num1 + "} "); | |
currentLink = currentLink.nextLink; | |
} | |
System.out.println(""); | |
} | |
} | |
class LinkedList { | |
public static void main(String[] args) { | |
LinkList list = new LinkList(); | |
list.add(1); | |
list.add(2); | |
list.add(3); | |
list.add(4); | |
list.add(5); | |
list.printList(); | |
while(!list.isEmpty()) { | |
Node deletedLink = list.delete(); | |
System.out.print("deleted: "); | |
list.printList(); | |
System.out.println(""); | |
} | |
list.printList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment