链表的Java实现


import java.lang.System;
public class Hello
{
    public static void main(String[] args)
    {
        LinkList List = new LinkList();
        List.add(1);
        List.add(2);
        List.add(3);
        List.add(4);
        List.print();
        int data;
        data = List.deleteElemAt(2);
        List.print();
        System.out.println("the value of data deleted is :"+data);
                List.reverse();
        List.print();
    }


}
class Node//元素结点
{
    public int data;
    public  Node next;  
    public Node(int data)
    {
        this.data = data;
        this.next = null;
    }
}

class LinkList
{
    private Node head;//头结点指针
    private int length;//链表长度
    private Node tail;//尾结点指针
        public LinkList()
        {
            head = new Node(0);
            head.next = null;
            tail = head;
        }

        public void insert(int index, int e)
        {
            if(index>this.length+1 || index<=0) return ;
            if(index==this.length+1) 
            {
                this.add(e);
            }
            Node p = this.head;
            int i=0;
            while(p.next != null)
            {
                ++i;
                if(i==index)
                {
                    Node temp = new Node(e);
                    temp.next = p.next;
                    p.next = temp;
                    ++this.length;
                }
                p = p.next;
            }
        }

        public void add(int e)
        {
                    Node temp = new Node(e);
                    this.tail.next = temp;
                    this.tail = temp;
                    ++ this.length;
        }

        public boolean isEmpty()
        {
            return length == 0;
        }

        public void reverse()
        {
            Node p = this.head.next;
            Node q = null;
            this.head.next = null;
            while(p!=null)
            {
                q = p;
                p=p.next;
                q.next = this.head.next;
                this.head.next = q;

            }
        }

        public void print()
        {
            Node p = this.head.next;
            while(p!=null)
            {
                System.out.print(p.data+"	");
                p=p.next;
            }
            System.out.println();
        }

        public int length()
        {
            return this.length;
        }

        public int deleteElemAt(int index)
        {
            if(index>this.length || index<=0) return -9999;
            Node p = this.head;
            int i = 0;
            while(p.next != null)
            {
                ++i;
                if(i==index)
                {
                    Node temp = p.next;
                    p.next = temp.next;         
                    return temp.data;

                }
                p = p.next;
            }
            return  -9999;
        }
}


//结果:

D:ToolsUltraEdit-32Data
λ java Hello
1       2       3       4
1       3       4
the value of data deleted is :2
4       3       1
原文地址:https://www.cnblogs.com/yldf/p/6249881.html