Creating a Linked List Using JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 09:07:26

342 Views

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

Java Labelled Statement

Kumar Varma
Updated on 15-Jun-2020 09:06:36

4K+ Views

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

Add Elements to a Linked List using JavaScript

Samual Sam
Updated on 15-Jun-2020 09:06:10

2K+ Views

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

Disable jQuery Global Ajax Event Handlers for a Request

David Meador
Updated on 15-Jun-2020 09:06:07

595 Views

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

Handle jQuery AJAX Error

David Meador
Updated on 15-Jun-2020 09:04:44

2K+ Views

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

Creating a Queue in JavaScript

Samual Sam
Updated on 15-Jun-2020 09:02:25

757 Views

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

Remove Elements from a Queue Using JavaScript

Samual Sam
Updated on 15-Jun-2020 08:59:21

981 Views

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 Elements from a Queue in JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 08:57:26

744 Views

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.

Clearing Elements of the Queue in JavaScript

Samual Sam
Updated on 15-Jun-2020 08:56:02

428 Views

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 ] [ ]

The Queue Class in JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 08:55:35

319 Views

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

Advertisements