Javascript Data Structure List Linked List 2

Introduction

function Node(element) {
  this.element = element;// ww  w  . j  a  v a  2 s.  c  o m
  this.next = null;
}
function LList() {
  this.head = new Node("head");
  this.find = find;
  this.insert = insert;
  // this.remove = remove;
  this.display = display;
}

function find(item) {
  let currNode = this.head;
  while (currNode.element != item) {
    currNode = currNode.next;
  }
  return currNode;
}

function insert(newElement, item) {
  let newNode = new Node(newElement);
  let current = this.find(item);
  newNode.next = current.next;
  current.next = newNode;
}

function display() {
  let currNode = this.head;
  while (!(currNode.next == null)) {
    console.log(currNode.next.element);
    currNode = currNode.next;
  }
}

// main program

let cities = new LList();
cities.insert("C", "head");
cities.insert("R", "C");
cities.insert("S", "R");
cities.display()



PreviousNext

Related