linkedList start
This commit is contained in:
80
src/ScuffedLinkedList/linkedlist.java
Normal file
80
src/ScuffedLinkedList/linkedlist.java
Normal 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++;
|
||||
}
|
||||
}
|
||||
36
src/ScuffedLinkedList/node.java
Normal file
36
src/ScuffedLinkedList/node.java
Normal 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user