Java’da Double Linked List’te mainime “DLL.Swap(3);” satırını eklediğimde listim bundan:
10 20 30 40 50 60 70 80 90
buna dönüşmeli:
10 20 70 40 50 60 30 80 90
Yani baştan 3. sıradaki terimle sondan 3. sıradaki terim yer değiştirmeli ama yapamadım.
Test.java
public class Test {
public static void main(String[] args) {
DoubleLinkedList DLL = new DoubleLinkedList();
DLL.add(10);
DLL.add(20);
DLL.add(30);
DLL.add(40);
DLL.add(50);
DLL.add(60);
DLL.add(70);
DLL.add(80);
DLL.add(90);
DLL.display1();
DLL.display2();
System.out.println(DLL.size());
}
}
DoubleLinkedList.java
public class DoubleLinkedList {
private Node head;
private Node tail;
public DoubleLinkedList() {
head = null;
tail = null;
}
public void add(Integer dataToAdd) {
Node newnode;
if (head == null) { //list is empty
newnode = new Node(dataToAdd);
head = newnode;
tail = newnode;
}
else { //add to the end
newnode = new Node(dataToAdd);
newnode.setPrev(tail);
tail.setNext(newnode);
tail=newnode;
}
}
public void remove(Integer s)
{
if (head == null)
System.out.println("linked list is empty");
else
{
while (((Integer) head.getData()).equals(s)) {
head = head.getNext();
head.setPrev(null);
}
Node temp = head;
while (temp != null)
{
if (((Integer)temp.getData()).equals(s)) {
if (temp.getNext() == null) {
tail = tail.getPrev();
tail.setNext(null);
}
else {
temp.getPrev().setNext(temp.getNext());
temp.getNext().setPrev(temp.getPrev());
}
}
temp=temp.getNext();
}
}
}
public int size()
{
int count = 0;
if (head == null)
System.out.println("linked list is empty");
else {
Node temp = head;
while (temp != null)
{
count++;
temp=temp.getNext();
}
}
return count;
}
public void display1()
{
if (head == null)
System.out.println("linked list is empty");
else {
Node temp = head;
while (temp != null)
{
System.out.print(temp.getData() + " ");
temp = temp.getNext();
}
System.out.println();
}
}
public void display2()
{
if (head == null)
System.out.println("linked list is empty");
else {
Node temp = tail;
while (temp != null)
{
System.out.print(temp.getData() + " ");
temp = temp.getPrev();
}
System.out.println();
}
}
public boolean search(Integer s)
{
boolean flag = false;
if (head == null)
System.out.println("linked list is empty");
else {
Node temp = head;
while (temp != null)
{
if (temp.getData().equals(s)) {
flag = true;
break;
}
temp = temp.getNext();
}
}
return flag;
}
public void display()
{
if (head == null)
System.out.println("linked list is empty");
else {
Node temp1 = head;
Node temp2 = tail;
while (temp1 != temp2 && temp1 != temp2.getNext())
{
System.out.print(temp1.getData() + " ");
System.out.print(temp2.getData() + " ");
temp1 = temp1.getNext();
temp2 = temp2.getPrev();
}
if(temp1 == temp2) System.out.print(temp1.getData());
System.out.println();
}
}
public Object getElement(int x)
{
boolean flag = false;
if (head == null)
{
System.out.println("linked list is empty");
return null;
}
else if(x > size())
{
System.out.println("index is out of range");
return null;
}
else {
Node temp = head;
int count = 1;
while (temp != null)
{
if (count == x) {
return temp.getData();
}
temp = temp.getNext();
count++;
}
}
return null;
}
}
Node.java
public class Node {
private Object data;
private Node prev;
private Node next;
public Node(Object dataToAdd) {
data = dataToAdd;
prev = null;
next = null;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
}