JavaScript data structure and algorithm of the linked list _ basic knowledge

Source: Internet
Author: User
Tags constructor prev

Introduction to the linked list

A linked list is a common data structure and a linear table, but does not store data in a linear order. Instead, a pointer to the next node is stored in each node. Can look at the picture to understand. (C language based on the possibility of better understanding).
The use of a linked-list structure to overcome the need for the array to know the size of the data in advance (the C-language array requires a predefined length), the list structure can make full use of computer memory space to achieve flexible memory dynamic management.
The next step is to introduce two common linked lists: One-way linked list, bidirectional linked list in JavaScript implementation.

One-way linked list

The simplest form of the list is a one-way list, the nodes in the list contain two parts, the first part stores its own information, and the second part stores pointers to the next node. The last node points to null:

Realization of one-way linked list in Javascipt

First, create a constructor.

/**
 * One-way list constructor/function
LinkedList () {
 /**
  * The constructor of a node in a one-way list
  * @param {any} element to pass to the node of the list c10/>*/
 var Node = function (Element) {
  this.element = element;
  The address of the next node
  this.next = null;
 }

 Length of one-way linked list
 var = 0;
 The header node of one-way linked list, initialized to null
 var head = null;
}

It is not difficult to see that one-way linked list constructors are much more complex than stacks and queues.
One-way linked lists need to have the following methods:

    1. Append (Element): adding elements to the tail of the list
    2. Insert (position,element): Inserts an element to a single point in a one-way list
    3. IndexOf (Element): Find the position of an element in a one-way list
    4. Remove (Element): Remove the given element
    5. RemoveAt (position): Removes an element from a single point in a one-way list
    6. GetHead (): Get the head of a one-way list
    7. Isampty (): Check if one-way linked list is empty, NULL returns True
    8. ToString (): Prints all the contents of a list as a string
    9. Size (): Returns the one-way list length

Append method:

Note: Add elements to the tail of a one-way list.
Realize:

/**
 * add element to the tail of one-way list
 * @param {any} element to join the node of the list *
/this.append = function (Element) {
 var node = new Node (element);
 var current;

 if (head = = null) {
  head = node;
 } else {
  //The current item equals the list header
  element. While loop to the last one, the node is added to the tail of the list. Current
  = head;
  When Next is null, the decision is false. Exits the loop. While
  (current.next) {current
   = Current.next;
  }
  current.next = node;
 }
 length++;
};

Insert Method:

Description: Inserts an element into a one-way linked list.
Realize:

/**
 * Inserts an element into a one-way list *
 @param {number} position the position to insert
 * @param {any} element
 * @return {boolean}
    Insert successfully Returns TRUE, Failure returns false
 *
/This.insert = function (position, Element) {
 if (position >= 0 && Position <= length) {
  var node = new node (element);
  var current = head;
  var previous;
  var index = 0;

  if (position = = 0) {
   node.next = current;
   Head = node;
  } else {while
   (index++ < position) {
    previous = current;
    current = Current.next;
   }

   previous.next = node;
   Node.next = current;
  }

  length++;
  return true;
 } else {return
  false;
 }
};

IndexOf Method:

Description: Find the position of an element in a one-way list.
Realize:

/**
 * Find the position of an element in a one-way list
 * @param {any} element to look for
 * @return {number}     return value >=0 to find the corresponding position
. This.indexof = function (Element) {
 var current = head;
 var index =-1;

 while (current) {
  if (element = = current.element) {return
   index;
  }
  index++;
  current = Current.next;
 }

 return-1;
};

Remove method:

Description: Removes the given element.
Realize:

/**
 * Removal of the given element
 * @param {any} element
 * @return {number}     return value >=0 = Remove Success
* This.remove = function (Element) {
 var index = this.indexof (element);
 Return This.removeat (index);

RemoveAt Method:

Description: Removes an element from a location in a one-way linked list.
Realize:

/**
 * Remove an element in a one-way list
 * @param {number} position the location of the element to be removed
 * @return {any} to     remove a successful return of the removed element and return NULL if
 unsuccessful.
this.removeat = function (position) {
 if (position > 1 && Position < length) {
  var current = He AD;
  var previous;
  var index = 0;

  if (position = = 0) {
   //because before head points to the first element, the head is now modified to point to the second element.
   //The core concept is that the linked lists are all linked by pointers, not arrays.
   //So you just need to change the head element. Head
   = current.next;
  } else {
   while (index++ < position) {
    //previous refers to the element before which the element position is to be manipulated, and the element after which current represents.
    previous = current;
    current = Current.next;
   }

   Previous.next = Current.next;
  }

  length--;

  return current.element;
 } else {return
  null;
 }
};

GetHead Method:

Description: Gets the head of the one-way linked list.
Realize:

/**
 * Get the head of one-way list
 * @return {any} The head of a one-way
 list
/this.gethead = function () {return heads
 ;
}

Isampty, toString, size method

Realize:

/**
 * Determine if the one-way list is empty
 * @return {Boolean} is null returns TRUE, NOT NULL returns FALSE *
/this.isampty = function () {return
 Length = = 0
};

/**
 * To output a list of all the contents in a string
 * @return {string} to output strings
/this.tostring = function () {
 var current = Head;
 var string = ';

 while (current) {
  string + = Current.element;
  current = Current.next;
 }
 return string;
};

/**
 * Returns one-way list length
 * @return {number} one-way list length/
this.size = function () {return
 length;
};

Two-way linked list

Two-way linked lists are very similar to one-way lists. In a one-way list, only links to the next node. But in a two-way list, there are links to the previous node, which are bidirectional.

Realization of bidirectional linked list in Javascipt

First, it's still a constructor:

/**
 * bi-directional linked list constructor
/function doublylinkedlist () {
 /**
  * The constructor of a node in a two-way list
  * @param {any} element The element to pass in the list
  *
 /var Node = function (Element) {
  this.element = element;
  This.prev = null;
  This.next = null;
 }

 The length of the two-way linked list
 var = 0;
 The header node of the bidirectional linked list is initialized to null
 var head = null;
 The tail node of a bidirectional linked list, initialized to null
 var tail = null;
}

Two-way linked lists need to have the following methods:

    1. Append (Element): adding elements to the tail of a two-way list
    2. Insert (position,element): Inserts an element into a two-way linked list
    3. RemoveAt (position): Removing elements from a location in a two-way list
    4. Showhead (): Get the head of a two-way list
    5. Showlength (): Get the two-way linked list length
    6. Showtail (): Get the tail of two-way linked list

Append method:

Description: Add elements to the tail of a two-way list
Realize:

/**
 * add element to the tail of
 the list * @param {any} element to join the node of the list
 * @return {any}     join the node of the list * *
this.append = function (Element) {
 var node = new node (element);

 if (head = = null) {head
  = node;
  tail = node;
 } else {
  var previous;
  var current = head;

  while (Current.next) {current
   = Current.next;
  }

  current.next = node;
  Node.prev = current;
  tail = node;
 }

 length++;
 return node;
};

Insert Method:

Description: Inserts an element into a two-way linked list.
Realize:

/**
 * Insert an element into the list *
 @param {number} position the position to insert
 * @return {Boolean}     Insert successfully returns TRUE, Failure returns false
 * */
This.insert = function (position, Element) {
 if (position >= 0 && position <= length) {

  var node = new Node (element);
  var index = 0;
  var previous;
  var current = head;

  if (position = = 0) {

   if (head = = null) {head
    = node;
    tail = node;
   } else {
    current.prev = node;
    Node.next = current;
    Head = node;
   }
  } else if (position = = length) {Current

   = tail;
   current.next = node;
   Node.prev = current;
   tail = node;
  } else {while

   (index++ < position) {
    previous = current;
    current = Current.next;
   }

   previous.next = node;
   Node.prev = previous;
   Current.prev = node;
   Node.next = current;
  }

  length++;
  return true;
 } else {return
  false;
 }
};

RemoveAt Method:

Description: Removes elements from a location in a two-way list.
Realize:

/**
 * Remove an element in the list
 * @param {number} position the location of the element to be removed
 * @return {any} to       remove the removed element successfully and return FALSE if
 unsuccessful.
this.removeat = function (position) {
 if (position > 1 && Position < length) {
  var current = He AD;
  var index = 0;
  var previous;

  if (position = = 0) {head
   = Current.next;

   if (length = = 1) {
    tail = null;
    Head.prev = null;
   }
  } else if (position = = length-1) {current
   = tail;
   tail = Current.prev;
   Tail.next = null;
  } else {while
   (index++ < position) {
    previous = Current.prev;
    current = Current.next;
   }
   Previous.next = Current.next;
   Current.next.prev = previous;
  }

  length--;
  return current.element;
 } else {return
  false;
 }
};

Showhead, Showlength, Showtail methods

Realize:

/**
 * Get the head of the list
 * @return {any} The head of the list
/this.showhead = function () {return heads
 ;
};

/**
 * Get the list length
 * @return {number} chain list length */
this.showlength = function () {return
 length;
};

/**
 * Get the tail of the list
 * @return {any} linked list tail
/this.showtail = function () {return
 tail;
};

Feelings

The link list This section, basically all is writes the code according to the demand first, after writing and then compares with the book. The discovery is simply a moment of seconds into slag. I write a lot of dark pits, logic is also very confusing. It's still too young.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.