单向链表

来源:互联网 发布:哈工大人工智能 编辑:程序博客网 时间:2024/06/01 07:12

注意:

构造函数名不带泛型

链表 LinkedList

单向链表 SinglyLinkedList

 

get、set、add、remove、length、isEmpty、clear

 

get时可直接返回值,set时返回老值(跟Map的put一样)

add时,记得也前节点关联

add时,若为空链表,则将要添加的节点设为头节点

只add元素时,相当于:

 

add(E element) =  add(Integer.MAX_VALUE, element);  

 

单链表结点类Node声明如下:

  

public class Node<E>  {      public E Data;      public Node<E> Next;        public Node(E data,Node<E> next)      {          this.Data = data;          this.Next = next;      }        public Node(E Data)      {          this(Data,null);      }        public Node()      {          this(null,null);      }  }  

 

Node<E>类有两个成员变量,data表示结点的数据域,保存数据元素本身,数据类型是E;next表示结点的地址域,保存后继结点的地址。

Node类中成员变量next的数据类型是Node类自己,Node类是“自引用的类”。所谓自引用的类(self-referential calss),是指一个类声明包含一个引用当前类的对象的成员变量。

 

建立并链接结点的语句如下:

  

public class MainForm  {      public static void main(String args[])      {          Node<String> CC =new Node<String>("C");          Node<String> BB =new Node<String>("B",CC);          Node<String> AA =new Node<String>("A",BB);                    System.out.println(AA.Data);          System.out.println(AA.Next.Data);          System.out.println(AA.Next.Next.Data);      }  }  

 

单链表的头指针head也是一个结点引用,声明如下:

Node<E> head = null;  

 

当head ==null时,表示空单链表。

 

 

实现:

public class SinglyLinkedList<E>            //单链表类  {      protected Node<E> head;                //头指针,指向单链表第一个结点            public SinglyLinkedList()              //构造空单链表      {          this.head = null;      }            public SinglyLinkedList(Node<E> head)  //构造指定头指针的单链表      {          this.head = head;      }                  //判断单链表是否为空      public boolean isEmpty()                  {          return this.head == null;      }             //返回单链表长度,单链表遍历算法      public int length()                     {          int i = 0;          Node<E> p = this.head;          while(p != null)          {              i ++;              p = p.Next;          }          return i;      }                  //返回序号为index的对象,若单链表为空或序号错误则返回null      public E get(int index)                {          if(this.head != null && index >= 0)          {              int j = 0;              Node<E> p = this.head;              while(p != null && j < index)              {                  j ++;                  p = p.Next;              }              if(p !=null)              {                  return (E)p.Data;              }          }          return null;      }                  //设置序号为index的对象为element,若操作成功,返回原对象,否则返回null     public E set(int index,E element)                               {          if(this.head != null && index >= 0 && element != null)          {              int j = 0;              Node<E> p = this.head;              while(p != null && j < index)              {                  j ++;                  p = p.Next;              }              if(p != null)              {                  E old = (E)p.Data;                  p.Data = element;  //若操作成功,则返回原对象                  return old;                                                 }          }  //操作不成功        return null;                                                  }            //插入element对象,插入后对象序号为index,若操作成功返回true      public boolean add(int index,E element)                         {          if(element ==null)          {              return false; //不能添加空对象(null)          }         //头插入          if(this.head == null || index <= 0)           {              Node<E> q = new Node<E>(element);              q.Next = this.head;              this.head = q;              //this.head = new Node(element,this.head); //头插入,相当于上述3句          }  //单链表不空且index>=1          else          {              int j = 0;              Node<E> p = this.head;              while(p.Next != null && j < index - 1)               //寻找插入位置              {                  j ++;                  p = p.Next;              }              Node<E> q = new Node<E>(element);                   //中间/尾插入              q.Next = p.Next;                                    //q插入在p结点之后              p.Next = q;              //p.Next = new Node(element,p.Next);                //中间/尾插入,相当于上述3句          }          return true;      }            //在单链表最后插入对象        public boolean add(E element)                                   {          return add(Integer.MAX_VALUE,element);      }                  //移去序号为index的对象,若操作成功则返回被移去的对象,否则返回null        public E remove(int index)                                      {          E old = null;          if(this.head != null && index >= 0)          {              if(index == 0)                                      //头删除              {                  old = (E)this.head.Data;                  this.head = this.head.Next;              }              else                                                //中间/尾删除              {                  int j = 0;                  Node<E> p = this.head;                  while(p.Next != null && j < index - 1)           //定位到待删除结点的前驱结点                  {                      j ++;                      p = p.Next;                  }                  if(p.Next != null)                  {                      old = (E)p.Next.Data;                       //若操作成功,返回被移去对象                      p.Next = (p.Next).Next;                     //删除p的后继结点                  }              }          }          return old;      }             //清空单链表      public void clear()                                            {          this.head = null;      }          //返回所有元素值对应的字符串          public String toString()                                        {          String str = "(";          Node<E> p = this.head;          while(p != null)          {              str += p.Data.toString();              p = p.Next;              if(p != null)              {                  str += ",";              }          }          return str + ")";      }  }  

 

 

 

 

0 0
原创粉丝点击