zoukankan      html  css  js  c++  java
  • Java对链表的操作


    class LinkList
    {
     private class Node//创建节点类
     {
      public Object item;
      public Node   next;
     }

     private Node head;
     private Node slider;
     private int  count;

     public LinkList()//构造方法
     {
      clear();
     }

     public void clear()//清空链表
     {
      head   = null;
      slider = null;
      count  = 0;
     }

     public boolean isEmpty()//判断链表是否为空
     {
      return head==null;
     }

     public void gotoFirst()//指向链表的头结点
     {
      slider = head;
     }

     public Object nextItem()//插入数据
     {
      if(slider==null)
      {
       return null;
      }

      Object o = slider.item;

      slider = slider.next;

      return o;
     }

     public Object getFirst()
     {
      return head.item;
     }

     public Object getAt(int index)
     {
      if(index<0 || index>=count)
      {
       return null;
      }

      Node g  = head;

      for(int i=0; i<index; i++)
      {
       g  = g.next;
      }

      return g.item;

     }

     public void addAt(Object item,int index)
     {
      Node g  = head;
      Node ag = head;

      if(index<0)
      {
       addFirst(item);
      }
      else
       if(index>=count)
      {
       addLast(item);
      }
      else
      {
       for(int i=0; i<index; i++)
       {
        if(g==ag)
        {
         g  = g.next;
        }
        else
        {
         ag = g;
         g  = g.next;
        }
       }

       if(index==0)
       {
        head      = new Node();
        head.item = item;
        head.next = g;
       }
       else
       {
        ag         = new Node();
        ag.item    = item;
        ag.next    = g;
       }

       count++;
      }
     }

     public Object removeAt(int index)
     {
      if(index<0 || index>=count)
      {
       return null;
      }

      Node g  = head;
      Node ag = head;

      for(int i=0; i<index; i++)
      {
       if(g==ag)
       {
        g  = g.next;
       }
       else
       {
        ag = g;
        g  = g.next;
       }
      }

      if(index==0)
      {
       head = head.next;
      }
      else
      {
       ag.next = g.next;
      }

      count--;

      return g.item;
     }

     public void addFirst(Object item)
     {
      Node g    = head;

      head      = new Node();
      head.item = item;
      head.next = g;

      count++;
     }

     public void addLast(Object item)
     {
      if(head==null)
      {
       head      = new Node();
       head.next = null;
       head.item = item;
      }
      else
      {
       Node s = head;

       for(int i=0; i<count; i++)
       {
        if(s.next == null)
        {
         break;
        }

        s = s.next;
       }


       s.next = new Node();
       s      = s.next;
       s.next = null;
       s.item = item;
      }

      count++;
     }

     public int length()
     {
      return count;
     }

     public Object removeFirst()
     {
      return removeAt(0);
     }

     public Object removeLast()
     {
      return removeAt(count-1);
     }
    }

  • 相关阅读:
    单例模式
    SRM147 DIV2 950
    SRM147 DIV2 600
    SRM147 DIV2 250
    SRM147 DIV1 1000
    Python 实现字符串反转的9种方法
    ubtuntu redis 集群部署/搭建(官方原始方案)
    Python2 ValueError: chr() arg not in range(256) 解决办法?
    python 字典操作中has_key() 和 in 那个使用更加pythonic?
    Python库 使用filetype精确判断文件类型
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211490.html
Copyright © 2011-2022 走看看