链表-顺序存储-Java实现
2012-02-16 17:00
381 查看
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(); } }
相关文章推荐
- java实现线性表的顺序存储
- 队列(顺序存储) java实现
- 顺序存储链表 C语言实现
- 数据结构:线性表的链式存储(单向链表)--Java实现
- java实现线性表的顺序存储操作
- C++学习总结——顺序存储,链式存储,索引存储,哈希存储的优缺点,以及vector数组,list链表,十字链表,索引结构,hash表的实现
- 基于顺序链表的栈的顺序存储的C风格实现
- 数据结构Java实现——④数组——>稀疏矩阵三元组顺序存储
- 线性表的顺序存储及JAVA实现
- 线性表 顺序存储 链式存储 ---java实现
- 栈的Java实现(顺序存储实现与链式存储实现)
- Java数据结构与算法之顺序表和链表实现栈和队列
- 【第五节】Java代码实现队列----【链表的存储形式】
- 线性表的顺序存储之java实现
- c++ 顺序存储和链表存储 栈的实现代码
- 数据结构——单向链表的顺序存储及实现
- 线性表 顺序存储 链式存储 ---java实现
- 二叉树的顺序存储和链式存储结构(java实现)
- 用Java实现线性表中的顺序存储的学习和源码(合并两个线性表)
- 单链表-链式存储-Java实现