• java单链表


    节点类

    package link; 
    /** 
     * @author wangpei 
     * @version 
     *创建时间:2017年3月3日 下午5:54:04 
     * javaz单链表的操作
     */
    public class Node {
        protected Node next;
        protected int data;//数据区
        public Node(){
    
        }
        public Node(int data){
            this.data=data;
        }
        public void print(Node current){
            System.out.println("data="+current.data);
        }
    
    
    
    }
    

    主要操作类:

    package link; 
    
    import java.util.ArrayList;
    
    /** 
     * @author wangpei 
     * @version 
     *创建时间:2017年3月3日 下午5:58:10 
     * 单链表的操作
     */
    public class Only {
    
        public void create_linkList1(Node head,int []data){//头插法建立单链表
             for(int i=0;i<data.length;i++){
             Node s=new Node(data[i]);//创建一个新节点
             s.next=head.next;
             head.next=s;
             }
    
        }
        public void create_linkList2(Node node,Node r,int []data){//尾插法建立单链表
            for(int i=0;i<data.length;i++){
    
             Node s=new Node(data[i]);//创建一个节点
             r.next=s;
             r=s;
             }
    
    
        }
    
        public void printAllNode(Node h){//打印所有节点信息。
            Node current =h.next;
            while(current!=null){
                current.print(current);
                 current=current.next;
            }
    
        }
        public ArrayList<Integer>  printAllNodeBytail(Node listNode){//打印所有节点信息。
            ArrayList<Integer> list=new ArrayList<Integer>();
            Node current =listNode.next;
    
            while(current!=null){
                list.add(current.data);
                 current=current.next;
            }
            for(int i=0;i<list.size()/2;i++){
                int t=list.get(i);
                list.set(i, list.get(list.size()-i-1));
                list.set( list.size()-i-1,t);
    
            }
    
            return list;
        }
    
        public boolean delete(Node node,int i){//删除单链表head上的i节点
            Node p=get_Node(node,i-1);//获取到第i个节点
            p.next=p.next.next;
            return false;
    
    
        }
        //根据头结点查找第i各节点
        private Node get_Node(Node head,int i) {
            Node current =head;
            int j=0;
             while(current!=null){
                 if(j==i)
                     break;
                  current=current.next;
                  j++;
    
    
             }
             return current;
        }
    
        //删除链表中的重复节点
        public void deleteAgatin(Node head){
    
    
        }
        //单链表的倒置,思想:从原来的链表中依次读出结果按头插法存在新链表中。
        public void reverse(Node head){
            Node p=head.next;
            Node q;
            head.next=null;
            while(p!=null){
                System.out.println("进入该循环");
                q=p;
                p=p.next;
                q.next=head.next;
                head.next=q;
    
    
    
            }
    
        //  return rehead;
    
        }
        public static void main(String[] args) {
            Node head=new Node();
            Node r=head;
    
            Only o=new Only();
            int []data={1,2,3,4,5};
            o.create_linkList2(head,r,data);//插入一个节点
            o.printAllNode(head);
    //      o.create_linkList2(2);//插入一个节点
    //      o.create_linkList2(3);//插入一个节点
        //  o.printAllNode(head);
            //o.delete(1);
    
    
    //      o.reverse(head);
    //      o.printAllNode( head);
            ArrayList<Integer> list=o.printAllNodeBytail(head);
            for(int i:list){
                System.out.println("list="+i);
            }
    
    
        }
    
    
    }
    
  • 相关阅读:
    Software Update Services 2.0 概述
    Windows 2000组策略
    Windows 2000 默认安全策略设置
    C语言I博客作业06
    C语言I博客作业08
    C语言I博客作业09
    C语言I博客作业03
    C语言12019级秋季作业第一周作业
    C语言I博客作业02
    C语言I博客作业05
  • 原文地址:https://www.cnblogs.com/wangxiaopei/p/8551238.html
Copyright © 2020-2023  润新知