Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Queue using linked list
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Node{ int data; Node next=null; public Node(int data){ this.data = data; } } class Queue{ Node front=null; Node rear=null; void enqueue(int data){ if(rear==null){ Node n = new Node(data); front = n; rear = n; } else{ Node n = new Node(data); rear.next = n; rear = n; } } public Node dequeue(){ Node x=null; if(front==null){ System.out.println("Queue is empty"); return null; } x = front; front = front.next; if(front==null){ rear = null; } return x; } public void show(){ Node temp = front; while(temp.next!=null){ System.out.print(temp.data + "->"); temp = temp.next; } System.out.print("\n"); } } class Rextester { public static void main(String args[]) { Queue q = new Queue(); q.enqueue(1); q.enqueue(3); q.enqueue(4); q.enqueue(5); q.enqueue(7); q.enqueue(0); q.enqueue(2); q.show(); System.out.println(q.dequeue().data); System.out.println(q.dequeue().data); q.show(); } }
run
|
edit
|
history
|
help
0
Ifelsenested
Feet-inches input validation
First Unique Character in a String
Fibonacci 2
Minimo quadrato somma di cubi
Problem: Single_digit
3.B
log
output1
Date_From_Str