Linked List __ List

Source: Internet
Author: User
Linked list

Linked list, one-way linked list, two-way linked list
Let me look at the two-way linked list (most commonly used as shown here:
(Figure I) two-way linked list Query

Query program List_search (L,K) to find the elements of the data k inside the L, through a simple linear lookup, return a pointer to this element. If K does not appear in the list, the program returns Empty (NIL).

List_search (L, K)

x = L.head while
x!= NIL and X.key!= k
    x = x.next return
x
Insert

Given an element property already set, and then insert it into the head of the list, as shown in figure (b) compared to (a)
List_insert (L, X)

X.next = L.head
if l.head!= NIL l.head.prev =
    x
l.head = x
X.prev = NIL
Remove

Deletes an element x from the list L. However, you must provide a pointer to x that can be returned by List_search (l, x).
List_delete (L, X)

If X.prev!= NIL
    x.prev.next = x.next
Else L.head = X.next
if x.next!= NIL x.next.prev
    = X.prev
Two-way cyclic link list

The following figure is a two-way circular chain with Sentinels, and the Sentinel's next is the head of the chain, and the Sentinel's prev is the tail of the list.
Query

List_search (L, K)

x = L.nil.next while
x!= L.nil and X.key!= k
    x = x.next return
x

The above method can also be optimized to remove each of the X!= NIL judgment, but I do not know how to remove, this is an introduction to the algorithm exercises. Insert

List_insert (L, K)

X.next = L.nil.next
l.nil.next.prev = x
l.nil.next = x
X.prev = L.nil
Remove

A two-way circular chain with Sentinels can reduce the constant factor, but simply the code, if a short list of many cases the Sentinels will waste memory.
List_delete (L, X)

X.prev.next = x.next
X.next.prev = X.prev

because the single linked list deletes the operation, the localization spends the time is O (n), therefore generally uses the bidirectional link list. Application stack is implemented with a single linked list, and push and pop only spend O (1) Time

PUSH (L, X)

X.next = L.head
l.head = x

POP (L)

if L.head = = NIL
    error "underflow"
x = l.head
l.head = l.head.next return
x
using single linked list to realize Queue,enqueue and dequeue all only spend O (1) Time

Queue diagram:

ENQUEUE (L, X)

L.tail.next = x
l.tail = x

Dequeue (L)

if L.head = = NIL
    error "underflow"
x = l.head
l.head = l.head.next return
x
using a one-way cyclic list to implement the dictionary operation Insert, DELETE, and SEARCH, and give the running time of the written process

Dict_insert (L, X)

X.next = L.nil.next
l.nil.next = x

T (N) = O (1)

Dict_delete (L, X)

p = l.nil while
p.next!= l.nil and P.next!= x
    p = p.next
if p.next!= l.nil  
    = P.next

T (n) = O (n)

Dict_search (L, X)

x = L.nil.next while
x!= L.nil and X.key!= k
    x = x.next return
x

T (n) = O (n)

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.