zoukankan      html  css  js  c++  java
  • 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);
            }
    
    
        }
    
    
    }
    
  • 相关阅读:
    Mysql大量插入随机数据方法--存储过程
    Linux永久修改系统时间和时区方法
    python反转字符串(简单方法)及简单的文件操作示例
    sql怎么批量替换字段里的字符串的
    varchar和Nvarchar区别
    VS改大小写的快捷键
    SQL中PIVOT 行列转换
    [转]VS中展开和折叠代码
    Bootstrap 标签页(Tab)插件
    C# DataTable 和List之间相互转换的方法
  • 原文地址:https://www.cnblogs.com/wangxiaopei/p/8551238.html
Copyright © 2011-2022 走看看