Last active
May 1, 2017 18:08
-
-
Save situnamrit/d84b53e4742f7e66d045e5faa4654350 to your computer and use it in GitHub Desktop.
Queue-Dynamic ( Linked list )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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