Skip to content

Instantly share code, notes, and snippets.

@situnamrit
Last active May 1, 2017 18:08
Show Gist options
  • Save situnamrit/d84b53e4742f7e66d045e5faa4654350 to your computer and use it in GitHub Desktop.
Save situnamrit/d84b53e4742f7e66d045e5faa4654350 to your computer and use it in GitHub Desktop.
Queue-Dynamic ( Linked list )
import java.util.Scanner;
class Node{
{
int info;
Node next;
}
public class DynamicQueue
{
static Scanner sc = new Scanner(System.in);
static Node front = null;
static Node rear = null;
public static void insert()
{
int option;
while(option!=0)
{
System.out.println("Enter info: ");
Node curr = new Node();
Node.info = sc.nextInt();
if(front == null)
{
front = curr;
rear = curr;
}
else
{
rear.next = curr;
rear = curr;
}
curr.next = null;
System.out.println("Enter 0 to exit: or else enter 1 ");
option = sc.nextInt();
}
}
public static void delete() {
if(front == null) {
System.out.println("Underflow!");
return;
}
else
front = front.next;
}
public static void display() {
Node p;
p = front;
if(front == null) {
System.out.println("Underflow!");
return;
}
System.out.print("front = ");
for(; p != null; p = p.next)
System.out.print(p.info + " <,<,.... ");
System.out.print(" = rear");
}
public static void main(String[] args) {
int choice;
while( choice!=4 )
{
System.out.println("\nMenu: \n");
System.out.print("1. Insert\n2. Delete\n3. Display\n4. Exit\n\nEnter your choice(1 - 4): ");
choice = read.nextInt();
switch(choice) {
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: System.exit(0);
default: System.out.println("Invalid choice! Try again.\n");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment