cs61b学习 part3

如果你有许多list,这里将会是大量的时间,我指的是对于单向链表查找时间复杂度O(N)相对于数组O(1)的时间复杂度会慢一些

 

所以这究竟是顺序表的编写还是链表的改进?

IntList

public class IntList {public int first;public IntList rest;public IntList(int f, IntList r) {first = f;rest = r;}/** Return the size of the list using... recursion! */public int size() {if (this.rest == null) {/* base case */return 1;}return 1 + this.rest.size();}/** Return the size of the list using no recursion! */public int iterativeSize() {/* You can not assign "this" in Java*/IntList p = this;int totalSize = 0;while (p != null) {totalSize += 1;p = p.rest;}return totalSize;}/** Returns the ith value in this list.*/public int get(int i) {IntList p = this;int nth = 0;if (i >= p.size()) {return -1;}while (nth != i) {p = p.rest;nth += 1;}return p.first;}public static void main(String[] args) {IntList L = new IntList(15, null);L = new IntList(10, L);L = new IntList(5, L);System.out.println(L.size());System.out.println(L.iterativeSize());System.out.println(L.get(2));}
} 

SLList 

/** An SLList is a list of integers, which hides the terrible truth* of the nakedness within. */
public class SLList {	private static class IntNode {public int item;public IntNode next;public IntNode(int i, IntNode n) {item = i;next = n;System.out.println(size);}} /* The first item (if it exists) is at sentinel.next. */private IntNode sentinel;private int size;private static void lectureQuestion() {SLList L = new SLList();IntNode n = IntNode(5, null);}/** Creates an empty SLList. */public SLList() {sentinel = new IntNode(63, null);size = 0;}public SLList(int x) {sentinel = new IntNode(63, null);sentinel.next = new IntNode(x, null);size = 1;}/** Adds x to the front of the list. */public void addFirst(int x) {sentinel.next = new IntNode(x, sentinel.next);size = size + 1;}/** Returns the first item in the list. */public int getFirst() {return sentinel.next.item;}/** Adds x to the end of the list. */public void addLast(int x) {size = size + 1; 		IntNode p = sentinel;/* Advance p to the end of the list. */while (p.next != null) {p = p.next;}p.next = new IntNode(x, null);}/** Returns the size of the list. */public int size() {return size;}public static void main(String[] args) {/* Creates a list of one integer, namely 10 */SLList L = new SLList();L.addLast(20);System.out.println(L.size());}
}

3 DLList(LinkedListDeque链表双边队列)


/*** Created by JianjunChen on 08/16/2020* This is a Linked List Doubly ended queue Data Structure using Lists!* @Rule The amount of memory that your program uses at any given time must be* proportional to the number of items.* @Rule Do not maintain references to items that are no longer in the deque.* @Rule Implement all the methods listed above in “The Deque API” section.*/public class LinkedListDeque<T> {/**LinkedNode Nested Class*/private class LinkedNode {private LinkedNode prev; /* Doubly Linked List*/private T item;private LinkedNode next;public LinkedNode(LinkedNode p, T i, LinkedNode n) {prev = p;item = i;next = n;}}private LinkedNode sentinel;//private LinkedNode last;private int size;/** Constructor of LinkedListDeque */public LinkedListDeque() {sentinel = new LinkedNode(null, null, null);sentinel.prev = sentinel;sentinel.next = sentinel;//last = sentinel;size = 0;}/** Adds an item of type T to the front of the deque.*/public void addFirst(T item) {LinkedNode first = new LinkedNode(sentinel, item, sentinel.next);/* Note that the order cannot be reversed since if we firstly write* sentinel.next = first; the sentinel.next.prev will equal to first.prev!!!!*/sentinel.next.prev = first;sentinel.next = first;size += 1;}/** Adds an item of type T to the back of the deque. */public void addLast(T item) {LinkedNode last = new LinkedNode(sentinel.prev, item, sentinel);/* Note that the order cannot be reversed!!! */sentinel.prev.next = last;sentinel.prev = last;size += 1;}/** Returns true if deque is empty, false otherwise. */public boolean isEmpty() {if (sentinel.next == sentinel) {return true;}return false;}/** Returns the number of items in the deque. */public int size() {return size;}/* Prints the items in the deque from first to last, separated by a space. */public void printDeque() {LinkedNode p = sentinel;while (p.next != sentinel) {System.out.print(p.next.item + " ");p = p.next;}}/** Removes and returns the item at the front of the deque.If no such item exists, returns null.*/public T removeFirst() {if (isEmpty()) {return null;}T firstItem = sentinel.next.item;/* Note that the order cannot be reversed!!!*/sentinel.next.next.prev = sentinel;sentinel.next = sentinel.next.next;size -= 1;return firstItem;}/** Removes and returns the item at the back of the deque.* If no such item exists, returns null. */public T removeLast() {if (isEmpty()) {return null;}T lastItem = sentinel.prev.item;/* Note that the order cannot be reversed!!! */sentinel.prev.prev.next = sentinel;sentinel.prev = sentinel.prev.prev;size -= 1;return lastItem;}/** Gets the item at the given index, where 0 is the front, 1 is the next item,* and so forth. If no such item exists, returns null. Must not alter the deque! */public T get(int index) {if (index > size - 1) {return null;}LinkedNode p = sentinel.next;int i = 0;while (i != index) {p = p.next;i += 1;}return p.item;}/** Helper method for getRecursive */private T getRecursiveHelper(LinkedNode currentNode, int index) {if (index == 0) {return currentNode.item;}return getRecursiveHelper(currentNode.next, index - 1);}/** Same as get but using recursion!! */public T getRecursive(int index) {if (index > size - 1) {return null;}return getRecursiveHelper(sentinel.next, index);}
}

4 AList(ArrayDeque数组双边队列) 


/*** Created by JianjunChen on 08/18/2020* This is a Array based Doubly Ended Queue Data Structure!!* @Rule The starting size of your array should be 8.* @Rule Implement all the methods listed above in “The Deque API” section.* @Rule The amount of memory that at any given time must be proportional to the number* of items. For arrays of length 16 or more, your usage factor should always be at least 25%.**/// Circular ArrayDeque
public class ArrayDeque<T> {private int initialCapacity = 8; //initial array capacityprivate int capacity;  //current array capacityprivate int eFactor = 2; //expanding factorprivate double usageFactor;private int mCapacity = 16; // The minimum capacity for contractionprivate double mRatio = 0.25; //The minimum usage ratio before contractionprivate int cFactor = 2; //contraction factorprivate T[] items;private int size;private int nextFirst;private int nextLast;public ArrayDeque() {capacity = initialCapacity;items = (T[]) new Object[initialCapacity];size = 0;nextFirst = 4;nextLast = 5;}/** Finding the next nextFirst and nextLast index in circle ArrayDeque. */private int oneMinus(int index) {if (index == 0) { // whether the index is out of bounds!index = capacity - 1;} else {index -= 1;}return index;}private int onePlus(int index) {if (index == capacity - 1) { // whether the index is out of bounds!index = 0;} else {index += 1;}return index;}/** Resize the original array to a new array with given capacity. */private void resize(int newCapacity) {T[] a = (T[]) new Object[newCapacity];int currentFirst = onePlus(nextFirst);int currentLast = oneMinus(nextLast);if (currentFirst < currentLast) {int length = currentLast - currentFirst + 1;System.arraycopy(items, currentFirst, a, 0, length);nextFirst = newCapacity - 1;nextLast = length;} else {int firstRightCount = capacity - currentFirst;int firstLeftCount = capacity - firstRightCount;System.arraycopy(items, currentFirst, a, 0, firstRightCount);System.arraycopy(items, 0, a, firstRightCount, firstLeftCount);nextFirst = newCapacity - 1;nextLast = capacity;}capacity = newCapacity;items = a;}/** Adds an item of type T to the front of the deque.* @Rule Must take constant time, except during resizing operations.*/public void addFirst(T item) {items[nextFirst] = item;size += 1;nextFirst = oneMinus(nextFirst);//The array is full, needed resize operation!if (size == capacity) {resize(size * eFactor);}}/** Adds an item of type T to the back of the deque.* @Rule Must take constant time, except during resizing operations.*/public void addLast(T item) {items[nextLast] = item;size += 1;nextLast = onePlus(nextLast);//The array is full, needed resize operation!if (size == capacity) {resize(size * eFactor);/*此处原为+1*/}}/** Returns true if deque is empty, false otherwise. */public boolean isEmpty() {if (size == 0) {return true;}return false;}/** Returns the number of items in the deque. */public int size() {return size;}/** Prints the items in the deque from first to last, separated by a space. */public void printDeque() {if (isEmpty()) {return;}int index = onePlus(nextFirst);while (index != nextLast) {System.out.print(items[index] + " ");index = onePlus(index);}}private void contract() {usageFactor = (double) size / capacity;if (usageFactor <= mRatio && capacity >= mCapacity) {int newCapacity = capacity / cFactor;resize(newCapacity);}}/** Removes and returns the item at the back of the deque. If no such item exists, returns null.* @Rule must take constant time, except during resizing operations.*/public T removeFirst() {if (isEmpty()) {return null;}int currentFirst = onePlus(nextFirst);T currentFirstItem = items[currentFirst];nextFirst = currentFirst;items[nextFirst] = null;size -= 1;contract();return currentFirstItem;}/** Removes and returns the item at the back of the deque. If no such item* exists, returns null..* @Rule must take constant time, except during resizing operations.*/public T removeLast() {if (isEmpty()) {return null;}int currentLast = oneMinus(nextLast);T currentLastItem = items[currentLast];nextLast = currentLast;items[nextLast] = null;size -= 1;return currentLastItem;}/*** Gets the item at the given index, where 0 is the front, 1 is the next item, and so forth.* If no such item exists, returns null. Must not alter the deque!* @Rule must take constant time.*/public T get(int index) {if (index >= size) {return null;}int indexFromFirst = nextFirst + 1 + index;if (indexFromFirst >= capacity) {indexFromFirst -= capacity;}return items[indexFromFirst];}
}

C、203

 100+101+....+999+1000=495550,大概500000

这张图片展示SLList代表Single-Linked List(单向链表),而ALList代表Array-Backed List(数组支持链表)的区别,即O(N)和O(NlogN)存疑

为了让alist更加通用实现了如下变化 

/* Array based list.
@author Josh Hug*/
//        0  1  2  3  4  5  6  7
//items:[ 6  9  -1 2  0  0  0  0]
//size:5
/*Invariants:addlast:The next item we want to add,will go into position sizegetlast:The item we want to return is in position size -1size:The number of items in the list should be size.
*/
public class AList<Item> {private Item[] items;private int size;/*** Creates an empty list.*/public AList() {items = (Item[]) new Object[100];size = 0;}/*Resizes the underlying array to the target capacity.*/private void resize(int capacity) {Item[] a = (Item[]) new object[capacity];System.arraycopy(items, 0, a, 0, size);items = a;}/*Inserts x into the back of the list.*/public void addLast(Item x) {if (size == items.length) {resize(size + 1);}items[size] = x;size = size + 1;}/*Returns the item from the back of the List.*/public Item getlast() {return items[size - 1];}/*Gets the ith item in the list (0 is the front).*/public Item get(int i) {return items[i];}/*Returns the number of items in the list.*/public int size() {return size;}/*Deletes item from back of the list and returns deleted item.*/public Item removeLast() {Item x = getLast();items[size - 1] = null;size = size - 1;return x;}
}

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/443207.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

后端增删改查的基本应用——一个简单的货物管理系统

最终效果&#xff0c;如图所示&#xff1a; 如果想要进行修改操作&#xff0c;可点击某栏修改选项&#xff0c;会在本表格下方弹出修改的具体操作界面&#xff08;点击前隐藏&#xff09;&#xff0c;并且目前的信息可复现在修改框内。 本篇文章通过该项目将后端和前端结合起来…

编译链接的过程发生了什么?

一&#xff1a;程序的翻译环境和执行环境 在 ANSI C 的任何一种实现中&#xff0c;存在两个不同的环境。 第 1 种是翻译环境&#xff0c;在这个环境中源代码被转换为可执行的机器指令。 第 2 种是执行环境&#xff0c;它用于实际执行代码 也就是说&#xff1a;↓ 1&#xff1…

微信小程序启动不起来,报错凡是以~/包名/*.js路径的文件,都找不到,试过网上一切方法,最终居然这么解决的,【避坑】命运的齿轮开始转动

app.json "resolveAlias": {"~/*": "/*"},文件代码也没有问题&#xff0c;网上的方法试过来了&#xff0c;大模型AI也问过遍&#xff0c;熬夜到凌晨2点半&#xff0c;最不可思议的是居然是因为微信开发者工具版本的问题&#xff0c;我真的是笑死…

网站排名,让网站快速有排名的几个方法

要让网站快速获得并提升排名&#xff0c;需要综合运用一系列专业策略和技术&#xff0c;这些策略涵盖了内容优化、技术调整、外链建设、用户体验提升等多个方面。以下是让网站快速有排名的几个方法&#xff1a; 1.内容为王&#xff1a;创造高质量、有价值的内容 -深入…

南京大学《软件分析》李越, 谭添——1. 导论

导论 主要概念: soundcompletePL领域概述 动手学习 本节无 文章目录 导论1. PL(Programming Language) 程序设计语言1.1 程序设计语言的三大研究方向1.2 与静态分析相关方向的介绍与对比静态程序分析动态软件测试形式化(formal)语义验证(verification) 2. 静态分析:2.1莱斯…

Redis数据库与GO(一):安装,string,hash

安装包地址&#xff1a;https://github.com/tporadowski/redis/releases 建议下载zip版本&#xff0c;解压即可使用。解压后&#xff0c;依次打开目录下的redis-server.exe和redis-cli.exe&#xff0c;redis-cli.exe用于输入指令。 一、基本结构 如图&#xff0c;redis对外有个…

k8s的安装和部署

配置三台主机&#xff0c;分别禁用各个主机上的swap&#xff0c;并配置解析 systemctl mask swap.target swapoff -a vim /etc/fstab配置这三个主机上的主机以及harbor仓库的主机 所有主机设置docker的资源管理模式为system [rootk8s-master ~]# vim /etc/docker/daemon.json…

为什么推荐你一定要弄懂千门八将108局,学会做局思维的人有多么的厉害?

在纷繁复杂的社会与商业环境中&#xff0c;能够洞悉事物本质、预见趋势并巧妙布局的人&#xff0c;往往能在竞争中脱颖而出&#xff0c;成为时代的弄潮儿。而“千门八将108局”这一古老而深邃的智慧体系&#xff0c;不仅蕴含了中国传统文化中对于策略、心理学、人际交往的深刻理…

PCL 提取点云边界

目录 一、概述 1.1原理 1.2实现步骤 1.3应用场景 二、代码实现 2.1关键函数 2.1.1 计算法向量 2.1.2 提取边界点 2.1.3 可视化边界点 2.2完整代码 三、实现效果 PCL点云算法汇总及实战案例汇总的目录地址链接&#xff1a; PCL点云算法与项目实战案例汇总&#xff0…

动手学深度学习(李沐)PyTorch 第 6 章 卷积神经网络

李宏毅-卷积神经网络CNN 如果使用全连接层&#xff1a;第一层的weight就有3*10^7个 观察 1&#xff1a;检测模式不需要整张图像 很多重要的pattern只要看小范围即可 简化1&#xff1a;感受野 根据观察1 可以做第1个简化&#xff0c;卷积神经网络会设定一个区域&#xff0c…

SolarWinds中如何添加华为交换机实现网络管理

号主&#xff1a;老杨丨11年资深网络工程师&#xff0c;更多网工提升干货&#xff0c;请关注公众号&#xff1a;网络工程师俱乐部 下午好&#xff0c;我的网工朋友。 SolarWinds作为一款广受好评的网络管理软件&#xff0c;它提供了全面的网络配置、监控和管理解决方案&#x…

组织病理学图像中的再识别|文献速递--基于多模态-半监督深度学习的病理学诊断与病灶分割

Title 题目 Re-identification from histopathology images 组织病理学图像中的再识别 01 文献速递介绍 在光学显微镜下评估苏木精-伊红&#xff08;H&E&#xff09;染色切片是肿瘤病理诊断中的标准程序。随着全片扫描仪的出现&#xff0c;玻片切片可以被数字化为所谓…

【Spring】“请求“ 之传递单个参数、传递多个参数和传递对象

文章目录 请求1. 传递单个参数注意事项1 . 正常传递参数2 . 不传递 age 参数3 . 传递参数类型不匹配 2. 传递多个参数3. 传递对象 请求 访问不同的路径&#xff0c;就是发送不同的请求。在发送请求时&#xff0c;可能会带一些参数&#xff0c;所以学习 Spring 的请求&#xff…

【093】基于SpringBoot+Vue实现的精品水果线上销售系统

系统介绍 视频演示 基于SpringBootVue实现的精品水果线上销售系统&#xff08;有文档&#xff09; 基于SpringBootVue实现的精品水果线上销售系统采用前后端分离的架构方式&#xff0c;系统设计了管理员、商家、用户三种角色&#xff0c;实现了公告类型管理、商家信誉类型管理…

自由学习记录

约束的泛型通配符? Java中的泛型 xiaomi和byd都继承了car&#xff0c;但是只是这两个类是car的子类而已&#xff0c;而arraylist<xiaomi> ,arraylist<byd> 两个没有半毛钱继承关系 所以传入的参数整体&#xff0c;是car的list变形&#xff0c;里面的确都能存car…

SDK4(note下)

以下代码涉及到了很多消息的处理&#xff0c;有些部分注释掉了&#xff0c;主要看代码 #include <windows.h> #include<tchar.h> #include <stdio.h> #include <strsafe.h> #include <string> #define IDM_OPEN 102 /*鼠标消息 * 键盘消息 * On…

数据湖数据仓库数据集市数据清理以及DataOps

一提到大数据我们就知道是海量数据&#xff0c;但是我们并不了解需要从哪些维度去考虑这些数据的存储。比如 数据湖、数据仓库、数据集市&#xff0c;以及数据自动化应用DataOps有哪些实现方式和实际应用&#xff0c;这篇文章将浅显的做一次介绍。 数据湖 数据湖是一种以自然…

SimpleFoc以及SVPWM学习补充记录

SimpleFoc SimpleFOC移植STM32&#xff08;一&#xff09;—— 简介 FOC控制的过程是这样的&#xff1a; 对电机三相电流进行采样得到 Ia,Ib,Ic。将 Ia,Ib,Ic 经过Clark变换得到 I_alpha I_beta。将 I_alpha I_beta 经过Park变换得到 Id,Iq。计算 Id,Iq 和其设定值 Id_ref 和…

网络知识点之—EVPN

EVPN&#xff08;Ethernet Virtual Private Network&#xff09;是下一代全业务承载的VPN解决方案。EVPN统一了各种VPN业务的控制面&#xff0c;利用BGP扩展协议来传递二层或三层的可达性信息&#xff0c;实现了转发面和控制面的分离。 EVPN解决传统L2VPN的无法实现负载分担、…

《神经网络》—— 长短期记忆网络(Long Short-Term Memory,LSTM)

文章目录 一、LSTM的简单介绍二、 LSTM的核心组件三、 LSTM的优势四、 应用场景 一、LSTM的简单介绍 传统RNN循环神经网络的局限&#xff1a; 示例&#xff1a;当出现“我的职业是程序员。。。。。。我最擅长的是电脑”。当需要预测最后的词“电脑”。当前的信息建议下一个词可…