import java.util.Arrays;
public class SequenceList<T>
{
private final int DEFAULT_SIZE = 16;
private int capacity;
private int size = 0;
private Object[] elementData;
public SequenceList()
{
capacity = DEFAULT_SIZE;
elementData = new Object[capacity];
}
public SequenceList(T element)
{
this();
elementData[0] = element;
size ++;
}
public SequenceList(T element, int initSize)
{
capacity = 1;
while(capacity < initSize)
{
capacity <<= 1;
}
elementData = new Object[capacity];
elementData[0] = element;
size++;
}
public int length()
{
return size;
}
@SuppressWarnings("unchecked")
public T get(int i)
{
if(i < 0 || i > size - 1)
{
throw new IndexOutOfBoundsException("线性表索引越界");
}
return (T)elementData[i];
}
public int locate(T element)
{
for(int i = 0; i < size; i ++)
{
if(elementData[i].equals(element))
{
return i;
}
}
return -1;
}
public void insert(T element, int index)
{
if(index < 0 || index > size)
{
throw new IndexOutOfBoundsException("线性表索引越界");
}
ensureCapacity(size + 1);
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = element;
size ++;
}
public void add(T element)
{
insert(element, size);
}
private void ensureCapacity(int minCapacity)
{
if(minCapacity > capacity)
{
while(capacity < minCapacity)
{
capacity <<= 1;
}
elementData = Arrays.copyOf(elementData, capacity);
}
}
@SuppressWarnings("unchecked")
public T delete(int index)
{
if(index < 0 || index > size - 1)
{
throw new IndexOutOfBoundsException("线性表索引越界");
}
T oldValue = (T)elementData[index];
int numMoved = size - index - 1;
if(numMoved > 0)
{
System.arraycopy(elementData, index + 1, elementData, index, numMoved);
}
elementData[--size] = null;
return oldValue;
}
public T remove()
{
return delete(size - 1);
}
public boolean empty()
{
return size == 0;
}
public void clear()
{
Arrays.fill(elementData, null);
size = 0;
}
public String toString()
{
if(size == 0)
{
return "[]";
}
else
{
StringBuilder sb = new StringBuilder("[");
for(int i = 0; i < size; i ++)
{
if(i < size - 1)
{
sb.append(elementData[i].toString()).append(", ");
}
else
{
sb.append(elementData[i]).append("]");
}
}
return sb.toString();
}
}
public static void main(String[] args)
{
SequenceList<String> list = new SequenceList<String>();
list.add("ru");
list.add("hua");
list.insert("shi", 0);
System.out.println(list);
}
}
sequencequque
package 数据结构;
import java.util.Arrays;
public class SequenceQueue<T>
{
private int DEFAULT_SIZE = 10;
private int capacity;
private Object[] elementData;
private int front = 0;
private int rear = 0;
public SequenceQueue()
{
capacity = DEFAULT_SIZE;
elementData = new Object[capacity];
}
public SequenceQueue(T element)
{
elementData[0] = element;
rear ++;
}
public SequenceQueue(T element, int initSize)
{
this.capacity = initSize;
elementData = new Object[capacity];
elementData[0] = element;
rear ++;
}
public int length()
{
return rear - front;
}
public void add(T element)
{
if(rear > capacity - 1)
{
throw new IndexOutOfBoundsException("队列已满异常");
}
elementData[rear ++] = element;
}
public boolean isEmpty()
{
return rear == front;
}
@SuppressWarnings("unchecked")
public T remove()
{
if(isEmpty())
{
throw new IndexOutOfBoundsException("空队列异常");
}
T oldValue = (T)elementData[front];
elementData[front ++] = null;
return oldValue;
}
@SuppressWarnings("unchecked")
public T topElement()
{
if(isEmpty())
{
throw new IndexOutOfBoundsException("空队列异常");
}
return (T)elementData[front];
}
public void clear()
{
Arrays.fill(elementData, null);
front = 0;
rear = 0;
}
public String toString()
{
if(isEmpty())
{
return "[]";
}
else
{
StringBuilder sb = new StringBuilder("[");
for(int i = front; i < rear; i ++)
{
if(i < rear - 1)
{
sb.append(elementData[i].toString()).append(", ");
}
else
{
sb.append(elementData[i].toString()).append("]");
}
}
return sb.toString();
}
}
public static void main(String[] args)
{
SequenceQueue<String> sq = new SequenceQueue<String>();
sq.add("ru");
sq.add("hua");
sq.add("shu");
sq.add("shu");
System.out.println(sq);
sq.remove();
System.out.println(sq);
}
}
package 数据结构;
public class SingleDirLinkedList<T>
{
private class Node
{
private T data;
private Node next;
public Node(T data, Node next)
{
this.data = data;
this.next = next;
}
}
// 头结点
private Node header;
// 尾结点
private Node tail;
private int size;
public SingleDirLinkedList()
{
header = null;
tail = null;
}
public SingleDirLinkedList(T element)
{
header = new Node(element, null);
tail = header;
size ++;
}
public int length()
{
return size;
}
private Node getNodeByIndex(int index)
{
if(index < 0 || index > size - 1)
{
throw new IndexOutOfBoundsException("线性表索引越界");
}
Node current = header;
for(int i = 0; i< size && current != null ; i++, current = current.next)
{
if(i == index)
{
return current;
}
}
return null;
}
public T get(int index)
{
return getNodeByIndex(index).data;
}
public int locate(T element)
{
Node current = header;
for(int i = 0; i < size && current != null; i++, current = current.next)
{
if(current.equals(element))
{
return i;
}
}
return -1;
}
// 尾插法 插入链表
public void addTail(T element)
{
if(header == null)
{
header = new Node(element, null);
tail = header;
}
else
{
Node node = new Node(element, null);
tail.next = node;
tail = node;
}
size ++;
}
// 头插法 插入链表
public void addHeader(T element)
{
header = new Node(element, header);
if(tail == null)
{
tail = header;
}
size ++;
}
public void insert(T element, int index)
{
if(index < 0 || index > size - 1)
{
throw new IndexOutOfBoundsException("线性表索引越界");
}
if(header == null)
{
addTail(element);
}
else
{
if(index == 0)
{
addHeader(element);
}
else
{
Node prev = getNodeByIndex(index - 1);
prev.next = new Node(element, prev.next);
size ++;
}
}
}
public T delete(int index)
{
if(index < 0 || index > size - 1)
{
throw new IndexOutOfBoundsException("线性表索引越界");
}
Node del = null;
if(index == 0)
{
del = header;
header = del.next;
}
else
{
Node prev = getNodeByIndex(index - 1);
del = prev.next;
prev.next = del.next;
del.next = null;
}
size --;
return del.data;
}
public T removeTail()
{
return delete(size - 1);
}
public boolean isEmpty()
{
return size == 0;
}
public void clear()
{
header = null;
tail = null;
size = 0;
}
public String toString()
{
if(isEmpty())
{
return "[]";
}
else
{
StringBuilder sb = new StringBuilder("[");
Node current = header;
for(int i = 0; i < size && current != null; i++, current = current.next)
{
if(i < size - 1)
{
sb.append(current.data).append(", ");
}
else
{
sb.append(current.data).append("]");
}
}
return sb.toString();
}
}
public static void main(String[] args)
{
SingleDirLinkedList<String> sdll = new SingleDirLinkedList<String>();
sdll.addTail("ru");
sdll.addTail("hua");
System.out.println(sdll);
sdll.insert("shi", 1);
System.out.println(sdll);
}
}