Linked List Itr

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

package linkedlistitr;

public class Node {

int data;
Node next;

public Node(int data){


this.data = data;
}
}

public class LinkedListItr {

Node head;
Node tail;
int size = 0;

public void insert(int data)


{
Node current = head;
if(head == null)
{
head = new Node(data);
size++;
}else {
while(current.next!=null)
{
current = current.next;
}

tail = new Node(data);


current.next = tail;
size++;
}
}

public Node advance(Node current)


{
return current = current.next;
}

public boolean isLast(Node current)


{
if(current.next == null) {
return true;
} else {
return false;
}
}

public boolean isInList(Node current)


{
if(current == null) {
return false;
} else {
return true;
}
}

public void delete(int data)


{
Node current = head;

if(head == null)
{
System.out.println("List is Empty");

}else if(data == head.data){

head = head.next;

}else {

while(!isLast(current))
{
if(current.next.data == data)
{
current.next = current.next.next;
size--;
return;
}
current = advance(current);
}

public void locate(int data)


{
Node current = head;

for (int i = 0; i < size; i++)


{
if(current.data == data)
{
System.out.println("Data found in index " + i);
} else {
current = advance(current);
}
}
}

public void size()


{
System.out.println("List Size : " + size);
}

public void show()


{
Node current = head;

while(current.next!=null)
{
System.out.println(current.data);
current = current.next;
}
System.out.println(current.data);
}

public class MainClass {

public static void main(String[] args) {

LinkedListItr list = new LinkedListItr();

list.insert(5);
list.insert(6);
list.insert(7);
list.insert(8);
list.show();

System.out.println("-------------------");

list.size();
list.locate(8);

You might also like