linkedList start

This commit is contained in:
Libkyy
2022-03-15 22:51:23 +02:00
parent c22ad78dc8
commit e9101d53ea
2 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
package ScuffedLinkedList;
public class linkedlist
{
private node head;
private node tail;
private int size;
linkedlist(int value)
{
this.head = new node(value);
}
public void add(int value, int index)
{
node inserted = new node(value);
if(index == 0)
{
if(head.next == null)
{
}
else
{
inserted.next = head;
head.prev = inserted;
}
}
else if(index <= size)
{
if(index < size/2)
{
int counter = 0;
node previous = head;
node current = previous.prev;
while (counter < index - 1) {
counter++;
current = current.next;
previous = previous.next;
}
inserted.next = current;
inserted.prev = previous;
current.prev = inserted;
previous.next = inserted;
size++;
}
else
{
int counter = size;
node previous = tail;
node current = previous.prev;
while (counter > index) {
counter--;
current = current.prev;
previous = previous.prev;
}
inserted.next = current;
inserted.prev = previous;
current.prev = inserted;
previous.next = inserted;
}
}
else if(index >= size)
{
inserted.prev = tail;
tail.next = inserted;
tail = inserted;
}
size++;
}
}

View File

@@ -0,0 +1,36 @@
package ScuffedLinkedList;
public class node
{
int data;
public node next;
public node prev;
node(int data)
{
this.data = data;
}
node()
{
}
public void setData(int data)
{
this.data = data;
}
public int getData()
{
return data;
}
@Override
public String toString()
{
return String.valueOf(this.data);
}
}