Linked List(链表)

来源:互联网 发布:听觉音乐 淘宝 编辑:程序博客网 时间:2024/05/22 15:11

Linked List

1. Singly Linked List
     A singly linked List is a concrete data structure consisting of a sequence of nodes.
                                                                       tt          
     Each node contains: element and link to the next node.
                       
1.1 Node class for singly Linked List
    /** Node of a singly linked list of strings. */     public class Node {        private String element; // we assume elements are character strings        private Node next; /** Creates a node with the given element and next node. */        public Node(String s, Node n) {           element = s;           next = n;                               }        /** Returns the element of this node. */        public String getElement() { return element; }        /** Returns the next node of this node. */        public Node getNext() { return next; } // Modifier methods:        /** Sets the element of this node. */        public void setElement(String newElem) { element = newElem; }        /** Sets the next node of this node. */        public void setNext(Node newNext) { next = newNext; } } 
1.2 Class for Singly Linked List     
     <span style="font-family: Arial, Helvetica, sans-serif; color: rgb(255, 0, 128);">/** Node of a singly linked list of strings. */</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span><p><span style="color:#8000A0;">     public</span><span style="color:#FF8000;">class</span>Node { </p><p>   <span style="white-space:pre"></span><span style="color:#8000A0;">private String</span> element; <span style="color:#FF0080;">//we assume elements are character strings</span> </p><p>   <span style="white-space:pre"></span><span style="color:#8000A0;">private </span>Node next; <span style="color:#FF0080;">/**Creates a node with the given element and next node. */</span> </p><p>   <span style="white-space:pre"></span><span style="color:#8000A0;">public </span><span style="color:blue;">Node</span>(<span style="color:#8000A0;">String</span> s, <span style="color:#8000A0;">Node </span>n) { </p><p>      element = s; </p><p>      next = n;                              </p><p>        } </p><p>     <span style="color:#FF0080;">/** Returns the element of thisnode. */</span></p><p>   <span style="white-space:pre"></span><span style="color:#8000A0;">public String</span> <span style="color:blue;">getElement</span>() { <span style="color:#FF8000;">return</span>element; }</p><p>   <span style="white-space:pre"></span><span style="color:#FF0080;">/** Returns the next node of thisnode. */</span></p><p>   <span style="white-space:pre"></span><span style="color:#8000A0;">public </span>Node <span style="color:blue;">getNext</span>() { <span style="color:#FF8000;">return</span>next; } <span style="color:#FF0080;">//Modifier methods:</span></p><p>   <span style="white-space:pre"></span><span style="color:#FF0080;">/** Sets the element of thisnode. */</span></p><p>   <span style="white-space:pre"></span><span style="color:#8000A0;">public void</span> <span style="color:blue;">setElement</span>(<span style="color:#8000A0;">String</span> newElem) { element = newElem; } </p><p>   <span style="white-space:pre"></span><span style="color:#FF0080;">/** Sets the next node of thisnode. */</span></p><p>   <span style="white-space:pre"></span><span style="color:#8000A0;">public void</span> <span style="color:blue;">setNext</span>(Node newNext) { next = newNext; }</p><p>     <span style="font-family: Arial, Helvetica, sans-serif;">} </span></p>

1.3 Basic Operations:

     1.Insertion At Head

     a.Allocate a new node

     b.Insert the new element

     c.Have the new node point to old head

     d.Update head to point to the new node

                    

    2.Removal At Head

    a.Update head to point to the next node

    b.Allow garbage collector to reclaim the former first node

               

    3.Insertion At Tail

    a.Allocate a new node

    b.Insert the new element

    c.Have the new node point to null

    d.Have the old last node point to new node

    e.Update tail to point to new node

             

    4.Removal At Tail

    a.Update the last second node to point to null

    b.Allow garbage collector to reclaim the last node

            

      


2.Doubly Linked List

   A doubly linked list is a concrete data structure consisting of a sequence of nodes, and each node contains: element, link to previous node and link to the next node.

                                                                                                                       

                                         

2.1 The Node Class For Doubly Linked List

     

2.2 The Class For Doubly Linked List

     /** Doubly linked list .*/     public class DoublyLinkedList {      protected Node header; // head node of the list     protected Node trailer; // trailer node of the list     protected long size; // number of nodes in the list      /** Default constructor that creates an empty list */      public DoublyLinkedList() {        header = null; trailer = null; size = 0;                                      }           // ... update and search methods would go here ...      } 

Basic Operations:

1.Insertion At Head

  

2.Insertion In The Middle

  

3.Removal At Tail

  

4.Removal In Middle

  

0 0
原创粉丝点击