Let's start by defining a simple class with a constructor that initializes the head to null. We'll also define another structure on the prototype of the LinkedList class that'll represent each node in the linked list.Exampleclass LinkedList { constructor() { this.head = null; this.length = 0; } } LinkedList.prototype.Node = class { constructor(data) { this.data = data; this.next = null; } }Let's also create a display function that'll help us see how our list looks like. This function works as follows.It starts from the head.It iterates ... Read More
Yes. Java supports labeled statements. You can put a label before a for statement and use the break/continue controls to jump to that label. ExampleSee the example below.Live Demopublic class Tester { public static void main(String args[]) { first: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++){ if(i == 1){ continue first; ... Read More
We need to create a function insert(data, position) that inserts data at given position in the linked list. We'll perform the following steps −Create a new NodeCheck if the list is empty. If it then adds the node to head and return.If not, then we'll iterate to the position we want to insert it to using currElem. We iterate a linked list by making currElem equal to currElem.next.Then we'll make node point to the next node in the list. This is to keep track of the rest of the list.Finally, we break the link from currElem to rest of the ... Read More
Use the global error handler to receive a few of the parameters that jQuery can provide. After that add the suppressErrors: true. You can try to run the following code to disable some jQuery global Ajax event handlers for a request:Live Demo $(document).ready(function(){ $("div.log").ajaxError(function(evt, xhr, settings) { if(settings.suppressErrors) { return; } $(this).text( "ajaxError handler is triggered" ); }); $("button.trigger").click(function() { $("div.log").text(''); $.ajax("ajax/missing.html"); }); $("button.triggerSuppress").click(function() { $("div.log").text(''); $.ajax( "ajax/missing.html", { suppressErrors: true }); }); }); Trigger (process errors) Trigger (won't process errors) Check log
To handle jQuery AJAX error. The ajaxError( callback ) method attaches a function to be executed whenever an AJAX request fails. This is an Ajax Event.Here is the description of all the parameters used by this method −callback − The function to execute. The XMLHttpRequest and settings used for that request are passed as arguments to this function. A third argument, an exception object, is passed if an exception occured while processing the request.The following is an example showing the usage of this method:Live Demo Handling errors in jQuery ... Read More
Though Arrays in JavaScript provide all the functionality of a Queue, let us implement our own Queue class. Our class will have the following functions − enqueue(element): Function to add an element in the queue. dequeue(): Function that removes an element from the queue. peek(): Returns the element from the front of the queue. isFull(): Checks if we reached the element limit on the queue. isEmpty(): checks if the queue is empty. clear(): Remove all elements. display(): display all contents of the arrayLet's start by defining a simple class with a constructor that takes the max size of the queue and a helper function that'll help us ... Read More
Dequeuing elements from a Queue means removing them from the front/head of the queue. We are taking the start of the container array to be the head of the queue as we'll perform all operations with respect to it.Hence, we can implement the pop function as follows − Exampledequeue() { // Check if empty if (this.isEmpty()) { console.log("Queue Underflow!"); return; } return this.container.shift(); }You can check if this function is working fine using − Examplelet q = new Queue(2); q.dequeue(); q.enqueue(3); q.enqueue(4); console.log(q.dequeue()); q.display();OutputThis will give the output −Queue Underflow! 3 [ ... Read More
Peeking a Queue means getting the value at the head of the Queue. So we can implement the peek function as follows − EXamplepeek() { if (isEmpty()) { console.log("Queue Underflow!"); return; } return this.container[0]; }You can check if this function is working fine using − Examplelet q = new Queue(2); q.enqueue(3); q.enqueue(4); console.log(q.peek()); q.display();OutputThis will give the output −3 [ 3, 4 ]As you can see here, peek() differs from dequeue in that it just returns the front value without removing it.
We can clear the contents just by reassigning the container element to an empty array. For example, clear() { this.container = []; }ExampleYou can check if this function is working fine using:let q = new Queue(2); q.enqueue(3); q.enqueue(4); q.display(); q.clear(); q.display();OutputThis will give the output:[ 3, 4 ] [ ]
Here is the complete implementation of the Queue class −Exampleclass Queue { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; } this.maxSize = maxSize; // Init an array that'll contain the queue values. this.container = []; } // Helper function to display all values while developing display() { console.log(this.container); } // Checks if queue is empty isEmpty() { ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP