This tutorial is all about how to create a queue function in Java. This function is use to insert and remove and element but in Queue, the element is inserted at the end of the queue and remove the element at the beginning of the queue.
In creating this program, you need to create a 3 files. Two java class for your java codes and 1 text file for your data. In my case I use the following filenames:
![]() |
Image: Wikipedia |
In creating this program, you need to create a 3 files. Two java class for your java codes and 1 text file for your data. In my case I use the following filenames:
- QueueBook.Java
- QueueRun.Java
- what_Queue.Txt
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.io.File; | |
import java.util.Scanner; | |
import java.util.StringTokenizer; | |
/** | |
* | |
* @author Jillian | |
*/ | |
public class QueueBook { | |
/* | |
Step 1: create new Book named front and back - LINE 24,25 | |
Step 2: create a new class named Book - LINE 26, 34 | |
Step 3: Declare next as new Book - LINE 29 | |
Step 4: Declare data as new String - LINE 28 | |
Step 5: Create a new method named Book and includes parameters - LINE 30 TO 33 | |
*/ | |
Book front; | |
Book back; | |
class Book{ | |
//String name; | |
String data; | |
Book next; | |
Book(String id,String title,String author,String publisher,String year){ | |
data = id + " | " + title + " | " + author + " | " + publisher + " | " + year; | |
next = null; | |
} | |
} | |
/* | |
Step 1: Create a string method named enqueue with parameters - LINE 40 TO 53 | |
Step 2: declare newBook as new Book and initialized it with parameters - LINE 41 | |
Step 3: declare val as String and initialized it with enqueued--> - LINE 42 | |
Step 4: create if else statment - line 44 to 52 | |
Step 5: in if block, initialized the front and back with newBook if isEpty - line 46 to 48 | |
Step 6: in else block, initialized the front and back with newBook if not Epty - line 51 to 53 | |
Step 7: return the val value - line 56 | |
*/ | |
public String enqueue(String id,String title,String author,String publisher,String year){ | |
Book newBook = new Book(id,title,author,publisher,year); | |
String val = "enqueued--> "; | |
if(isEmpty()){ | |
this.front = newBook; | |
this.back = newBook; | |
val += newBook.data; | |
}else{ | |
newBook.next = this.back; | |
this.back = newBook; | |
val += newBook.data; | |
} | |
return val; | |
} | |
/* | |
Step 1: Create a string method named dequeue with parameters - line 67 to 86 | |
Step 2: declare frontBook as new Book and initialized it with front - LINE 68 | |
Step 3: declare val as String and initialized it with dequeued--> - LINE 69 | |
Step 4: create if else statment - line 71 to 84 | |
Step 5: in if block, display Noting no records found if empty - line 72 | |
Step 6: in else if block, initialized the front and back with null if temp is equls to front - line 73 to 77 | |
Step 7: in else block, create a while loop to check if temp and front is not equal then if true, initialized temp with temp.next | |
Step 8: initilized front with temp - line 82 | |
Step 9: initialized front.next with null - line 85 | |
Step 10: initialized val variable with frontBook.data - line 87 | |
Step 11: return val value - line 89 | |
*/ | |
public String dequeue(){ | |
Book frontBook = front; | |
String val = "dequeued--> "; | |
Book temp = this.back; | |
if(isEmpty()){ | |
val += "Nothing...No records found"; | |
}else if(temp == front){ | |
front = null; | |
back = null; | |
val += frontBook.data; | |
}else{ | |
while(temp.next !=front){ | |
temp = temp.next; | |
} | |
front = temp; | |
front.next = null; | |
val += frontBook.data; | |
} | |
return val; | |
} | |
private boolean isEmpty(){ | |
return this.front == null || this.back==null; | |
} | |
/* | |
Step 1: create a new method named whatThis - line 102 | |
Step 2: inside the method, declare file as new File and initialized it with a file path - line 103 | |
Step 3: declare sc as new Scanner and initialized using the file - line 104 | |
Step 4: create a while loop to check if the scanner element - line 105 | |
Step 5: inside the loop, create tks as new String tokenizer - line 106 | |
Step 6: create a while loop to separate the string - line 107 to 118 | |
*/ | |
public void whatThis()throws Exception{ | |
File file = new File("D:\\MIX FILES\\Assorted Files\\DataStructure\\DataStructure2\\DataStucture\\src\\Queues\\what_Queue.txt"); | |
Scanner sc = new Scanner(file); | |
while (sc.hasNextLine()){ | |
StringTokenizer tks = new StringTokenizer(sc.nextLine(), ","); | |
while (tks.hasMoreTokens()){ | |
String problem = tks.nextToken(); | |
String implemen = tks.nextToken(); | |
String subject = tks.nextToken(); | |
String programmer = tks.nextToken(); | |
System.out.println("--------------------------------------"); | |
System.out.println("Problem Domain: " + problem); | |
System.out.println("Implementation: " +implemen); | |
System.out.println("Subject: " +subject); | |
System.out.println("Programmer: " +programmer); | |
} | |
} | |
} | |
} |
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.io.File; | |
import java.util.Scanner; | |
import java.util.StringTokenizer; | |
/** | |
* | |
* @author Jillian | |
*/ | |
public class QueueRun { | |
/* | |
Step 1: create a main method - line 28 to 45 | |
Step 2: inside the method, declare file as new File and initialized it with a file path - line 30 | |
Step 3: declare sc as new Scanner and initialized using the file - line 31 | |
Step 4: create a while loop to check if the scanner element - line 32 | |
Step 5: inside the loop, create stl as new String tokenizer - line 33 | |
Step 6: create a while loop to separate the string - line 34 to 41 | |
Step 7: display book using dequeue or enqueue - line 43 | |
Step 8: display book whatThis method - line 44 | |
*/ | |
public static void main(String[] args) throws Exception{ | |
QueueBook Book = new QueueBook(); | |
File file = new File("D:\\MIX FILES\\Assorted Files\\DataStructure\\DataStructure2\\DataStucture\\src\\DoublyLinkedList2\\bookList.txt"); | |
Scanner sc = new Scanner(file); | |
while (sc.hasNextLine()){ | |
StringTokenizer st1 = new StringTokenizer(sc.nextLine(), ","); | |
while (st1.hasMoreTokens()){ | |
String callNum = st1.nextToken(); | |
String title = st1.nextToken(); | |
String author = st1.nextToken(); | |
String publisher = st1.nextToken(); | |
String year = st1.nextToken(); | |
System.out.println(Book.enqueue(callNum,title,author,publisher,year)); | |
} | |
} | |
System.out.println(Book.dequeue()); | |
Book.whatThis(); | |
} | |
} |
0 Comments