数据结构(Java)-持续更新补充
复习一下数据结构,巩固一下基础。
之后打算再学一下算法,之前刷题总感觉摸不清门道,应该是概念没彻底搞明白。
栈
import java.util.Arrays;
public class Stack {
private int size;
private int[] array;
public Stack() {
this(10);
}
public Stack(int init) {
if(init <= 0) {
init = 10;
}
array = new int[init];
}
public void push(int item) {
if(size == array.length) {
array = Arrays.copyOf(array, size * 2);
}
array[size++] = item;
}
public int peek() {
if(size == 0) {
throw new ArrayIndexOutOfBoundsException("栈已经空啦");
}
return array[size - 1];
}
public int pop() {
int item = peek();
size--;
return item;
}
public boolean isFull() {
return size == array.length;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
}
队列
public class ArrayQueue {
private final Object[] items;
private int head;
private int tail;
public ArrayQueue(int capacity) {
this.items = new Object[capacity];
}
public boolean put(Object item) {
if(head == (tail + 1) % items.length) {
return false;
}
items[tail] = item;
tail = (tail + 1) % items.length;
return true;
}
public Object peek() {
if(head == tail) {
return null;
}
return items[head];
}
public Object poll() {
if(head == tail) {
return null;
}
Object item = items[head];
items[head] = null;
head = (head + 1) % items.length;
return item;
}
public boolean isFull() {
return head == (tail + 1) % items.length;
}
public boolean isEmpty() {
return head == tail;
}
public int size() {
if(head <= tail) {
return tail - head;
}
return tail - head + items.length;
}
}
用栈来模拟队列
public class Stack2Queue {
private Stack stack1;
private Stack stack2;
private int maxLength;
public Stack2Queue(int capacity) {
maxLength = capacity;
stack1 = new Stack(capacity);
stack2 = new Stack(capacity);
}
public boolean put(int item) {
if(stack1.isFull() || size() == maxLength) {
return false;
}
stack1.push(item);
return true;
}
public int poll() {
if(!stack2.isEmpty()) {
return stack2.pop();
}else {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.pop();
}
}
public int size() {
return stack1.size() + stack2.size();
}
}
用队列来模拟栈
public class Queue2Stack {
private ArrayQueue queue1;
private ArrayQueue queue2;
private int maxLength;
public Queue2Stack(int capacity) {
maxLength = capacity;
queue1 = new ArrayQueue(capacity);
queue2 = new ArrayQueue(capacity);
}
public boolean push(Object item) {
if(size() == maxLength) {
return false;
}
if(queue2.isEmpty()) {
queue1.put(item);
}else {
queue2.put(item);
}
return true;
}
public Object pop() {
if(size() == 0) {
// return null;
throw new IndexOutOfBoundsException("栈里空啦");
}
if(queue2.isEmpty()) {
while(queue1.size() > 1) {
queue2.put(queue1.poll());
}
return queue1.poll();
}else {
while(queue2.size() > 1) {
queue1.put(queue2.poll());
}
return queue2.poll();
}
}
public int size() {
return queue1.size() + queue2.size();
}
}
链表
public class Node {
private int data;
private Node next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public class Link {
private int size = 0;
private Node first;
private Node last;
public Link() {
}
public void addFirst(int data) {
if(size == 0) {
fillStart(data);
}else {
Node node = new Node();
node.setData(data);
node.setNext(first);
first = node;
size++;
}
}
public void addLast(int data) {
if(size == 0) {
fillStart(data);
}else {
Node node = new Node();
node.setData(data);
last.setNext(node);
last = node;
size++;
}
}
public void add(int data, int index) {
if(size > index) {
if(size == 0) {
fillStart(data);
}else if(index == 0) {
addFirst(data);
}else if (index == size -1) {
addLast(data);
}else {
Node node = new Node();
node = get(index - 1);
Node temp = new Node();
temp.setData(data);
temp.setNext(node.getNext());
node.setNext(temp);
size++;
}
}else {
throw new IndexOutOfBoundsException("链表没有这么长");
}
}
public void removeFirst() {
if(size == 0) {
throw new IndexOutOfBoundsException("链表中没有元素");
}else if(size == 1) {
clear();
}else {
Node node = first;
first = first.getNext();
node = null;
size--;
}
}
public void removeLast() {
if(size == 0) {
throw new IndexOutOfBoundsException("链表中没有元素");
}else if(size == 1) {
clear();
}else {
Node node = get(size -2);
node.setNext(null);
last = node;
size--;
}
}
public void removeMiddle(int index) {
if(size == 0) {
throw new IndexOutOfBoundsException("链表中没有元素");
}else if(size == 1) {
clear();
}else {
Node node = get(index - 1);
Node temp = node.getNext();
node.setNext(temp.getNext());
size--;
}
}
public int size() {
return size;
}
public void clear() {
first = null;
last = null;
size = 0;
}
public Node get(int index) {
Node temp = first;
for(int i = 0; i < index; i++) {
temp = temp.getNext();
}
return temp;
}
private void fillStart(int data) {
first = new Node();
first.setData(data);
last = first;
size++;
}
public void printAll() {
Node temp = first;
System.out.println(temp.getData());
for(int i = 0; i < size - 1; i++) {
temp = temp.getNext();
System.out.println(temp.getData());
}
}
}
暂时先复习了这么多,本文持续更新~
如果有啥觉得可以补充或者建议,可以留言噢~
参考:
- 《轻松学算法》赵烨

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Spring Boot开发时遇到的一系列问题及解决办法总结
问题一 Spring Boot扫描包提示找不到mapper的问题,异常信息内容: Considerdefiningabeanoftypeinyourconfiguration 分析原因:Spring Boot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描,“Application类”是指Spring Boot项目入口类。如果Application类所在的包为:com.yoodb.blog,则只会扫描com.yoodb.blog包及其所有子包,如果service或dao所在包不在com.yoodb.blog及其子包下,则不会被扫描。 解决方法: 方式一:使用注解@ComponentScan(value=”com.yoodb.blog”),其中,com.yoodb.blog为包路径。 方式二:将启动类Application放在上一级包中,注意的是Application启动类必须要保证在包的根目录下。 问题二 启动Spring Boot时,,抛出异常信息如下: YourApplicationContextisunlikelytostartduetoa@Com...
-
下一篇
前端性能优化总结
从用户访问资源到资源完整的展现在用户面前的过程中,通过技术手段和优化策略,缩短每个步骤的处理时间从而提升整个资源的访问和呈现速度。网站的性能直接会影响到用户的数量,所有前端性能优化很重要。 前端性能优化分为如下几个方面: 一、代码部署: 1、代码的压缩与合并 2、图片、js、css等静态资源使用和主站不同域名地址存储,从而使得在传输资源时不会带上不必要的cookie信息。 3、使用内容分发网络 CDN 4、为文件设置Last-Modified、Expires和Etag 5、使用GZIP压缩传送 6、权衡DNS查找次数(使用不同域名会增加DNS查找) 7、避免不必要的重定向(加"/") 二、编码 html: 1、使用结构清晰,简单,语义化标签 2、避免空的src和href 3、不要在HTML中缩放图片 css: 1、精简css选择器 2、把CSS放到顶部 3、避免@import方式引入样式 4、css中使用base64图片数据取代图片文件,减少请求数,在线转base64网站:http://tool.css-js.com/base64.html 5、使用css动画来取代javascript...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合Redis,开启缓存,提高访问速度
- 2048小游戏-低调大师作品
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2全家桶,快速入门学习开发网站教程
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2更换Tomcat为Jetty,小型站点的福音