得分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. JS let, var, const的用法以及区别

    本文摘自多位前辈的博文,另外还有一些我的多余补充,摘自地址已补充.非常感谢各位前辈.仅以笔记学习为目的! 深入学习ES6的知识还请访问阮一峰老师的ES6教程 如果不使用let或者const,在JS只有 ...

  2. java命令-(学习)jstack 工具

    一.介绍 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...

  3. 对战平台虚拟War3局域网的原理对战平台虚拟War3局域网的原理

    转载请注明来源:https://www.cnblogs.com/hookjc/ 以War3为例,启动魔兽后,首先是如何看见主机的问题:魔兽是通过TCP/UDP协议进行数据发送的,那如何实现看到对方?我 ...

  4. 认识BufferedReader的readLine、ready,以及InputStream的available

    最近,同学做实验的时候,在读取服务器端返回的时候,使用了BufferedReader类的readLine,他是这么写的,while(reader.ready()) {//执行读取操作,即readLin ...

  5. 把 Navigation Bar 下面那条线删掉的最简单的办法! — By: 昉

    系统默认的 Navigation Bar 下面一直有条线,翻尽了文档却没找到能把它弄走的相关接口,处女座的简直木法忍啊有木有!!!! 研究了一下navigationBar下的子视图,原来只需要几行代码 ...

  6. Ext原码学习之lang-Array.js

    // JavaScript Document //Array 方法 (function(){ var arrayPrototype = Array.prototype, slice = arrayPr ...

  7. LAMP以及各组件的编译安装

    LAMP以及各组件的编译安装 目录 LAMP以及各组件的编译安装 一.LAMP 1. LAMP概述 2. 各组件的主要作用 3. 平台环境的安装顺序 二.编译安装apache httpd 1. 关闭防 ...

  8. 给 zsh 自定义命令添加参数自动补全

    有时我会自定义一些 zsh 命令,以便提升某些高频操作的效率.本文记录我给一个自定义命令添加参数自动补全的方法. 场景 我自定义了一个 zsh 命令 gmt,执行 gmt <b2>,可以将 ...

  9. 带你十天轻松搞定 Go 微服务系列(九、链路追踪)

    序言 我们通过一个系列文章跟大家详细展示一个 go-zero 微服务示例,整个系列分十篇文章,目录结构如下: 环境搭建 服务拆分 用户服务 产品服务 订单服务 支付服务 RPC 服务 Auth 验证 ...

  10. 模块random+os+sys+json+subprocess

    模块random+os+sys+json+subprocess 1. random 模块   (产生一个随机值) import random 1 # 随机小数 2 print(random.rando ...