×

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

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Java Strictfp Keyword

Strictfp is used to impose limits on floating-point calculation. It ensures that we will get the same result on every platform while performing an operation with the floating-point variable. The floating-point calculation is platform-dependent due...

1 minute read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

nth Prime Number in Java

A number is considered prime if only divisible by one and itself. Prime numbers include, for example, 2, 3, 5, 7, 11, etc. In looking at it another way, a...

5 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 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.

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.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We...

4 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 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.