×

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last.

Rear is moved to the next node by the operation enQueue(), which also adds a new node after it.

deQueue() This operation advances the front node to the subsequent node after removing the front node.

Approach:

To implement the queue using a linked list, follow these steps:

  • Next, make a Struct QNode with the data elements QNode* and integer data.
  • A parameterized constructer that sets the data to equal the integer value x and then next as NULL.
  • A struct Queue with the data elements QNode front and rear should be created.
  • The front and rear are set to NULL by default constructor Queue().
  • Operation to enqueue with parameter x:
  • Set front and rear to temp and return after initialising QNode* temp with data = x if rear is set to NULL.
  • If not, place the rear's next to the temp before moving the rear to the temp variable.
  • If not, place the rear's next to the temp before moving the rear to the temp variable. Dequeue Operation:
  • If front is set to NULL, return from the dequeue operation.
  • Set front to its next while initialising QNode* temp with front.
  • Set rear to NULL if front is equal to NULL.
  • Delete the temp variable

Insertion

If the queue is empty, the insert operation appends it by adding an element to the end of it. In order to make the new element the last one in the queue, we place it into an empty queue. The statement front = NULL is true in this instance. The front and rear pointers' next pointers will now both point to NULL, making the new element the only element in the queue. The condition is no longer true if front = NULL. It is necessary to update the end pointer rear so that the next pointer of rear now points to the new node next pointer.

Deletion

The element that was first inserted out of all the queue elements is removed during deletion operations. We must find whether the queue is empty or not. The condition for this is (front == NULL) if it becomes true then queue is empty, then we just print underflow on the console. If not, we shall remove the object that the pointer front is pointing at. Copy the node pointed by the front pointer into the pointer next for this purpose. After that, move the front pointer, point to the node after it, and release the node that the node next pointed at.

Queue implementation using Linked List

// Node creation to store new values using class QNode1
class QNode1 {
int k1 ;
QNode1 next ;


public QNode1 ( int k1 )
{
this . k1 = k1 ;
this . next = null ;
}
}
class QueueImplementation {
QNode1 f, r ;
public QueueImplementation ( ) { this.f = this.r = null; }
void enqueue ( int k1 )
{
QNode1 temp = new QNode1 ( k1 ) ;
if (this . r == null ) {
this . f = this . r = temp ;
return ;
}
this . r . next = temp ;
this . r = temp ;
}
void dequeue()
{ if ( this . f == null)
return ;
QNode1 temp = this.f;
this.f = this.f.next;
if ( this . f == null )
this . r = null ;
}
}


// Main section of the program from where execution begins
public class Main {
public static void main ( String [ ] args )
{
QueueImplementation q1 = new QueueImplementation ( ) ;
q1 .enqueue ( 30 ) ;
q1 .enqueue ( 40 ) ;
q1 .dequeue () ;
q1 .enqueue ( 70 ) ;
q1 .dequeue () ;
q1 .enqueue ( 10 ) ;
q1 .enqueue ( 80 ) ;
q1 .dequeue ( ) ;
System . out . println(" Front  of the queue : " + q1 . f . k1 ) ;
System . out . println ( " Rear of the queue : " + q1 . r . k1 ) ;
}
}

Output

Various Operation on Queue using Linked List in Java

Related Topics

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Java Rename File

Renaming a file is the process of changing its name. Using the renameTo() function of the Java File class, renaming operations are possible. A file can be renamed using Java's renameTo()...

3 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Streams in Java

The conventional Java SE 8 version came with many new peculiarities, out of which the most striking of which are assuredly lambda expressions and the method references. Streams and Streams...

14 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.

Series Program in Java

Series Program in Java The series program in Java is written to print the mathematical series such as the Fibonacci series, Pell series, etc. A few of the renowned series are...

12 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.