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);
     }
    }

  • 相关阅读:
    MVC MVP MVVM
    RC4 对称加密
    ARM 寻址方式
    杂项记录 arm64 的一些特性
    无向图-笔记-代码
    iOS 自定义导航栏
    ios中设置UIButton圆角,添加边框
    iOS 中UICollectionView实现各种视觉效果
    UIScrollView中UITableView
    iOS 13 适配总结
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211490.html
Copyright © 2011-2022 走看看