• 链表的各种操作


    1.在一个包中创建Node类 ,linkList和ManuList类

      Node类中主要是定义链表的一些对象

    * 定义一个Node类
     */
    package List;
    
    public class Node {
       public int value;
       public Node next;//创建一个对象指向下一个指针
       
       public Node(int value)//创建一个构造函数
       {
           this.value=value;
           this.next=null;
       }
       public Node(){};//创建一个无参的构造函数,重载
    }

    MaunList是链表要实现的各种操作

    /*
     * 链表的定义
     */
    package List;
    
    import java.util.Stack;
    
    public class MaunList {
       
        /*遍历列表*/
        public void trivalList(Node head){
            Node p=head;
            while(p!=null){
                System.out.print(p.value+" ");
                p=p.next;
            }
        }
    
        /*从列表中找到一个指定的数第一次出现的位置,无就返回-1*/
        public int indexOf(Node head,int num){
            int pos=0;
            while(head!=null){
                 if(num==head.value){ 
                     return pos;
                     }
                 pos++;
                 head=head.next;
            }
            return -1;
        }
        
        /*
         * 寻找链表中目标出现的最后的位置
         */
        public int LastOf(Node head,int num){
            int pos=-1;
            int count=0;
            while(head!=null){
                 if(num==head.value){ 
                 pos=count;
                 }
                 count++;
                 head=head.next;
                } 
             return pos;
        }
        /*
         * 链表的最后位置插入一个数
         * 1.找到最后节点
         * 2.新建节点
         * 3.给最后的节点赋值
         */
        
        public void addLast(Node head,int num){
             
            while(head.next!=null){
                  
                 head=head.next;
            }
             
                 head.next=new Node(num);//创建一个新的节点,并复制为num;
            
        }
        /*
         * 指定位置的插入
         * 1.新建节点
         * 2.根据位置断开节点
         * 3.链接节点
         */
        public void add(Node head,int pos,int num){
            for(int i=0;i<pos-1;i++){
                 head=head.next;//1前一个节点的位置
                 }
            Node node=new Node(num);//2,创建新的节点,节点的内容的num;
            node.next=head.next;
            head.next=node;
                 
            }
     
        /*
         * 顺序节点的插入
         * 1.根据数的大小插入
         */
        public Node addInOrder(Node head,int num){
            if(head==null||head.value>=num){
                Node node=new Node(num);
                node.next=head;
                return node;
                
            }
            Node p=head;
            while(head.next!=null){
                if(head.value<num&&head.next.value>=num){
                    Node node=new Node(num);
                    node.next=head.next;
                    head.next=node;
                    return p;
                }else{
                    head=head.next;
                }
            }
            this.addLast(head, num);
            return p;
        }
        /*
         * 删除一个
         */
        public Node deleNode(Node head,int num){
            if(head==null) return head;
            if(head.value==num){
                
                return head.next;
            }
            Node p=head;
            while(head.next!=null){
                 if(head.next.value==num){
                    head.next=head.next.next;
                    return p;//相同的只删除一个
                 }
                 else{
                     head=head.next;
                 }
            }
            return p;
        }
    
        /*8
         *链表排序
         *1.先找一个最大的数,从大到小排序
         */
        public Node sortList(Node head){
            
            Node newHead=new Node();//新建一个链表
            Node temp;
            int max;
            while(head!=null){
                max=head.value;
                temp=head;
                
                while(temp!=null){
                  if(temp.value>max){
                    max=temp.value;
                  }
                    temp=temp.next;
                
                }
                this.addLast(newHead, max);
                head=this.deleNode(head,max);
            }
            return newHead.next;
            
        }
        
        /*交换两个数*/
        public void changNum(Node head,int a,int b){
             
            while(head!=null){
                if(head.value==a){
                    head.value=b;
                    head=head.next;
                }
                if(head.value==b){
                    head.value=a;
                    head=head.next;
                }
                else{
                    head=head.next;
                }
            }
             
        }
        /*交换两个节点
         * 指定位置交换
         * */
        public Node ChangeNum(Node head,int pos1,int pos2){
             Node temp=head;
             int pos=0;
             int a=0;
             Node numHead;
            while(head!=null){
                
                if(pos==pos1){
                    a=head.value;
                     numHead = head;
                     head=head.next;
                     pos++;
                     while(head!=null){
                       if(pos==pos2){
                       numHead.value=head.value;
                        head.value=a;
                       return temp; 
                     } else{
                          pos++;
                          head=head.next;
                   }
                }
                     
                }else{
                    pos++;
                    head=head.next;
                }
            }
            return temp;               
            
        }
        
        
        /*节点的链接*/
        public Node MaunList(int[] scores) {
            if(scores.length==0) return null;
             
             Node head=new Node();
             head.value=scores[0];
             Node temp=head;
            
             for(int i=1;i<scores.length;i++){
                 Node node=new Node();
                 node.value=scores[i]; 
                 temp.next=node;
                 temp=temp.next;
                 
             } 
             return head;
             }
        }

    linkList是对函数的调用

    package List;
    
    public class linkList {
    
        public static void main(String[] args) {
             int[] value=new int[]{60,70,70,80,90,70,100};
             
            MaunList ml=new MaunList();
                    
             Node head=ml.MaunList(value);//把数组转换成链表
            System.out.println(head.next.value);
            
    /*         ml.trivalList(head);
             System.out.println(ml.indexOf(head,70));
             System.out.println(ml.LastOf(head, 70));
             
             ml.addLast(head, 90); 
             ml.trivalList(head);
              
             ml.add(head, 1, 100);
             ml.trivalList(head);
              
             
            /*插入最小的数相当于一个新的列表*/ 
    /*         Node h=ml.addInOrder(head,1);
             ml.trivalList(h);
    
             ml.addInOrder(head,666);
             ml.trivalList(head);
    */         
    /*         head=ml.deleNode(head,70);
             ml.trivalList(head);
    */    
    
    /*         ml.trivalList(head);
    
             head=ml.sortList(head);
             ml.trivalList(head);
    */
    /*         String c="[,(,]";
             ml.isMatch(c);
             System.out.print(ml.isMatch(c));
    */         
    /*         head=ml.ChangeNum(head,0, 4);
             ml.trivalList(head);
    */
             ml.changNum(head, 60, 100);
             ml.trivalList(head);
            
        }
    
    }
  • 相关阅读:
    [LeetCode 220.] 存在重复元素 III
    C++ 构造函数 & 析构函数
    [LeetCode 891.] 子序列宽度之和【hard】
    [LeetCode 447.] Number of Boomerangs
    HJ93 数组分组
    HJ77 火车进站
    [LeetCode 338.] 比特位计数
    线段树
    大数量问题的一般解决方法
    字典树
  • 原文地址:https://www.cnblogs.com/sunli0205/p/5225746.html
Copyright © 2020-2023  润新知