手动实现基于java栈和队列。
栈:
package base_struct.hashmap;
public class DhStack <K>{
private class Data<K>{
public K k;
public Data(K k){
this.k=k;
}
}
private int size;
private int offet;
private Data<K>[] data;
public DhStack(int a)
{
data=new Data[a];
size=a;
offet=0;
}
public synchronized void put(K k)
{
Data<K> temp=new Data<K>(k);
data[offet]=temp;
offet++;
}
public synchronized K get()
{
offet=offet-1;
if(offet<0)
{
return null;
}
K a=data[offet].k;
return data[offet].k;
}
}
对列:
public class DhQueue<K> {
private class Data<K>{
public K k;
public Data<K> next;
public Data(K k){
this.k=k;
}
}
/*
* 限制队列大小的参数
*/
private int size;
private int offet;
private Data<K> dataLinkedList;
public DhQueue(int a)
{
size=a;
}
public synchronized void put(K k)
{
if(offet>=size-1)
{
throw new IllegalArgumentException();
}
Data<K> data=new Data<K>(k);
Data<K> temp=dataLinkedList;
if(dataLinkedList==null)
{
dataLinkedList=data;
}else{
while (temp.next != null) {
temp = temp.next;
}
temp.next=data;
}
offet++;
}
public synchronized K get()
{
if(offet<=0)
{
return null;
}
K a=dataLinkedList.k;
dataLinkedList=dataLinkedList.next;
offet=offet-1;
return a;
}
}