java实现单链表

来源:互联网 发布:ubuntu显示文件 编辑:程序博客网 时间:2024/06/07 01:09

我们开始学习java实现单链表。

 

单链表类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package linklist;
 
public class LinkList {
     
    class Element
    {
        public Object value=null;
        private Element next=null;
    }
    private Element header = null;//头结点
    /**
     * 初始化链表
     * */
    void initList()
    {
        header = new Element();
        header.value=null;
        header.next=null;
    }
     
    /**
     * 插入链表
     * */
    void insertList(Object o)
    {
        Element e=new Element();
        e.value=o;
        if(header.next==null)//第一次插入元素
        {
            header.next=e;
        }else//不是第一次插入元素
        {
            //temp引用在栈中,temp和header引用都指向堆中的initList()中new的Element对象
            Element temp = header;
            while(temp.next!=null)//寻找最后一个元素
            {
                temp=temp.next;
            }
            temp.next=e;
        }
    }
     
    /**
     * 删除链表中第i个元素
     * */
    void deletelist(Object o)
    {
        Element temp =header;
        while(temp.next!=null)
        {
            //判断temp当前指向的结点的下一个结点是否是要删除的结点
            if(temp.next.value.equals(o))
            {
                temp.next=temp.next.next;//删除结点
            }else
            {
                temp=temp.next;//temp“指针”后移
            }
        }
    }
     
    /**
     * 获取链表的第i个位置的元素
     * */
    Element getElement(int i)
    {
        if(i<=0 || i>size())
        {
            System.out.println("获取链表的位置有误!返回null");
            return null;
        }
        else
        {
            int count =0;
            Element element = new Element();
            Element temp = header;
            while(temp.next!=null)
            {
                count++;
                if(count==i)
                {
                    element.value=temp.next.value;
                }
                temp=temp.next;
            }
            return element;
        }
    }
    /**
     * 链表长度
     * */
    int size()
    {
        Element temp = header;
        int size=0;
        while(temp.next!=null)
        {
            size++;
            temp=temp.next;
        }
        return size;
    }
     
    /**
     * 判断链表中是否存在某元素
     * */
    Boolean isContain(Object o)
    {
        Element temp =header;
        while(temp.next!=null)
        {
            if(temp.next.value.equals(o))
            {
                return true;
            }
            temp=temp.next;
        }
        return false;
    }
    /**
     * 打印链表
     * */
    void print()
    {
        System.out.print("打印链表:");
        Element temp =header;
        while(temp.next!=null)
        {
            temp=temp.next;
            System.out.print(temp.value+"\t");
        }
        System.out.println();
    }
}

  测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package linklist;
 
public class LinkListMain {
 
    public static void main(String[] args) {
        LinkList lList = new LinkList();
        lList.initList();
        lList.insertList(1);
        lList.insertList(2);
        lList.insertList(3);
        lList.insertList(4);
        lList.insertList(5);
        lList.print();
        lList.deletelist(2);
        lList.print();
        System.out.println("链表长度:"+lList.size());
        System.out.println("第1个元素值为:"+lList.getElement(1).value);
        System.out.println("第2个元素值为:"+lList.getElement(2).value);
        System.out.println("第3个元素值为:"+lList.getElement(3).value);
        System.out.println("第4个元素值为:"+lList.getElement(4).value);
         
        System.out.println(lList.isContain(2));
        System.out.println(lList.isContain(6));
        System.out.println(lList.isContain(5));
    }
 
}

  

0 0