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);
        
        }
}

猜你喜欢

转载自blog.csdn.net/qq_26552071/article/details/86220351