Java数据结构之——链表

import java.lang.Object;
import java.util.Dictionary;
import java.util.Hashtable;

class Node{
    Node next = null;
    int data;
    public Node(int data){
        this.data = data;
    }
}

public class MyLinkedList{
    Node head = null;
    public void add(int d){
       if(head == null){
           head = new Node(d);
       } 
       else{
           Node curr = head;
           while(curr.next != null){
               curr = curr.next;
           }
           curr.next = new Node(d);
       }
      
    }
    
    public Boolean deleteNode(int index){
        if(index < 1 || index > length())
            return false;
        if(index == 1){
            head = head.next;
            return true;
        }
        int i = 1;
        Node preNode = head;
        Node currNode = head.next;
        
        while(currNode != null){
            if(i == index){
                preNode.next = currNode.next;
                currNode.next = null;
                return true;
            }
            preNode = currNode;
            currNode = preNode.next;
            i++;
        }
        return false;
    }
    
    public int length(){
        int len = 0;
        Node curr = head;
        while(curr!=null){
            curr = curr.next;
            len++;
        }
        return len;
    }
    
    public void printList(){
        Node currNode = head;
        while(currNode != null){
            System.out.println(currNode.data);
            currNode = currNode.next;
        }
    }
    
    public Node orderList(){
        Node currNode = head;
        while(currNode.next != null){
            Node nextNode = currNode.next;
            while(nextNode != null){
                if(currNode.data > nextNode.data){
                    int tmp = currNode.data;
                    currNode.data = nextNode.data;
                    nextNode.data = tmp;
                }
                nextNode = nextNode.next;
            }
            currNode = currNode.next;
        }
        return head;
    }
    
    /*
    * 递归方法逆序输出单链表
    *
    */
    public void printListReversely(Node listHead){
        Node currNode = listHead;
        if(currNode != null){
            printListReversely(currNode.next);
        
            System.out.println(currNode.data);
        }
    }
    
    /*
    * 双重循环实现删除重复元素
    *
    */
    public void deleDuplecate(){
        Node currNode = head;
        Node nextNode;
        while(currNode != null){
            nextNode = currNode;
            while(nextNode.next != null){
                if(nextNode.next.data == currNode.data){
                    nextNode.next = nextNode.next.next;
                }
                else{
                   nextNode = nextNode.next; 
                }
                
            }
            currNode = currNode.next;
        }
    }
    
    /*
    * Hashtable实现删除重复元素
    *
    */
    public void deleDuplecateHash(){
        Node currNode = head;
        Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
        Node preNode = head;
        while(currNode != null){
            if(table.containsKey(currNode.data)){
                preNode.next = currNode.next;
            }
            else{
                table.put(currNode.data, 1);
                preNode = currNode;
            }
            currNode = currNode.next;
        }
    }
    
    /*
    * 打印单链表中的倒数第k个元素
    *
    */
    public void findElem(int k){
        Node p = head;
        Node q = head;
        for(int i = 0; i < k; i++){
            q = q.next;
        }
        while(q != null){
            p = p.next;
            q = q.next;
        }
        System.out.println("The kth element is : " + p.data + " begin end Node.");
    }
    
    public static void main(String[] args){
        MyLinkedList list = new MyLinkedList();
        list.add(5);
        list.add(9);
        list.add(5);
        list.add(3);
        System.out.println("排序前");
        list.printList();
        System.out.println("排序后");
        list.orderList();
        list.printList();
        System.out.println("The length of list is " + list.length());
        // list.deleduplecate();
        // system.out.println("delete duplecate elements");
        // list.printlist();
        //list.findElem(1);
        System.out.println("print list reversely:");
        list.printListReversely(list.head);
        
        }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值