Wild Front end data Structure Basics Exercise (3)--list

Source: Internet
Author: User

There are a lot of tutorials on the web, and the basic knowledge is self-searching.

The exercises are mainly selected from the book "Data structure and algorithm JavaScript description" published by Orelly.

Reference code visible: Https://github.com/dashnowords/blogs/tree/master/Structure/List

Basic knowledge of linked lists
    • Characteristics:

      A list consists of nodes, each of which adds a reference to an object to its successor node. Which is to 链表 convert a linear table into a table that is discontinuous on a storage space and that can be accessed continuously at the abstraction level.

    • Use:

      Faster insertions and deletions, because only the actions are inserted to delete adjacent elements, and if the elements in the middle position are manipulated in a linear table, subsequent element positions need to be adjusted. Applications such as the prototype chain in JavaScript.

    • Basic properties

      • elementThe value of the current node
      • nextNext node
    • Basic methods

      • insert(item, newitem)Insert a new element after the item NewItem

        Inserting an element requires the item element node to point to the next new element, and the new element points to the next successor element of the item element.

      • remove(pos)Remove an element from the team header

        When deleting a node, it is necessary to point its predecessor node to next its successor.

      • find(element)The location of the node where the query value is element

      • findpre(element)The previous node of a node with a query value of element

      • display()Show entire list

Basic Exercises
    1. Implement a class based on the underlying characteristics of the list LinkedList , and use it in subsequent topics when you need a linked list.

      Note: When you delete a specified element, you need to modify a pointer to the previous node of the specified element, next so the search method should return its previous node for use by the next step when the node being found exists.

    2. Implements a doubly linked list TwoWayLinkedList class.

      "Note point": each instance will record the precursor node and the successor node, the doubly linked list increases the ability of the reverse traversal than the single-linked list, and because the attributes of the found node contain the information of the precursor and the successor, the same search method is used when inserting nodes and deleting nodes.

    3. The class LinkedList is the reference datum to implement a circular chain list CircularLinkedList class.

      "Note point": The circular linked list is characterized by the next pointer of the tail node pointing to the head node.

After-school exercises (sixth in the book)
    1. Implement Advance(n) the method to move the node forward by N nodes.
    2. Implements a back(n) method that moves the node backward by n nodes.
    3. Implementation show() method that displays only the data on the current node.
    4. Slightly
    5. Slightly
    6. Legend has it that during the Jewish War of 1th century, Jewish historian Flavio Josephs and his 40 compatriots were surrounded by Roman soldiers, and Jewish soldiers decided to commit suicide rather than captive, so they negotiated a suicide plan. They were in a circle, starting with a person, counting to the third person to kill the third man, and then counting, until all the people were killed, and Joseph and the other decided not to take part in this crazy game, they quickly calculated two positions and stood there to survive. Write a paragraph to surround the n person, and the m person will be killed, calculate which two people in a lap will end up in stock and use a circular list to solve the problem.
Exercise Ideas
    1. Move forward n position, when the location validation is legitimate, delete a node from the original location, insert a node in the new location, for easy operation directly using the two-way linked list to achieve. "Note points": The sample code is directly based on the value of the linked list to find the node, it does not support duplicate data, you can add properties for the node index to distinguish the same data.

    1. Consistent with the previous question principle

    2. Simple, do not repeat.

    3. Use a single linked list to store the input score, and when the last score node pointer points to it null .

    4. The element with a value of 1-40 loops the list to delete the node until the total number of nodes is only 2 left. To make it easier to count the number of remaining elements, add an attribute to the list to count record the number of elements.

Wild Front end data Structure Basics Exercise (3)--list

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.