• 链表顺序存储Java实现


    package com.wrh.lab.dataStructure.linearList;
    /**
    *
    *
    @author wrh
    * the interface of linear list.
    *
    */

    public interface LinearList<E> {

    /**
    * identify whether the list is empty or not
    *
    @return true for empty and false for not empty
    */

    public boolean isEmpty();

    /**
    * get the length of the list
    *
    @return the length of the list
    */

    public int getLength();

    /**
    * return the element in index
    *
    @param index
    *
    @return the element in index
    */

    public E get(int index);

    /**
    *
    *
    @param index
    *
    @param element
    *
    @return the former element
    */

    public E set(int index, E element);

    /**
    * add element after the index
    *
    @param index
    *
    @param element
    *
    @return true for add element success and false for add fail
    */

    public boolean add(int index, E element);

    /**
    * add the element at the end of the list
    *
    @param element
    *
    @return true for success and false for fail
    */

    public boolean addLast(E element);

    /**
    * remove the element at index and return this element
    *
    @param index
    *
    @return
    */

    public E remove(int index);

    /**
    * show the elements in the list
    *
    @return
    */

    public void display();

    /**
    * clear the list
    */

    public void clear();
    }
    package com.wrh.lab.dataStructure.linearList.impl;

    import com.wrh.lab.dataStructure.linearList.LinearList;

    public class LinearListImpl<E> implements LinearList<E> {

    private E[] entry; //store the element
    private int length; //the length of the linear list
    private static final int MAX_SIZE = 50;


    // constructor
    public LinearListImpl(){}


    //constructor
    public LinearListImpl(E[] entry){

    this.entry = entry;
    this.length = entry.length;
    }

    @Override
    public boolean isEmpty() {
    boolean isEmpty = false;
    if (length == 0){
    isEmpty = true;
    } else {
    isEmpty = false;
    }
    return isEmpty;
    }

    @Override
    public int getLength() {
    return this.length;
    }

    @Override
    public E get(int index) {
    if (length > index) {
    return entry[index];
    } else {
    return null;
    }
    }

    @Override
    public E set(int index, E element) {
    if (length > index) {
    E e = entry[index];
    entry[index] = element;
    return e;
    } else {
    return null;
    }
    }

    @Override
    public boolean add(int index, E element) {
    boolean isSuc = false;
    if (length > index && length < MAX_SIZE) {
    resizeList(entry);
    int newPos = index + 1;
    for (int i = length; i > newPos; i--) {
    entry[i] = entry[i - 1];
    }
    entry[newPos] = element;
    length +=1;
    isSuc = true;
    } else {
    isSuc = false;
    }
    return isSuc;
    }

    @Override
    public boolean addLast(E element) {
    boolean isSuc = false;
    if (length < MAX_SIZE) {
    resizeList(entry);
    entry[length] = element;
    length += 1;
    isSuc = true;
    } else {
    isSuc = false;
    }
    return isSuc;
    }

    @Override
    public E remove(int index) {
    if (length > 0) {
    E e = entry[index];
    for (int i = index; i < length -1; i ++) {
    entry[i] = entry[i + 1];
    }
    length -= 1;
    return e;
    } else {
    return null;
    }
    }

    @Override
    public void clear() {
    entry = null;
    length = 0;
    }

    @Override
    public void display() {
    if (length > 0 && length < MAX_SIZE) {
    for(int i = 0; i < length; i++) {
    System.out.print(entry[i] + " ");
    }
    } else {
    System.out.println("null");
    }

    }

    /**
    * resize the list
    *
    @param entry
    */

    public void resizeList(E[] entry) {
    int resize = length + 1;
    Object[] seqList_upp = new Object[resize];
    System.arraycopy(entry,0,seqList_upp,0,length);
    this.entry = (E[]) seqList_upp;
    }

    //test the list
    public static void main(String[] args) {

    Integer[] a = {0,1,2,3,4,5,6,7,8,9};
    LinearList<Integer> l = new LinearListImpl<Integer>(a);
    System.out.println(l.getLength());
    System.out.println(l.get(8));
    l.display();
    System.out.println();
    l.add(3, 12);
    System.out.println(l.getLength());
    l.remove(2);
    System.out.println(l.getLength());
    l.display();
    l.clear();
    System.out.println();
    l.display();
    }


    }



  • 相关阅读:
    项目管理改进实践
    Abount StoneAge Dictionary Project
    词库引擎核心设计
    JDOM / XPATH编程指南
    2007年图灵奖揭晓
    pjscrape: A webscraping framework written in Javascript, using PhantomJS and jQuery
    centos install node.js
    Posts tagged pyqt4
    install python262 写得不错
    WebSPHINX: A Personal, Customizable Web Crawler
  • 原文地址:https://www.cnblogs.com/wrh526/p/2354577.html
Copyright © 2020-2023  润新知