Linked List(链表)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值