In: Computer Science
JAVA DATA STRUCTURE (Linked Lists/Queue)
public class Node {
int value;
Node nextNode;
Node(int v, Node n){
value = v;
nextNode = n;
}
Node (int v){
this(v,null);
}
}
public class Stack {
protected Node top;
Stack(){
top = null;
}
boolean isEmpty(){
return( top == null);
}
void push(int v){
Node tempPointer;
tempPointer = new Node(v);
tempPointer.nextNode = top;
top = tempPointer;
}
int pop(){
int tempValue;
tempValue = top.value;
top = top.nextNode;
return tempValue;
}
void printStack(){
Node aPointer = top;
String tempString = "";
while (aPointer != null)
{
tempString =
tempString + aPointer.value + "\n";
aPointer =
aPointer.nextNode;
}
System.out.println(tempString);
}
}
public class StackWithLinkedList {
public static void main(String[] args){
int popValue;
Stack myStack = new Stack();
myStack.push(5);
myStack.push(7);
myStack.push(9);
myStack.printStack();
popValue = myStack.pop();
popValue = myStack.pop();
myStack.printStack();
}
}
Add the Queue class to the program introduced above. New Class should be CREATED (-->NOT importing<-- ANY TYPE of CLASS from Java Library, just using the provided classes), You should create Queue class like we created the Stack class(see the CODE above). Create the Queue class with the following specifications:
a. It must create a queue(constructor)
b. Insert one end (enqueue)
c. Do extractions at the other end (dequeue)
d. Determine if queue is empty (isEmpty)
e. Print the content of the queue (printQueue)
f. Finally, you must develop a main class QueueWithLinkedList that
proves that every method works (just like we did with the
StackWithLinkedList class provided in the program
above)
Code for Queue is provided below alongwith the output screenshot. Code is Explained in code comments. If need any further clarification please ask in comments.
############################################################################
CODE-->
//node for storing elemnts
class Node {
int value; //to store data
Node nextNode; //to store next node
//constructor
Node(int v, Node n){
value = v;
nextNode = n;
}
//constructor
Node (int v){
this(v,null);
}
}
//Queue class
class Queue
{
protected Node front,rear; //Node as data member
Queue() //constructor
{
front=null; //empty Queue has fron and rear null
rear=null;
}
//function to check if Queue is empty
boolean isEmpty()
{
return(front==null); //if yes return true
}
//add elemnt in queue
void enqueue(int value)
{
Node tempPointer=new Node(value); //maling node
if(isEmpty()) //checking if Queue ise empty
{
front=rear=tempPointer; //assigning front=rear a newnode
return;
}
rear.nextNode=tempPointer; //if there are elemnts in Queue then add this node to the last
rear=tempPointer;
}
//function of deltion of element
int dequeue()
{
Node temp=front;
if(!isEmpty()) //if Queue is not empty
{
front=front.nextNode; //set fron of node to next of front node
}
if(front==null) //if the Queue has become Empty
rear=null; //then set rear null
return temp.value; //retrurn popped value
}
//function to print Queue
void printQueue(){
Node aPointer = front;
String tempString = "";
while (aPointer != null)
{
tempString = tempString + aPointer.value + "\n";
aPointer = aPointer.nextNode;
}
System.out.println(tempString);
}
}
//main class
public class QueueWithLinkedList {
public static void main(String[] args)
{
Queue queue=new Queue(); //creating object of Queue
System.out.println("IS Queue Empty: "+queue.isEmpty()); //checking if empty
queue.enqueue(5);
queue.enqueue(10); //adding elements
queue.enqueue(15);
queue.enqueue(20);
queue.enqueue(25);
queue.enqueue(30);
System.out.println("After Adding Elements, IS Queue Empty: "+queue.isEmpty());
System.out.println("Queue is : ");
queue.printQueue();
System.out.println("Deleteing Element from Queue: "+queue.dequeue()); //deleting element
System.out.println("Queue After Deletion: ");
queue.printQueue(); //printing Queue
}
}
####################################################################################
OUTPUT