得分46.25
有一个点的bug不会修(希望大佬带我),style没有注意。
1.LinkedListDeque.java

public class LinkedListDeque <T>{
private class staffnode{
private T item;
private staffnode pre;
private staffnode next; private staffnode(T x) {
item=x; }
}
private int size=1;
private staffnode first;
private staffnode last;
private staffnode standf; private LinkedListDeque(T x) {
first=new staffnode(x);
standf=new staffnode(x);
first.next=standf;
first.pre=standf;
standf.next=first;
standf.pre=first;
last=first;
size=1; } public LinkedListDeque() {
standf=new staffnode(null);
first=standf;
last=first;
size=0;
} public void addFirst(T x) { if(size==0) {
first=new staffnode(x);
standf.next=first;
standf.pre=first;
first.pre=standf;
first.next=standf;
last.pre=standf;
last.next=standf;
last=first; size++; }
else {
size++;
if(size==2) {
last.pre=first;
}
staffnode first1=new staffnode(x);
standf.next=first1;
first1.next=first;
first1.pre=standf;
first.pre=first1; first=first1;
first.pre=first1.pre;
first.next=first1.next; } }
public void addLast(T x) { if(isEmpty()) {
last=new staffnode(x);
standf.pre=last;
standf.next=last;
last.next=standf;
last.pre=standf;
first=last;
first.next=standf;
first.pre=standf;
size++; } else {
staffnode v=last;
last=new staffnode(x);
last.pre=v;
v.next=last;
last.next=standf;
standf.pre=last;
size++;
} }
public T removeFirst() {
if(size>=1) {
T e=first.item;
first=first.next;
standf.next=first;
size--;
return e;}
else
return null;
} public T getRecursive(int index){
if (index>=size)
return null; else
return getRecursive(first,index,0); } private T getRecursive(staffnode p,int index,int t){
if(t==index) return p.item;
else {
return getRecursive(p.next,index,t+1);
}
}
public T removeLast() {
if(size>=1) { T r=last.item;
last=last.pre;
last.next=standf; size--; return r;
} else return null; } public boolean isEmpty() { if(size==0) return true;
else return false;
}
public T get(int index) {
int t=0; staffnode a=first;
while(a!=standf) { if(index==t) return a.item;
a=a.next;
t++;
}
return null;
}
public void printDeque() {
staffnode p=first; while(p!=standf) {
System.out.print(p.item);
System.out.print(" ");
p=p.next;
}
System.out.print("\n");
}
public int size() {
return size;
} }

2.ArrayDeque

public class ArrayDeque<T> {
private T[] items;
private int begin;
private int size;
private int last;
private double rate;
private int start;
private int s1;
private int s2;
private int end;
public ArrayDeque() {
items = (T[]) new Object[8];
begin=items.length-1;
size=0;
rate=0;
start=0;
last=0;
end=items.length;
s1=0;
s2=0;
} public void addLast(T item) { if(last<=begin&&last<items.length) {
items[last]=item;
size++;
last++;
rate=size/items.length;
s2++;
}
else {
T[] a = (T[]) new Object[items.length*2];
if(s2>0)
System.arraycopy(items, start, a, 0,s2);
if(begin<items.length-1)
System.arraycopy(items, begin+1, a, a.length-s1, s1);
items = a;
end=items.length;
start=0;
last=s2;
begin=items.length-s1-1;
items[last]=item;
last++;
size = size + 1;
s2++;
} }
public void addFirst(T item) { if(begin>=0&&begin>=last) {
items[begin]=item;
begin--;
size++;
s1++;
rate=(double) size/items.length;
}
else {
T[] a = (T[]) new Object[items.length*2];
if(s2>0)
System.arraycopy(items, start, a, 0, s2);
if(s1>0)
System.arraycopy(items, begin+1, a, a.length-s1, s1);
items = a;
end=items.length;
begin=items.length-s1-1;
if(begin>=0&&begin<items.length)
items[begin]=item;
begin--;
start=0;
last=s2;
size = size + 1;
rate=(double) size/items.length;
s1++;
} }
private T d;
public T removeLast() {
if(s2>0) {
s2--;
size--;
last--;
d=items[last];
rate=(double) size/items.length;
if(rate<0.25&&items.length>16) {
recycle();
}
return d; }
else if(s1>0){
s1--;
end--;
size--;
d=items[end];
rate=(double) size/items.length;
if(rate<0.25&&items.length>16) {
recycle();
}
return d;
}
else
return null;
}
private int c;
public T removeFirst() {
if(s1>0) {
s1--;
size--;
begin++;
d=items[begin];
rate=(double) size/items.length;
if(rate<0.25&&items.length>16) {
recycle();
}
return d;
}
else if(s2>0) {
s2--;
d=items[start];
start=start+1;
size--;
rate=(double) size/items.length;
if(rate<0.25&&items.length>16) {
recycle();
}
return d;
}
else return null;
}
private void recycle() {
if (size > 0) {
T[] a = (T[]) new Object[size];
if (s1 > 0)
System.arraycopy(items, begin + 1, a, 0, s1);
if (s2 > 0)
System.arraycopy(items, start, a, s1, s2);
items = a;
s2 = size;
s1 = 0;
begin = items.length - 1;
end = items.length;
start = 0;
last = s2;
}
else {
T[] a = (T[]) new Object[1]; //if(begin+1<=items.length-1)
if (s1 > 0)
System.arraycopy(items, begin + 1, a, 0, s1);
if (s2 > 0)
System.arraycopy(items, start, a, s1, s2);
items = a;
s2 = size;
s1 = 0;
begin = items.length - 1;
end = items.length;
start = 0;
last = s2;
}
}
public int size() {
return size;
}
public T get(int index) {
if(index<s1)
return items[begin+index+1];
if(index>=s1&&index<size)
return items[start+(index-s1)];
else return null;
} public void printDeque() { for(int i=begin+1;i<end;i++) {
System.out.print(items[i]);
System.out.print(' ');
}
for(int j=start;j<last;j++) {
System.out.print(items[j]);
System.out.print(' ');
}
System.out.print("\n");
}
public boolean isEmpty() {
return size==0;
} }

我的失误点总结:
LinkList题目第一次没有注意给设置的pre赋值。每一次改变链表都要注意first,last以及他们之前、之后的节点的pre,next的变化。get递归写法要注意。
第二题总是忘记更新start,end的值只关心了begin和last。
在给addFirst()修改的时候也要对应修改addLast()。
remove函数同理,介于这不是简单的数组,get()函数不能简单采用数组常用的直接返回对应下标的值的办法,要判断index是在哪一部分。

CS61b proj1a的更多相关文章

  1. CS61B sp2018笔记 | Lists

    Lists csdn同作者原创地址 1. IntLists   下面我们来一步一步的实现List类,首先你可以实现一个最简单的版本: public class IntList { public int ...

  2. CS61B HW0

    The Enhanced For Loop public class EnhancedForBreakDemo { public static void main(String[] args) { S ...

  3. Java学习资源整理(超级全面)

    这里整理一些自己平常搜集的比较好的关于Java的学习资源,主要包括博客站点.书籍.课程等. 了解Java最新资讯 这部分主要是了解与Java相关的动态以及信息,能够拓展我们的视野以及寻找一些好的ide ...

  4. Java中的Iterable与Iterator详解

    在Java中,我们可以对List集合进行如下几种方式的遍历: List<Integer> list = new ArrayList<>(); list.add(5); list ...

  5. CS61A hw01

    前不久在知乎上看到CS61A 和CS61B spring18 开课的消息.上去看了一眼,发现真的不错,所有proj hw都可以本地测试!也就是说除了没有课程成绩和官方讨论区和TA解答外,这个课完全可以 ...

  6. 选择排序的实现以及如何编写测试 #CS61B-sp18-3.1

    Selection Sort的思想: 就是在一系列数字中先找到一个最小的放在所有数字的第一个位置上,然后再从余下的数字里面找最小个放在余下的数字里的第一个位置上. 例如: 在这段数据里面我们找到最小的 ...

  7. AList的具体实现 #CS61B-sp18-2.5

    实现一个Array based list,其功能包括获取长度size,添加元素至最后addLast,得到元素get和去除最后一个元素. 设计思路及其实现: 我们都知道在获取数据的时候,直接调用缓存里面 ...

  8. proj0的具体实现 #CS61B-sp18

    https://github.com/Centurybbx/sp18-century/tree/master/proj0 proj0的具体实现在上面的Github中. 在proj0中我明显感受到国外大 ...

  9. ARTS第十二周

    1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有观点和思考的技术文章 以下是 ...

随机推荐

  1. 告别痛苦,快乐学习Pandas!开源教程《Joyful-Pandas》发布

    作者:耿远昊.Datawhale团队 寄语:Pandas 是基于Numpy的一种工具,是为了解决数据分析任务而创建的,其纳入了大量库和一些标准的数据模型,提供了大量能使我们快速便捷地处理数据的函数和方 ...

  2. atomic 原子自增工程用法案例

    案例 1 : 简单用法 atomic_int id; atomic_fetch_add(&id, 1) atomic_uint id; atomic_fetch_add(&id, 1) ...

  3. 通过json动态创建控制器

    通过字符串来创建控制器 如果通过字符串来创建控制器 不可以直接通过类型来获取对应的类 因为Swift有命名空间,类前需要加上命名空间的名称 获取命名空间的名称 let executable = NSB ...

  4. mysql启动错误:mysql.sock丢失

    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 我的是Cent ...

  5. Mybatis返回插入数据的主键的两种方式

    方式一: 需要在映射文件中添加如下片段: <insert id="insertProduct" parameterType="domain.model.Produc ...

  6. TestNG--@Factory

    原文地址:http://blog.csdn.net/wanghantong TestNg的@Factory注解从字面意思上来讲就是采用工厂的方法来创建测试数据并配合完成测试 其主要应对的场景是:对于某 ...

  7. memcache启动多个服务

    windows 7 64bit 环境下安装memcached 1.下载后解压到D:\memcached(下载地址:memcached-win64下载地址) 2.安装到windows服务,打开cmd命令 ...

  8. Rock Pi开发笔记(二):入手Rock Pi 4B plus(基于瑞星微RK3399)板子并制作系统运行

    前言   入手rock pi 4b plus板子,查看具体的实物,然后制作系统,刷系统,让摇滚派的ubuntu系统运行起来.   Rock Pi 4B plus 介绍   ROCK Pi 4 是 Ra ...

  9. 如何在 Xamarin 中快速集成 Android 版认证服务 - 邮箱地址篇

    Xamarin 作为微软提供的移动服务多系统开发平台,成为很多开发者首选的应用开发平台.AppGallery Connect(以下简称 AGC)也在逐步的支持 Xamarin 的 SDK.认证服务也是 ...

  10. Solution -「多校联训」神

    \(\mathcal{Description}\)   Link.   给定 \(n\) 阶排列 \(a\),\(q\) 次询问,每次给出 \(1\le l_1\le r_1<l_2\le r_ ...