zoukankan      html  css  js  c++  java
  • Circular List

    一.环形列表:列表中任何一个节点可以到达其他节点。

    1.插入:插入有俩中形式,第一种:插入到第一个节点之前,第二种:插入到列表中任何一个节点中

    2.删除:删除X 是第一个节点,最后一个节点链接到X第一个节点后面;第二种:列表中间节点,X前节点链接X后一个节点。

    有关于内存分配的运用。

    http://blog.csdn.net/yangyuankp/article/details/7651251

      1 package CircularList;
      2 
      3 
      4 //环形列表,
      5 //插入有俩中形式,第一种:插入到第一个节点之前,第二种:插入到列表中任何一个节点中
      6 public class circulist {
      7 
      8     
      9     public Node first;
     10     public Node last;
     11     
     12     public boolean isEmpty()
     13     {
     14         return first==null;
     15     }
     16     
     17     public void print()
     18     {
     19         Node current=first;
     20         while(current!=last)
     21         {
     22             System.out.println("["+current.data+"]");
     23             current=current.next;
     24         }
     25         System.out.println("["+current.data+"]");
     26         System.out.println(current.next);//Circular List 
     27         System.out.println();
     28     }
     29     
     30     public void insert(Node trp)
     31     
     32     {
     33         //插入两种方式,插入到第一个节点,或者插入到最后一个节点
     34         Node newNode;Node tmp;
     35         if(this.isEmpty())
     36         {
     37             first=trp;
     38             last=trp;
     39         }
     40         else if(trp.next==null)
     41         {
     42             //插入第一个节点之前
     43             last.next=trp;
     44             last=trp;
     45             last.next=first;
     46         }
     47         else
     48         {
     49             //插入到中间
     50             newNode=first;
     51             tmp=first;
     52             while(newNode.next!=trp.next)
     53             {
     54                 if(tmp.next==first)
     55                     break;
     56                 
     57                 newNode=newNode.next;
     58                 tmp=newNode;
     59             }
     60             tmp.next=trp;//发现在测试的时候出现trp点有所有节点信息。
     61             
     62         }
     63     }
     64     public static void main(String[] args)
     65     {
     66         circulist cir=new circulist();
     67         //建立5个数据环形列表
     68         for (int i=1;i<=5;i++)
     69         {    Node tmp=new Node(i);
     70             cir.insert(tmp);
     71             
     72         }
     73         cir.print();
     74         
     75         int insertposi=2;int i=1;
     76         Node insertNode=new Node(6);
     77         
     78         circulist cir2= new circulist();//直接与堆中地址相连
     79         cir2=cir;
     80         
     81         Node current=cir2.first;
     82         System.out.println(current);
     83         
     84         while(i<=insertposi)
     85         {
     86             System.out.println(current);
     87             current=current.next;
     88             i++;
     89         }
     90         //current.next=null;这句话不能用,在这里修改current同时会修改first列表
     91         
     92         cir.print();
     93         insertNode.next=current;
     94         System.out.println("被插入点"+insertNode.data);
     95         
     96             
     97         
     98         cir.insert(insertNode);
     99         System.out.println("在中间插入");
    100         cir.print();
    101     }
    102 }
    103 
    104 class Node {
    105     int data;
    106     Node next;
    107     
    108     //构造函数初始化,模拟单个小节点
    109     public Node(int data)
    110     {
    111         this.data=data;
    112         this.next=null;
    113     }
    114 }
    [1]
    [2]
    [3]
    [4]
    [5]
    CircularList.Node@1db9742
    
    CircularList.Node@1db9742
    CircularList.Node@1db9742
    CircularList.Node@106d69c
    [1]
    [2]
    [3]
    [4]
    [5]
    CircularList.Node@1db9742
    
    被插入点6
    在中间插入
    [1]
    [2]
    [6]
    [3]
    [4]
    [5]
    CircularList.Node@1db9742
    View Code

    对于链表删除

    package CurListLinnk;
    
    public class StudentLinkList {
    
        public Node first;
        
             public Node last;
            
             public boolean isEmpty()
            {
                  return first==null;
              }
              
             public void print()
             {
                 Node current=first;
                 while(current!=last)
                {
                    System.out.println("["+current.data+"]");
                    current=current.next;
                  }
                System.out.println("["+current.data+"]");
             System.out.println(current.next);//Circular List 
                  System.out.println();
            }
             
            public void insert(Node trp)
             
            {
                //插入两种方式,插入到第一个节点,或者插入到最后一个节点
                Node newNode;Node tmp;
                if(this.isEmpty())
                {
                first=trp;
                     last=trp;
                 }
                 else if(trp.next==null)
                 {
                     //插入第一个节点之前
                    last.next=trp;
                    last=trp;
                    last.next=first;
                 }
                else
                  {
                    //插入到中间
                    newNode=first;
                     tmp=first;
                    while(newNode.next!=trp.next)
                    {
                        if(tmp.next==first)
                            break;
                       
                    newNode=newNode.next;
                     tmp=newNode;
                     }
                    tmp.next=trp;//发现在测试的时候出现trp点有所有节点信息。
                     
                 }
            }
            public void delete (int position)
            {
                Node tmp=first,frontNode=first;
                int i=0;
                while(i<position)
                {    
                    frontNode=tmp;
                    tmp=tmp.next;
                    i++;
                     System.out.println(frontNode.data);
                }
                frontNode.next=tmp.next;
                
            }
            public static void main(String[] args)
            {
                    StudentLinkList cir=new StudentLinkList();
                         //建立5个数据环形列表
                         for (int i=1;i<=5;i++)
                         {    Node tmp=new Node(i);
                              cir.insert(tmp);
                             
                          }
                         System.out.println("删除之前");
                          cir.print();
                          cir.delete(2);
                          cir.print();
            }
    }
    
    
    class Node
    {
        int data;
      Node next;
      public Node(int ata)
      {
          
          this.data=ata;
      }
    }
    删除之前
    [1]
    [2]
    [3]
    [4]
    [5]
    CurListLinnk.Node@1db9742
    
    1
    2
    [1]
    [2]
    [4]
    [5]
    CurListLinnk.Node@1db9742
    View Code 
    双向链表:一个节点有三个字断:LLink--Data---RLink,LLink指向前一个节点,RLink指向后一个节点,与单方向节点差不多
  • 相关阅读:
    (十三)过滤器Filter(转)
    (十二)会话跟踪技术之servlet通信(forward和include)
    (十一)会话跟踪技术之作用域(request、session、servletContext)
    openjdk源码目录结构
    java socket相关的timeout
    eclipse创建maven web app
    hadoop mapred和mapreduce包
    hadoop shuffle
    bash shell和进程
    bash shell中的特殊用法
  • 原文地址:https://www.cnblogs.com/woainifanfan/p/6012234.html
Copyright © 2011-2022 走看看