Posts

Showing posts with the label queue

Implement Queue using Linked List - The Coding Shala

Home >> Data Structure >> Implement queue using linked list  In this post, we will learn how to implement Queue using Linked List and will write a Java Program for the same. Implement Queue using Linked List We will implement Queue using Linked List. The basic operation of the queue like push/offer and remove/poll method will implement here. Java Program:  class QueueNode { int data ; QueueNode next ; QueueNode ( int a ) { data = a ; next = null ; } } class MyQueue { QueueNode front , rear ; // This function should add an item at // rear void push ( int a ) { QueueNode newNode = new QueueNode ( a ); if ( front == null || rear == null ) { front = newNode ; rear = front ; } else { rear . next = newNode ; rear = rear . next ; } } // This function should remove front // item from queue and should re...

Implement Queue Using Array - The Coding Shala

Home >> Data Structures >> Implement Queue Using Array  In this post, we will learn how to Implement Queue Using Array and will write a Java program for the same. Implement Queue Using Array We will write a simple Java Program to implement queue using the array, Only Push and Pop method we will implement here. Java Program:  class MyQueue { int front , rear ; int arr [] = new int [ 100005 ]; MyQueue () { front = 0 ; rear = 0 ; } /* The method push to push element into the queue */ void push ( int x ) { arr [ rear ] = x ; rear ++; } /* The method pop which return the element poped out of the queue*/ int pop () { if ( front == rear ) return - 1 ; int res = arr [ front ]; front ++; return res ; } } Other Posts You May Like Queue Data Structure Circular Queue Data Structure Stack Data Structure Implement Stack Using Array Implem...

Number of Recent Calls LeetCode Solution - The Coding Shala

Home >> LeetCode >> Number of Recent Calls  In this post, we will learn how to solve LeetCode's Number of Recent Calls Problem and will implement its solution in Java. Number of Recent Calls Problem You have a RecentCounter class that counts the number of recent requests within a certain time frame. Implement the RecentCounter class: RecentCounter() Initializes the counter with zero recent requests. int ping(int t) Adds a new request at time t, where t represents some time in milliseconds and returns the number of requests that have happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t]. It is guaranteed that every call to ping uses a strictly larger value of t than the previous call. Example 1: Input: ["RecentCounter", "ping", "ping", "ping", "ping"] [[], [1], [100], [3001], [3002]] Output: [null, 1, ...

Java Queue - The Coding Shala

Home >> Learn Java >> Java queue Java Queue In this post, we will discuss what is Java Queue and how to implement it in Java? Java Queue is a part of Java Collections.  Queues are an important data structures when we want to maintain the order. Queue works on FIFO(First in First Out) concept. The two important operations in queue is enqueue(insert) and dequeue(delete).  Java Queue Implementation The following java program explains the basic operations of the Java Queue:  import java.util.LinkedList ; import java.util.Queue ; public class Main { public static void main ( String [] args ) { //initialize queue Queue < Integer > queue = new LinkedList <>(); //Print Queue System . out . println ( "Queue is: " + queue ); //push elements in Queue //we use add() and offer() method //the only difference between them is throwing exception queue . add ( 1 ); queue . add ( 2 ); ...

Queue Data Structure - The Coding Shala

Home >> Data Structures >> Queue Queue Data Structure In this post, we will see the basics of the queue. The queue  is a Data Structure, also known as the First-in-first-out data structure(FIFO). In a queue, there are two main operations: Enqueue and Dequeue. The insert Operation is called enqueue and the new element is always added at the end of the queue. The delete operation is called dequeue and we only allowed to remove the first element. Queue Java Program The following is a simple implementation of the queue:  import java.util.ArrayList ; import java.util.List ; //Queue implementation class MyQueue { //to store data private List < Integer > data ; private int q_start ; //start point of queue //initial state MyQueue (){ data = new ArrayList < Integer >(); q_start = 0 ; } //Enqueue Operation //add element at the end of queue are return true if successful public boolean EnQueue ( ...