[MOOC notes] Chapter III list (data structure)

Source: Internet
Author: User

1. Introduction to the list

A list is a typical structure with a dynamic storage strategy, and its basic unit is a node. Each node is logically composed of a linear sequence, either by reference or by pointers connected to each other. Adjacent nodes are referred to each other as precursors (predecessor) and successors (successor), and if precursors or successors are the only ones, the nodes without precursors (successors) are called the first (end) nodes (in the actual implementation of some lists, the first (last) node is the predecessor (successor), They are called head (tail) nodes, these two nodes are not practical, and will not be exposed, just for the convenience of some operations generated. )。

Unlike vectors, the list is physically not a contiguous memory space, so it is not possible to access the elements directly through the rank. So the list is more commonly used for location -based access, where each element is located by referencing each other in the node.


2. List of interfaces

As the basic unit of the constituent list, the nodes need at least these interfaces:

Operation Function
Pred () The position of the current node precursor node
SUCC () The location of the successor node of the current node
Data () The data object that the current node holds
Insertaspred (e) Insert the precursor node and deposit the referenced object E
INSERTASSUCC (e) Insert the subsequent node, save the referenced object E


A list of nodes, the following interfaces are required:

action size () first (), Last () insertasfirst (e), Insertaslast (e) insertbefore (p, e), InsertAfter (p, e) remove (p) disordered () sort () find (e) search (e) deduplicate () uniquify ()
features
report list current size (total number of nodes) list
list
insert e as the first and stub point of the list list
E as a precursor to node p, subsequent insert list
delete node P list
list
adjust the position of each node so that it is arranged in a non-descending order list
Find the target element e list
find nodes that are not greater than E and have the largest rank sequence table
Delete duplicate node list
Delete duplicate node sequence table
Traverse () Iterates through the list and processes all nodes uniformly, and the processing method is specified by the function object List

3. Rank-and-seek access, insert, and delete operations for a list
/** * List of the rank access, time complexity O (n) * @param r the rank of the element to be accessed * @return the node corresponding to the rank */public listnode get (int r) {//judgment rank is valid if (R >= this.size | | R < 0) {throw new ArrayIndexOutOfBoundsException ("Subscript Out of Bounds");} Get the first node of the list, listnode node = This.first ();//starting from the first node, pointing to the successor node, and then recursion R times to get the element with rank R (r--> 0) {node = NODE.SUCC ();} return node;} /** * The predecessor insert operation of the list (the subsequent insert operation is just the opposite), Time complexity O (1) * @param node * @param e * @return */public listnode insertbefore (ListNode node, T E ) {//increase the size of the list this.size++;//constructs a new node, the precursor of the new node is the predecessor of the node, and the successor of the new node is the node. listnode<t> NewNode = new ListNode (e, node.getpred (), node);//The Successor of the node precursor is set to the new node node.getpred (). SETSUCC (node);// The predecessor of the node is set to the new node node.setpred (node);//Return to the new node return newNode;} /** * Delete a legitimate location node, time complexity O (1) * @param node to delete nodes * @return elements of that node */public T Remove (listnode<t> node) {//reduce the size of the list this . size--;//the element in the backup node T e = Node.getdate ();//sets the successor of the node predecessor to the successor of the Node node.getpred (). SETSUCC (NODE.GETSUCC ());// The predecessor node of the node is set to the precursor node.getsucc () of the node. setpred (Node.getpred ());//delete the node's reference node = null;//Returns the element of that node return e;

4. Ordered list

A sequence table is like an ordered vector, and it has higher performance when it is re-weighed. The de-redo operations of lists and ordered lists are similar to vectors and ordered vectors, and the complexity of time is also equal:

/** * List of deduplication time complexity O (n^2) * @return Total number of deleted elements */public int deduplicate () {//record de-weight before size int oldsize = this.size;//traversal from first node LISTN Ode node = This.first ();//record the current position int r = 1;//loop in turn until the tail node while (node = NODE.GETSUCC ())! = This.trailer) {//Find out in node's R precursor if there is a It contents the same node ListNode temp = This.find (Node.getdate (), R, node);//If the node is deleted with the same node, increase the precursor range if (temp! = null) {This.remove (temp );} else {r++;}} Returns the scale change amount of return oldsize-this.size;} /** * deduplication time complexity O (n) * @return deleted total number of elements */public int uniquify () {//record the first element of each interval and the position of the first element of the next interval int oldsize = this.size;/ /starting from the first node ListNode node = This.first ();//Record the next node ListNode next;//sequentially through the tail node while (next = NODE.GETSUCC ())! = This.trailer) {//delete each of the same nodes if (Node.getdate (). Equals (Next.getdate ())) {This.remove (next);} else {node = next;}} Returns the scale change amount of return oldsize-this.size;}


But because lists are seek location access rather than rank-and-seek access, an ordered list does not produce more efficiency in the lookup. Therefore, the lookup time complexity of the sequence table is the same as the normal list O (n).


Note: This lesson for Tsinghua University Deng Junhui teacher in the Academy online MOOC Course "Data Structure", the next start time is the spring of 2015. If the interested students can go to see, personally think that the course of breadth or depth are higher than other data structure classes.




[MOOC notes] Chapter III list (data structure)

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.