线性表Java实现
顺序表
MyArrayList.java
package MYLINERLIST;
/**
* ****************************
* * 顺序表 *
* ****************************
*
*/
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyArrayList <AnyType> implements Iterable<AnyType>
{
private static final int DEFAULT_CAPACITY = 10;
private int theSize;
private AnyType [] theItems;
/* 构造方法 */
public MyArrayList()
{
doClear();
}
/* 清除表 */
public void clear()
{ doClear(); }
private void doClear()
{
this.theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
/* 返回表长 */
public int size()
{
return theSize;
}
/* 判断表是否为空 */
public boolean isEmpty()
{
return size() == 0;
}
public void trimToSize()
{
ensureCapacity(size());
}
/* 返回idx处的元素 */
public AnyType get(int idx)
{
if (idx < 0 || idx>=size())
throw new ArrayIndexOutOfBoundsException();
return theItems[idx];
}
/* 改变idx处的元素 */
public AnyType set(int idx, AnyType newval)
{
if (idx < 0 || idx>=size())
throw new ArrayIndexOutOfBoundsException();
AnyType old = theItems[idx];
theItems[idx] = newval;
return old;
}
/* 确保容量 至少有newCapacity 不够就复制添加 */
private void ensureCapacity(int newCapacity) {
if (newCapacity < theSize)
return;
AnyType [] old = theItems;
theItems = (AnyType []) new Object[newCapacity];
for (int i = 0; i<size(); i++)
{
theItems[i] = old[i];
}
}
/* 在表尾添加元素 */
public boolean add(AnyType x)
{
add(size(), x);
return true;
}
/* 在指定位置idx 插入元素 */
public void add(int idx, AnyType x)
{
if (theItems.length == size())
ensureCapacity(size() *2 +1);
for (int i = theSize; i>idx; i--)
theItems[i] = theItems[i-1];
theItems[idx] = x;
theSize ++;
}
/* 打印表 */
public void printList()
{
for (int i = 0;i<size();i++)
{
System.out.print(this.get(i)+" |");
}
System.out.println();
}
/* 删除idx处的元素 */
public AnyType remove(int idx)
{
AnyType removedItem = theItems[idx];
for (int i=idx; i<size()-1; i++)
theItems[i] = theItems[i+1];
theSize --;
return removedItem;
}
@Override
public Iterator<AnyType> iterator() {
return new ArrayListIterator();
}
private class ArrayListIterator implements Iterator<AnyType>
{
public int current = 0;
@Override
public boolean hasNext() {
return current < size();
}
@Override
public AnyType next() {
if(!hasNext())
throw new NoSuchElementException();
return theItems[current++];
}
@Override
public void remove() {
MyArrayList.this.remove(--current);
}
}
}
链表
MyLinkedList.java
package MYLINERLIST;
/**
* ****************************
* * 链表 *
* ****************************
*
*/
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class MyLinkedList<AnyType> {
private LinkedList<AnyType> list;
private int length;
public void InitList()
{
length = 0;
list = new LinkedList<AnyType>();
}
public int Length()
{return length;}
public boolean isEmpty()
{return Length()==0;}
/* 返回i位序的元素 */
public AnyType getElem(int i)
{
if (!(i>=1&&i<=length))
{
System.out.println("链表访问越界");
throw new ArrayIndexOutOfBoundsException();
}
Iterator<AnyType> iter = list.listIterator(i-1);
return iter.next();
}
/* 修改i位序的元素 */
public void setElem(int i, AnyType e)
{
list.set(i-1, e);
}
/* 在i位序插入元素 */
public void ListInsert(int i, AnyType e)
{
if (!(i>=1&&i<=length+1))
{
System.out.println("链表插入越界");
throw new ArrayIndexOutOfBoundsException();
}
list.add(i-1,e);
length++;
System.out.println("成功在"+i+"位序插入"+e);
}
/* 删除i位序的元素 */
public AnyType ListDelete(int i)
{
if (!(i>=1&&i<=length))
{
System.out.println("链表访问越界");
throw new ArrayIndexOutOfBoundsException();
}
AnyType e = getElem(i);
list.remove(i-1);
length--;
return e;
}
/* 返回第一个等于e的元素的位序 */
public int LocateElem(AnyType e)
{
if(!isEmpty())
{
int i = 1;
var iter = list.listIterator();
while (iter.hasNext())
{
if (iter.next()==e) break;
i++;
}
if (i>=1&&i<=Length()) return i;
}
System.out.println("查无此元素");
throw new NoSuchElementException();
}
/* 打印表中所有元素 */
public void printList()
{
System.out.println("表中元素:");
for (int i = 1; i<=Length();i++)
{
System.out.print(getElem(i)+" |");
}
}
/* 清除表 */
public void clearList()
{
while (Length()>0) ListDelete(Length());
}
/* 在表头插入e*/
public void InsertFirst(AnyType e)
{
list.addFirst(e);
length++;
}
/* 在表尾插入 e */
public void InsertLast(AnyType e)
{
list.addLast(e);
length++;
}
/* 删除表头元素返回 */
public AnyType removeFirst()
{
AnyType e = getElem(1);
list.removeFirst();
length--;
return e;
}
/* 删除表尾元素返回 */
public AnyType removeLast()
{
AnyType e = getElem(Length());
list.removeLast();
length--;
return e;
}
/* 获取表头元素 */
public AnyType getFirst()
{return getElem(1);}
/* 获取表尾元素 */
public AnyType getLast()
{return getElem(Length());}
}
栈
MyStack.java
package MYLINERLIST;
/**
* ****************************
* * 栈 *
* ****************************
*
*/
import java.util.LinkedList;
import java.util.ListIterator;
public class MyStack<E> {
private LinkedList<E> list;
private int length;
public void InitStack()
{
System.out.println("栈初始化...");
list = new LinkedList<E>();
length = 0;
}
public boolean isEmpty()
{return length==0;}
public void Push(E e)
{
list.addFirst(e);
length++;
}
public int Length()
{return length;}
public E Pop()
{
if(Length()==0)
throw new IndexOutOfBoundsException();
E e = list.getFirst();
list.removeFirst();
length--;
return e;
}
public E getTop()
{
if(Length()==0)
throw new IndexOutOfBoundsException();
return list.getFirst();
}
public void clearStack()
{
if (Length()==0) return;
while (Length()>0) Pop();
}
public void printStack()
{
ListIterator<E> iter = list.listIterator();
System.out.println("栈中元素: ");
while (iter.hasNext())
System.out.print(iter.next()+"| ");
}
}
队列
MyQueue.java
package MYLINERLIST;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* ****************************
* * 队列 *
* ****************************
*
*/
public class MyQueue<E> {
private LinkedList<E> list;
private int length;
/* 初始化 */
public void InitQueue()
{
length=0;
list = new LinkedList<E>();
}
/* 返回队长 */
public int Length()
{return length;}
/* 判空 */
public boolean QueueEmpty()
{return length==0;}
/* 进队 */
public void EnQueue(E e)
{
list.addFirst(e);
length++;
}
/* 出队 */
public E DeQueue()
{
if(QueueEmpty())
throw new ArrayIndexOutOfBoundsException();
E e = list.getLast();
list.removeLast();
length--;
return e;
}
/* 取队头 */
public E GetHead()
{
if(QueueEmpty())
throw new ArrayIndexOutOfBoundsException();
return list.getLast();
}
/* 清空队 */
public void ClearQueue()
{
while (!QueueEmpty())
DeQueue();
}
/* 打印队 */
public void PrintQueue()
{
ListIterator<E> iter = list.listIterator();
System.out.println("队中元素: ");
while (iter.hasNext())
System.out.print(iter.next()+"| ");
}
}
测试
MyLinerList.java
package MYLINERLIST;
/**
* 线性表的测试类
* 用于测试线性表的各种操作
*
*/
public class MyLinerList {
public static void main(String[] args) {
System.out.println("*******************");
System.out.println("* 线性表测试 *");
System.out.println("*******************");
System.out.println("\n");
System.out.println("*******************");
System.out.println("* 顺序表测试 *");
System.out.println("*******************");
/* Integer表测试 */
System.out.println("/* Integer表测试 */");
System.out.println("*******************************************************************************");
MyArrayList<Integer> ArrayListInteger = new MyArrayList<Integer>();
for (int i = 1; i<24; i++)
{
ArrayListInteger.add(i);
}
ArrayListInteger.printList();
System.out.print("\n");
ArrayListInteger.add(20,999);
ArrayListInteger.printList();
ArrayListInteger.remove(8);
System.out.print("\n");
ArrayListInteger.printList();
System.out.println("*******************************************************************************");
System.out.println();
/* Character表测试 */
System.out.println("/* Character表测试 */");
System.out.println("*******************************************************************************");
MyArrayList<Character> ArrayListCharacter = new MyArrayList<Character>();
for (char i = 97; i<24+97; i++)
{
ArrayListCharacter.add(i);
}
ArrayListCharacter.printList();
System.out.print("\n");
ArrayListCharacter.add(20,'z');
ArrayListCharacter.printList();
ArrayListCharacter.remove(8);
System.out.print("\n");
ArrayListCharacter.printList();
System.out.println("*******************************************************************************");
System.out.println("\n");
System.out.println("*******************");
System.out.println("* 链表测试 *");
System.out.println("*******************");
/* Integer链表测试*/
System.out.println("/* Integer链表测试*/");
var LinkedListInteger = new MyLinkedList<Integer>();
System.out.println("***初始化***");
LinkedListInteger.InitList();
System.out.println("***判空***");
if(LinkedListInteger.isEmpty()) System.out.println("表为空");
else System.out.println("表非空");
System.out.println("***插入***");
for(int i = 1; i<24; i++)
{
LinkedListInteger.ListInsert(i, i*i);
}
System.out.println("***删除***");
System.out.println("删除前:");
LinkedListInteger.printList();
LinkedListInteger.ListDelete(5);
System.out.println();
System.out.println("删除后:");
System.out.println();
LinkedListInteger.printList();
System.out.println();
System.out.println("***求表长***");
System.out.println("表长为: "+LinkedListInteger.Length());
System.out.println("***按位序访问***");
System.out.println("位序5的元素为"+LinkedListInteger.getElem(5));
System.out.println("***查找元素***");
System.out.println("36的位序是: "+ LinkedListInteger.LocateElem(36));
System.out.println("***清除表***");
System.out.println("清除前:");
System.out.println("表长为: "+LinkedListInteger.Length());
LinkedListInteger.clearList();
System.out.println("清除后:");
System.out.println("表长为: "+LinkedListInteger.Length());
/* Character链表测试*/
System.out.println("/* Character链表测试*/");
var LinkedListCharacter = new MyLinkedList<Character>();
System.out.println("***初始化***");
LinkedListCharacter.InitList();
System.out.println("***判空***");
if(LinkedListCharacter.isEmpty()) System.out.println("表为空");
else System.out.println("表非空");
System.out.println("***插入***");
for(int i = 1; i<24; i++)
LinkedListCharacter.ListInsert(i, (char)(i+96));
System.out.println("***删除***");
System.out.println("删除前:");
LinkedListCharacter.printList();
LinkedListCharacter.ListDelete(5);
System.out.println();
System.out.println("删除后:");
LinkedListCharacter.printList();
System.out.println();
LinkedListCharacter.printList();
System.out.println();
System.out.println("***求表长***");
System.out.println("表长为: "+LinkedListCharacter.Length());
System.out.println("***按位序访问***");
System.out.println("位序5的元素为"+LinkedListCharacter.getElem(5));
System.out.println("***查找元素***");
System.out.println("r的位序是: "+ LinkedListCharacter.LocateElem('r'));
System.out.println("***修改元素***");
System.out.println("修改前:");
LinkedListCharacter.printList();
System.out.println();
LinkedListCharacter.setElem(5,'k');
System.out.println("修改后:");
LinkedListCharacter.printList();
System.out.println();
System.out.println("***清除表***");
System.out.println("清除前:");
System.out.println("表长为: "+LinkedListCharacter.Length());
LinkedListCharacter.clearList();
System.out.println("清除后:");
System.out.println("表长为: "+LinkedListCharacter.Length());
System.out.println();
System.out.println();
System.out.println("************************");
System.out.println("* 栈测试 *");
System.out.println("************************");
var CharacterStack = new MyStack<Character>();
System.out.println("***初始化***");
CharacterStack.InitStack();
System.out.println("当前栈长为: "+CharacterStack.Length());
System.out.println("***压入元素***");
for(int i = 1; i<24; i++)
CharacterStack.Push((char)(i+96));
System.out.println("压入后: ");
CharacterStack.printStack();
System.out.println();
System.out.println("当前栈长为: "+CharacterStack.Length());
System.out.println();
System.out.println("***取栈顶***");
System.out.println("当前栈顶元素是: "+CharacterStack.getTop());
System.out.println("当前栈长为: "+CharacterStack.Length());
System.out.println("***弹出栈顶***");
System.out.println("弹出的栈顶元素是: "+CharacterStack.Pop());
System.out.println("弹出后: ");
CharacterStack.printStack();
System.out.println();
System.out.println("当前栈长为: "+CharacterStack.Length());
System.out.println("***清空栈***");
CharacterStack.clearStack();
System.out.println("当前栈长为: "+CharacterStack.Length());
System.out.println();
System.out.println();
System.out.println("************************");
System.out.println("* 队列测试 *");
System.out.println("************************");
var CharacterQueue = new MyQueue<Character>();
System.out.println("***初始化***");
CharacterQueue.InitQueue();
System.out.println("当前队长为: "+CharacterQueue.Length());
System.out.println("***入队***");
for(int i = 1; i<24; i++)
CharacterQueue.EnQueue((char)(i+96));
System.out.println("入队后: ");
CharacterQueue.PrintQueue();
System.out.println();
System.out.println("当前队长为: "+CharacterQueue.Length());
System.out.println();
System.out.println("***出队***");
System.out.println("出队元素是: "+CharacterQueue.DeQueue());
System.out.println("出队后: ");
CharacterQueue.PrintQueue();
System.out.println();
System.out.println("当前队长为: "+CharacterQueue.Length());
System.out.println("***清空队***");
CharacterQueue.ClearQueue();
System.out.println("当前队长为: "+CharacterQueue.Length());
}
}
运行结果
*******************
* 线性表测试 *
*******************
*******************
* 顺序表测试 *
*******************
/* Integer表测试 */
*******************************************************************************
1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |
1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |999 |21 |22 |23 |
1 |2 |3 |4 |5 |6 |7 |8 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |999 |21 |22 |23 |
*******************************************************************************
/* Character表测试 */
*******************************************************************************
a |b |c |d |e |f |g |h |i |j |k |l |m |n |o |p |q |r |s |t |u |v |w |x |
a |b |c |d |e |f |g |h |i |j |k |l |m |n |o |p |q |r |s |t |z |u |v |w |x |
a |b |c |d |e |f |g |h |j |k |l |m |n |o |p |q |r |s |t |z |u |v |w |x |
*******************************************************************************
*******************
* 链表测试 *
*******************
/* Integer链表测试*/
***初始化***
***判空***
表为空
***插入***
成功在1位序插入1
成功在2位序插入4
成功在3位序插入9
成功在4位序插入16
成功在5位序插入25
成功在6位序插入36
成功在7位序插入49
成功在8位序插入64
成功在9位序插入81
成功在10位序插入100
成功在11位序插入121
成功在12位序插入144
成功在13位序插入169
成功在14位序插入196
成功在15位序插入225
成功在16位序插入256
成功在17位序插入289
成功在18位序插入324
成功在19位序插入361
成功在20位序插入400
成功在21位序插入441
成功在22位序插入484
成功在23位序插入529
***删除***
删除前:
表中元素:
1 |4 |9 |16 |25 |36 |49 |64 |81 |100 |121 |144 |169 |196 |225 |256 |289 |324 |361 |400 |441 |484 |529 |
删除后:
表中元素:
1 |4 |9 |16 |36 |49 |64 |81 |100 |121 |144 |169 |196 |225 |256 |289 |324 |361 |400 |441 |484 |529 |
***求表长***
表长为: 22
***按位序访问***
位序5的元素为36
***查找元素***
36的位序是: 5
***清除表***
清除前:
表长为: 22
清除后:
表长为: 0
/* Character链表测试*/
***初始化***
***判空***
表为空
***插入***
成功在1位序插入a
成功在2位序插入b
成功在3位序插入c
成功在4位序插入d
成功在5位序插入e
成功在6位序插入f
成功在7位序插入g
成功在8位序插入h
成功在9位序插入i
成功在10位序插入j
成功在11位序插入k
成功在12位序插入l
成功在13位序插入m
成功在14位序插入n
成功在15位序插入o
成功在16位序插入p
成功在17位序插入q
成功在18位序插入r
成功在19位序插入s
成功在20位序插入t
成功在21位序插入u
成功在22位序插入v
成功在23位序插入w
***删除***
删除前:
表中元素:
a |b |c |d |e |f |g |h |i |j |k |l |m |n |o |p |q |r |s |t |u |v |w |
删除后:
表中元素:
a |b |c |d |f |g |h |i |j |k |l |m |n |o |p |q |r |s |t |u |v |w |
表中元素:
a |b |c |d |f |g |h |i |j |k |l |m |n |o |p |q |r |s |t |u |v |w |
***求表长***
表长为: 22
***按位序访问***
位序5的元素为f
***查找元素***
r的位序是: 17
***修改元素***
修改前:
表中元素:
a |b |c |d |f |g |h |i |j |k |l |m |n |o |p |q |r |s |t |u |v |w |
修改后:
表中元素:
a |b |c |d |k |g |h |i |j |k |l |m |n |o |p |q |r |s |t |u |v |w |
***清除表***
清除前:
表长为: 22
清除后:
表长为: 0
************************
* 栈测试 *
************************
***初始化***
栈初始化...
当前栈长为: 0
***压入元素***
压入后:
栈中元素:
w| v| u| t| s| r| q| p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a|
当前栈长为: 23
***取栈顶***
当前栈顶元素是: w
当前栈长为: 23
***弹出栈顶***
弹出的栈顶元素是: w
弹出后:
栈中元素:
v| u| t| s| r| q| p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a|
当前栈长为: 22
***清空栈***
当前栈长为: 0
************************
* 队列测试 *
************************
***初始化***
当前队长为: 0
***入队***
入队后:
队中元素:
w| v| u| t| s| r| q| p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a|
当前队长为: 23
***出队***
出队元素是: a
出队后:
队中元素:
w| v| u| t| s| r| q| p| o| n| m| l| k| j| i| h| g| f| e| d| c| b|
当前队长为: 22
***清空队***
当前队长为: 0
参考
《Java核心技术》
《算法》