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
element
The value of the current node
next
Next 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
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.
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.
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)
- Implement
Advance(n)
the method to move the node forward by N nodes.
- Implements a
back(n)
method that moves the node backward by n nodes.
- Implementation
show()
method that displays only the data on the current node.
- Slightly
- Slightly
- 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
- 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.
Consistent with the previous question principle
Simple, do not repeat.
Use a single linked list to store the input score, and when the last score node pointer points to it null
.
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