package linkd;
import java.util.Scanner;
/**
*
* @author IZAN
*/
public class circuler {
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
System.out.print("Berapa data yang akan dimasukkan:");
int max = sc.nextInt();
QueueLinkList queue = new QueueLinkList(max);
int pilihan=0;
while(pilihan != 3)
{
System.out.println("Menu");
System.out.print("1.insert\n2.remove\n3.exit\npilihan Anda:");
pilihan = sc.nextInt();
switch (pilihan)
{
case 1 : System.out.println("insert queue:");
System.out.print("Masukkan data:");
int temp;
temp = sc.nextInt();
queue.enqueue(temp);
queue.display();
break;
case 2 : System.out.println("remove queue:");
queue.dequeue();
queue.display();
break;
case 3 : System.out.println("exit");
}
}
}
}
class item //atau node/simpul
{
public int data;
public item next; // next node link in list
public item prev; // previous node link in list
public item(int id) // constructor
{
data = id; // initialize data
}
public void displayLink() // display ourself
{
System.out.print("{" + data + "} ");
}
}
class QueueLinkList
{
private item front; // ref to first link on list
private item rear; // ref to last link on list
int jumlah;
int max;
public QueueLinkList(int m ) // constructor
{
max = m;
jumlah = 0;
front = rear = null; // no items on list yet
}
public boolean isEmpty() // true if list is empty
{
return (front==null);
}
public void enqueue(int id) //node baru selalu di top // void hanya sesuai prosedur hanya langkah2 dan untuk dirinya sendiri
{
item newitem = new item(id);
if (jumlah < max)
{
if (front == null) // the first node created
{
front = rear = newitem; // first --> newLink
newitem.next = front;
newitem.prev = front;
}
else // the second node and the next node
{
rear.next = newitem; //next dr top (awal) diarahkan ke node baru
newitem.prev = rear; //prev dr node baru diarahkan ke tail (awal)
newitem.next = front; //buat circular
front.prev = newitem;
rear = newitem; //top (baru) diarahkan ke node baru
}
jumlah++;
}
else
{
System.out.println("Kapasitas penuh. Terjadi pergesaran item");
front=front.next;
front.prev=newitem;
newitem.next=front;
rear.next=newitem;
newitem.prev=rear;
rear=newitem;
}
}
public item dequeue() // delete first item
{
item temp = null;
if (front == null) // queue is empty
System.out.println("Queue is empty");
else if (front == rear) // queue is only one data
{
temp = front;
front = rear = null;
jumlah--;
}
else // queue has more than one data
{
temp = front; // save reference to link
front = front.next; // delete it: first-->old next
rear.next = front;
front.prev = rear;
jumlah--;
}
return temp;
}
public void display()
{
item current = front; // start from the first data
if (current == null) //queue kosong
System.out.println("The Queue is empty");
else if (current.next == front) //node hanya 1
{
current.displayLink();
}
else //if (current.next != front)
{
for(;current.next != front;) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
current.displayLink();
}
System.out.println("");
}
} // end class LinkList