标签:index 专题 return int 链表 length 数据结构 data public
简介
再编程中最主要的是算法思想,而且算法又是基于数据结构来实现的,而我们知道在物理内存存储结构中只有顺序表和链表两种形式,所在所有的数据结构中都是以顺序表和链表为基础进行演变或者映射,为了减少学习成本熟练掌握顺序表和链表是非常有必要的,这是我们学习树、图等复杂的数据结构奠定良好的基础。
顺序表
/**
* 顺序表
* @Author:TangJiachang
* */
class MyArraysList<T> {
/** 存储数据的数组 */
private Object[] data;
/** 线性表真实存储长度 */
private int length;
/**
* 默认长度为10
* */
public MyArraysList() {
this.data = new Object[10];
}
/**
* 指定长度
* */
public MyArraysList(int length) {
this.data = new Object[length];
}
/**
* 获取真实存储长度
* */
public int size() {
return this.length;
}
/**
* 添加元素
* */
public void add(T data) {
if (this.length == this.data.length) {
Arrays.copyOf(this.data, 2*this.data.length);
}
this.data[this.length] = data;
this.length++;
}
/**
* 在指定位置添加数组
* */
public void add(int index, T data) {
if (index == this.length) {
this.add(data);
return;
} else {
if (index < 0 | index > this.length) {
try {
throw new MyArraysListExcetion("索引不合法");
} catch (MyArraysListExcetion myArraysListExcetion) {
myArraysListExcetion.printStackTrace();
}
return;
}
if (this.data.length == this.length) {
Arrays.copyOf(this.data, 2*this.data.length);
}
// index索引后的数组往后挪一位
for (int i = this.length - 1; i >= index; i--) {
this.data[i + 1] = this.data[i];
}
this.data[index] = data;
this.length++;
}
}
/**
* 在指定索引替换数据
* */
public void setIndex(int index, T data) {
if (index < 0 | index > this.length) {
try {
throw new MyArraysListExcetion("索引不合法");
} catch (MyArraysListExcetion myArraysListExcetion) {
myArraysListExcetion.printStackTrace();
}
return ;
}
this.data[index] = data;
}
/**
* 获取指定数据的索引
* */
public int getIndex(T data) {
for (int i = 0; i < this.length; i++) {
if (this.data[i].equals(data)) {
return i;
}
}
return -1;
}
/**
* 获取指定索引上的数据
* */
public Object getData(int index) {
if (index < 0 | index > this.length) {
try {
throw new MyArraysListExcetion("索引不合法");
} catch (MyArraysListExcetion myArraysListExcetion) {
myArraysListExcetion.printStackTrace();
}
return -1;
}
return this.data[index];
}
/**
* 是否包含指定数据
* */
public boolean contains(T data) {
for (int i = 0; i < this.length; i++) {
if (this.data[i].equals(data)) { return true; }
}
return false;
}
/**
* 搜索指定数据返回下标
* */
public int search(T data) {
if (this.contains(data) == false) {
return -1;
}
for (int i = 0; i < this.length; i++) {
if (this.data[i].equals(data)) {
return i;
}
}
return -1;
}
/**
* 指定索引进行移除
* */
public void remove(int index) {
if (index < 0 | index > this.length) {
try {
throw new MyArraysListExcetion("索引不合法");
} catch (MyArraysListExcetion myArraysListExcetion) {
myArraysListExcetion.printStackTrace();
}
return;
}
// 所有数据往前挪一位
for (int i = index; i < this.length; i++) {
this.data[i] = this.data[i + 1];
}
this.length--;
return;
}
/**
* 清空顺序表
* */
public void clear() {
for (int i = this.length; i > 0; i--) {
this.data[i-1] = this.data[i];
}
}
//陈列顺序表中所有的元素
public void print(){
for (int i = 0; i < this.length; i++){
System.out.print(" " + this.data[i]);
}
System.out.println();
}
}
/**
* 顺序表异常类
* */
class MyArraysListExcetion extends Exception {
public MyArraysListExcetion(String mgs) {
super(mgs);
}
}
链表
/**
* @ClassName: SuperClass
* @Author: Tang
* @Date: 2021/10/27 19:13
*/
public class SuperClass {
public static void main(String[] args) {
ListNode<String> list = new ListNode<>();
list.add("a");
list.add("b");
list.add("c");
System.out.println(list.contains("a"));
System.out.println(list.contains("e"));
System.out.println("替换前: ");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println("替换后(根据索引): ");
list.replace(0, "E");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println("替换后(根据数据): ");
list.replace("b", "替换b");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
}
}
/**
* 链表
* @Author:TangJiachang
* */
class ListNode<T> {
/** 存储链表的长度 */
private int count;
/** 存储根节点的索引 */
private int foot;
/** 存储根节点 */
private Node root;
public ListNode() {}
/**
* 添加数据
* */
public void add(T data) {
if (this.isEmpty()) {
this.root = new Node(data);
} else {
this.root.add(data);
}
this.count++;
}
/**
* 获取数据
* */
public T get(int index) {
if (this.isEmpty()) {
return null;
} else {
this.foot = 0;
return this.root.get(index);
}
}
/**
* 根据索引移除数据
* */
public void remove(int index) {
if (index < 0 | index > this.count | this.isEmpty()) {
return ;
} else {
if (index == 0) {
Node temp = this.root;
this.root = this.root.next;
temp.next = null;
this.count--;
return;
} else {
this.root.remove(this.root, index);
}
}
}
/**
* 根据数据移除
* */
public void remove(T data) {
if (this.isEmpty()) {
return;
} else {
if (this.root.data.equals(data)) {
Node temp = this.root;
this.root = this.root.next;
this.count--;
return;
} else {
this.root.remove(this.root, data);
}
}
}
/**
* 判断链表算法为空
* */
public boolean isEmpty() {
if (this.count == 0 | this.root == null) {
return true;
} else {
return false;
}
}
/**
* 获取链表的长度
* */
public int size() {
return this.count;
}
/**
* 根据索引进行替换
* */
public void replace(int index, T data) {
if (this.isEmpty()) {
return;
} else {
if (index < 0 | index >= count ) {
return;
} else {
this.foot = 0;
this.root.replace(index, data);
}
}
}
/**
* 根据数据进行替换
* */
public void replace(T oldData, T newData) {
if (this.isEmpty()) {
return;
} else {
this.root.replace(oldData, newData);
}
}
/**
* 判断链表算法保护某数据
* */
public boolean contains(T data) {
if (this.isEmpty()) {
return false;
} else {
return this.root.contains(data);
}
}
private class Node {
/** 存储下一个节点的指针 */
private Node next;
/** 存储节点数据 */
private T data;
public Node(T data) {
this.data = data;
}
public void add(T data) {
if (this.next == null) {
this.next = new Node(data);
} else {
this.next.add(data);
}
}
public T get(int index) {
if (ListNode.this.foot++ == index) {
return this.data;
} else {
return this.next.get(index);
}
}
public void remove(Node preNode, int index) {
if (ListNode.this.foot++ == index) {
preNode.next = this.next;
this.next = null;
ListNode.this.count--;
return;
} else {
this.next.remove(this, index);
}
}
public void remove(Node preNode, T data) {
if (this.data.equals(data)) {
preNode.next = this.next;
this.next = null;
ListNode.this.count--;
return;
} else {
if (this.next !=null) {
this.next.remove(this, data);
} else {
return;
}
}
}
public void replace(T oldData, T newData) {
if (this.data.equals(oldData)) {
this.data = newData;
} else {
this.next.replace(oldData, newData);
}
}
public void replace(int index, T newData) {
if (ListNode.this.foot++ == index) {
this.data = newData;
} else {
this.next.replace(index, newData);
}
}
public boolean contains(T data) {
if (this.data.equals(data)) {
return true;
} else {
if (this.next == null) {
return false;
} else {
return this.next.contains(data);
}
}
}
}
}
标签:index,专题,return,int,链表,length,数据结构,data,public 来源: https://blog.csdn.net/weixin_43730516/article/details/121453839
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。