Sei sulla pagina 1di 3

SINGLY LINKED LIST

import java.util.*; /* Function to get size of list */


class Node public int getSize()
{ {
protected int data; return size;
protected Node link; }
/* Constructor */ /* Function to insert an element at begining */
public Node() public void insertAtStart(int val)
{ {
link = null; Node nptr = new Node(val, null);
data = 0; size++ ;
} if(head == null)
/* Constructor */ {
public Node(int d,Node n) head = nptr;
{ tail = head;
data = d; }
link = n; else
} {
/* Function to set link to next Node */ nptr.setLink(head);
public void setLink(Node n) head = nptr;
{ }
link = n; }
} /* Function to insert an element at end */
/* Function to set data to current Node */ public void insertAtEnd(int val)
public void setData(int d) {
{ Node nptr = new Node(val,null);
data = d; size++ ;
} if(head == null)
/* Function to get link to next node */ {
public Node getLink() head = nptr;
{ tail = head;
return link; }
} else
/* Function to get data from current Node */ {
public int getData() tail.setLink(nptr);
{ tail = nptr;
return data; }
} }
} /* Function to insert an element at position */
public void insertAtPos(int val , int pos)
/* Class linkedList */ {
class linkedList Node nptr = new Node(val, null);
{ Node ptr = head;
protected Node head; pos = pos - 1 ;
protected Node tail ; for (int i = 1; i < size; i++)
public int size ; {
/* Constructor */ if (i == pos)
public linkedList() {
{ Node tmp = ptr.getLink() ;
head = null; ptr.setLink(nptr);
tail = null; nptr.setLink(tmp);
size = 0; break;
} }
/* Function to check if list is empty */ ptr = ptr.getLink();
public boolean isEmpty() }
{ size++ ;
return head == null; }
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
head = head.getLink();
size--;
return ;
}
if (pos == size)
{
Node s = head;
Node t = head;
while (s != tail)
{
t = s;
s = s.getLink();
}
tail = t;
tail.setLink(null);
size --;
return;
}
Node ptr = head;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
/* Function to display elements */
public void display()
{
System.out.print("\nSingly Linked List = ");
if (size == 0)
{
System.out.print("Kosong\n");
return;
}
if (head.getLink() == null)
{
System.out.println(head.getData() );
return;
}
Node ptr = head;
System.out.print(head.getData()+ "->");
ptr = head.getLink();
while (ptr.getLink() != null)
{
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ "\n");
}
}
public class SinglyLinkedList {

public static void main(String[] args) {


System.out.println("Singly Linked List");
System.out.println("==================");
/* Creating object of class linkedList */
linkedList list = new linkedList();
Scanner scan = new Scanner(System.in);
char ch;
/* Perform list operations */
do
{
System.out.println("\nPilih salah satu");
System.out.println("1. Tambah di awal");
System.out.println("2. Tambah di akhir");
System.out.println("3. Tambah di posisi tertentu");
System.out.println("4. Hapus di posisi tertentu");
System.out.println("5. Cek List");
System.out.print("\nMasukan Pilihan Anda ");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.print("Masukan Data (integer) : ");
list.insertAtStart( scan.nextInt() );
break;
case 2 :
System.out.print("Masukan Data (integer) : ");
list.insertAtEnd( scan.nextInt() );
break;
case 3 :
System.out.print("Masukan Data (integer) : ");
int num = scan.nextInt() ;
System.out.print("Dimasukan indeks keberapa? ");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getSize() )
System.out.println("Indeks Tidak Valid\n");
else
list.insertAtPos(num, pos);
break;
case 4 :
System.out.print("Indeks keberapa?");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Indeks Tidak Valid\n");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.print("Size = "+ list.getSize());
break;
default :
System.out.println("Pilihan Salah \n ");
break;
}

/* Display List */
list.display();
System.out.println("\nAnda ingin mengulang (Ketik Y atau T) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}

Potrebbero piacerti anche