在Java中链表是否就是linkedlist类?
- package ChapterFive; 结点 (环)
- class Link<E> {
- public E data;
- public Link<E> next; (指向)
- public Link(E data) {
- this.data = data;
- }
- }
- class LinkList<E> { 链表(一堆环)
- public Link<E> first; (空)
- //链表中数据项的个数
- public int size;
- public LinkList() {
- first = null;
- size = 0;
- }
- //在表头插入新的数据
- public void insertFirst(E value) {
- Link<E> link = new Link<E>(value);
- link.next = first;
- first = link;
- size++;
- }
- //判断链表是否为空
- public boolean isEmpty() {
- return size == 0;
- }
- //删除表头
- public Link<E> deleteFirst() {
- Link<E> temp = first;
- first = first.next;
- size--;
- return temp;
- }
- //输出链表中的所有数据
- public void display() {
- Link<E> curr = first;
- while (curr != null) {
- System.out.print(curr.data + " ");
- curr = curr.next;
- }
- System.out.println();
- }
- //返回链表中数据项的个数
- public int size() {
- return size;
- }
- //获取从头至尾的第i个数据项
- public Link<E> get(int i) {
- if (i > size() - 1 || i < 0)
- try {
- throw new IndexOutOfBoundsException();
- } catch (Exception e) {
- e.printStackTrace();
- }
- Link<E> curr = first;
- for (int n = 0; n < size(); n++) {
- if (n == i)
- return curr;
- else
- curr = curr.next;
- }
- return null;
- }
- //输出从头至尾的第i个数据项
- public void remove(int i) {
- if (i == 0)
- deleteFirst();
- else if (i == size() - 1)
- get(i - 1).next = null;
- else {
- get(i - 1).next = get(i + 1);
- }
- size--;
- }
- }
- public class Link_list {
- public static void main(String[] args) {
- LinkList<Long> ll = new LinkList<Long>();
- for (int i = 0; i < 10; i++) {
- Long value = (long) (Math.random() * 100);
- ll.insertFirst(value);
- }
- ll.display();
- while (!ll.isEmpty()) {
- ll.deleteFirst();
- ll.display();
- }
- System.out.println("Ok");
- }
- }