线性表的链式存储结构之单链表类的实现_Java

来源:互联网 发布:iea数据 编辑:程序博客网 时间:2024/06/06 03:40
[java] view plaincopy
  1. package dataStructure.linearList;  
  2. import dataStructure.linearList.Node;                           //导入单链表结点类  
  3.   
  4. public class SinglyLinkedList<E> implements LList<E>            //单链表类,实现线性表接口  
  5. {  
  6.     protected Node<E> head;                                       //头指针,指向单链表第一个结点  
  7.       
  8.     public SinglyLinkedList()                                   //构造空单链表  
  9.     {  
  10.         this.head = null;  
  11.     }  
  12.       
  13.     public SinglyLinkedList(Node<E> head)                     //构造指定头指针的单链表  
  14.     {  
  15.         this.head = head;  
  16.     }  
  17.       
  18.     public boolean isEmpty()                                    //判断单链表是否为空  
  19.     {  
  20.         return this.head == null;  
  21.     }  
  22.       
  23.     public int length()                                         //返回单链表长度,单链表遍历算法  
  24.     {  
  25.         int i = 0;  
  26.         Node<E> p = this.head;  
  27.         while(p != null)  
  28.         {  
  29.             i ++;  
  30.             p = p.Next;  
  31.         }  
  32.         return i;  
  33.     }  
  34.       
  35.     public E get(int index)                                     //返回序号为index的对象,若单链表为空或序号错误则返回null  
  36.     {  
  37.         if(this.head != null && index >= 0)  
  38.         {  
  39.             int j = 0;  
  40.             Node<E> p = this.head;  
  41.             while(p != null && j < index)  
  42.             {  
  43.                 j ++;  
  44.                 p = p.Next;  
  45.             }  
  46.             if(p !=null)  
  47.             {  
  48.                 return (E)p.Data;  
  49.             }  
  50.         }  
  51.         return null;  
  52.     }  
  53.       
  54.     public E set(int index,E element)                           //设置序号为index的对象为element,若操作成功,返回原对象,否则返回null  
  55.     {  
  56.         if(this.head != null && index >= 0 && element != null)  
  57.         {  
  58.             int j = 0;  
  59.             Node<E> p = this.head;  
  60.             while(p != null && j < index)  
  61.             {  
  62.                 j ++;  
  63.                 p = p.Next;  
  64.             }  
  65.             if(p != null)  
  66.             {  
  67.                 E old = (E)p.Data;  
  68.                 p.Data = element;  
  69.                 return old;                                     //若操作成功,则返回原对象  
  70.             }  
  71.         }  
  72.         return null;                                            //操作不成功  
  73.     }  
  74.       
  75.     public boolean add(int index,E element)                     //插入element对象,插入后对象序号为index,若操作成功返回true  
  76.     {  
  77.         if(element ==null)  
  78.         {  
  79.             return false;                                       //不能添加空对象(null)  
  80.         }  
  81.           
  82.         if(this.head == null || index <= 0)                      //头插入  
  83.         {  
  84.             Node<E> q = new Node<E>(element);  
  85.             q.Next = this.head;  
  86.             this.head = q;  
  87.             //this.head = new Node(element,this.head);          //头插入,相当于上述3句  
  88.         }  
  89.         else                                                    //单链表不空且index>=1  
  90.         {  
  91.             int j = 0;  
  92.             Node<E> p = this.head;  
  93.             while(p.Next != null && j < index - 1)               //寻找插入位置  
  94.             {  
  95.                 j ++;  
  96.                 p = p.Next;  
  97.             }  
  98.             Node<E> q = new Node<E>(element);                   //中间/尾插入  
  99.             q.Next = p.Next;                                    //q插入在p结点之后  
  100.             p.Next = q;  
  101.             //p.Next = new Node(element,p.Next);                //中间/尾插入,相当于上述3句  
  102.         }  
  103.         return true;  
  104.     }  
  105.       
  106.     public boolean add(E element)                               //在单链表最后插入对象  
  107.     {  
  108.         return add(Integer.MAX_VALUE,element);  
  109.     }  
  110.       
  111.     public E remove(int index)                                  //移去序号为index的对象,若操作成功则返回被移去的对象,否则返回null  
  112.     {  
  113.         E old = null;  
  114.         if(this.head != null && index >= 0)  
  115.         {  
  116.             if(index == 0)                                      //头删除  
  117.             {  
  118.                 old = (E)this.head.Data;  
  119.                 this.head = this.head.Next;  
  120.             }  
  121.             else                                                //中间/尾删除  
  122.             {  
  123.                 int j = 0;  
  124.                 Node<E> p = this.head;  
  125.                 while(p.Next != null && j < index - 1)           //定位到待删除结点的前驱结点  
  126.                 {  
  127.                     j ++;  
  128.                     p = p.Next;  
  129.                 }  
  130.                 if(p.Next != null)  
  131.                 {  
  132.                     old = (E)p.Next.Data;                       //若操作成功,返回被移去对象  
  133.                     p.Next = (p.Next).Next;                     //删除p的后继结点  
  134.                 }  
  135.             }  
  136.         }  
  137.         return old;  
  138.     }  
  139.       
  140.     public void clear()                                         //清空单链表  
  141.     {  
  142.         this.head = null;  
  143.     }  
  144.       
  145.     public String toString()                                    //返回所有元素值对应的字符串  
  146.     {  
  147.         String str = "(";  
  148.         Node<E> p = this.head;  
  149.         while(p != null)  
  150.         {  
  151.             str += p.Data.toString();  
  152.             p = p.Next;  
  153.             if(p != null)  
  154.             {  
  155.                 str += ",";  
  156.             }  
  157.         }  
  158.         return str + ")";  
  159.     }  
  160. }  
原创粉丝点击