CS61b proj1a
得分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的更多相关文章
- CS61B sp2018笔记 | Lists
Lists csdn同作者原创地址 1. IntLists 下面我们来一步一步的实现List类,首先你可以实现一个最简单的版本: public class IntList { public int ...
- CS61B HW0
The Enhanced For Loop public class EnhancedForBreakDemo { public static void main(String[] args) { S ...
- Java学习资源整理(超级全面)
这里整理一些自己平常搜集的比较好的关于Java的学习资源,主要包括博客站点.书籍.课程等. 了解Java最新资讯 这部分主要是了解与Java相关的动态以及信息,能够拓展我们的视野以及寻找一些好的ide ...
- Java中的Iterable与Iterator详解
在Java中,我们可以对List集合进行如下几种方式的遍历: List<Integer> list = new ArrayList<>(); list.add(5); list ...
- CS61A hw01
前不久在知乎上看到CS61A 和CS61B spring18 开课的消息.上去看了一眼,发现真的不错,所有proj hw都可以本地测试!也就是说除了没有课程成绩和官方讨论区和TA解答外,这个课完全可以 ...
- 选择排序的实现以及如何编写测试 #CS61B-sp18-3.1
Selection Sort的思想: 就是在一系列数字中先找到一个最小的放在所有数字的第一个位置上,然后再从余下的数字里面找最小个放在余下的数字里的第一个位置上. 例如: 在这段数据里面我们找到最小的 ...
- AList的具体实现 #CS61B-sp18-2.5
实现一个Array based list,其功能包括获取长度size,添加元素至最后addLast,得到元素get和去除最后一个元素. 设计思路及其实现: 我们都知道在获取数据的时候,直接调用缓存里面 ...
- proj0的具体实现 #CS61B-sp18
https://github.com/Centurybbx/sp18-century/tree/master/proj0 proj0的具体实现在上面的Github中. 在proj0中我明显感受到国外大 ...
- ARTS第十二周
1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有观点和思考的技术文章 以下是 ...
随机推荐
- 消息队列 - mac上安装RabbitMq (转)
什么是RabbitMQ? RabbitMQ是由Erlang语言编写的实现了高级消息队列协议(AMQP)的开源消息代理软件(也称为面向消息的中间件).支持WIndows.Linux.MAC OS 操作系 ...
- CSS3自定义滚动条样式-webkit内核
自定义滚动条实现 此部分针对webkit内核的浏览器,使用伪类来改变滚动条的默认样式,详情如下: 滚动条组成部分 1. ::-webkit-scrollbar 滚动条整体部分 2. ::-webkit ...
- matlab构建栅格地图绘图思路
matlab构建栅格地图绘图思路 近来因研究需要,调研并思考了栅格地图的生成方法,暂时总结以备不时之需. 栅格的建立最需要注意栅格粒度的问题,即根据需要调整栅格的边长,目前有两种思路一种是固定栅格边长 ...
- 利用LNMP实现wordpress站点搭建
一.部署MySQL 1.1 二进制安装mysql5.6 # 准备用户,依赖包,二进制程序 [root@nginx ~]# yum install -y libaio perl-Data-Dumper ...
- 经纬坐标(BLH)数据创建.kml文件小工具设计 Java版
技术背景 KML,是标记语言(Keyhole Markup Language)的缩写,最初由Keyhole公司开发,是一种基于XML 语法与格式的.用于描述和保存地理信息(如点.线.图像.多边形和模型 ...
- LRU缓存及实现
一.淘汰策略 缓存:缓存作为一种平衡高速设备与低速设备读写速度之间差异而引入的中间层,利用的是局部性原理.比如一条数据在刚被访问过只有就很可能再次被访问到,因此将其暂存到内存中的缓存中,下次访问不用读 ...
- Dubbo基础一之实战初体验
本以为写这个小作文没什么难度的,可是好像并不是.前段时间重心放在驾考科目二,就想着小作文科二考过了再写也不是事,因为都实战过了.今天想着写却发现脑袋里啥都想不起来了,得翻项目和笔记回忆一下.所以还是那 ...
- JAVA8学习——Stream底层的实现二(学习过程)
继续深入Stream的底层实现过程 2.spliterator() 接上 https://www.cnblogs.com/bigbaby/p/12159495.html 我们这次回到最开始源码分析的地 ...
- Session、Session共享、Token演变
巨人的肩膀 深夜,我偷听到程序员要对session下手-- (qq.com)
- jenkins pipeline构建项目
以前用的jenkins自由风格发布代码.界面丑陋,出现问题位置不够清晰.今天改进一下流程使用jenkins pipeline构建项目. 学习使我快乐 步骤一.安装pipeline插件 点击系统管理-& ...