package com.example.demo;
// 1、定义要保存对象的类:
class Phone {// 此类提供要保存的数据
private String brand;
private double price;
public Phone() {
}
public Phone(String brand, double price) {
this.brand = brand;
this.price = price;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void setPrice(double price) {
this.price = price;
}
public String getBrand() {
return brand;
}
public double getPrice() {
return price;
}
@Override
public boolean equals(Object obj) {// 对象比较
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Phone other = (Phone) obj;
if (brand == null) {
if (other.brand != null)
return false;
} else if (!brand.equals(other.brand))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
return true;
}
//重写hashCode
@Override
public int hashCode() {
return Integer.valueOf(brand.hashCode()+price*63+"");
}
@Override
public String toString() {
return "brand = " + this.brand + ", price = " + this.price;
}
}
//2、定义链表的操作标准:
interface ILink {// 定义链表操作标准
void add(Object obj);// 向链表中保存数据
int size();// 取得链表的长度
Object get(int index);// 根据索引取得对应的数据
boolean isEmpty();// 判断链表是否为空
boolean contains(Object obj);// 判断某一对象是否存在
void set(int index, Object obj);// 根据索引设置数据
void remove(Object obj);// 删除数据
void clean();// 清空链表
Object[] toArray();// 取得链表中的所有数据
}
//3、链表的具体实现:
class LinkImpl implements ILink {
// ****************************************************
private Node root;// 根节点
private int count;// 统计链表中的元素个数
private int foot;// 链表元素的下角标
private Object[] retArray;// 用于接收返回链表中的所有数据
private class Node {// 私有内部类,为外部类服务实现数据的增加等一系列的操作
private Object data;// 可以保存任意类型的数据
private Node next;// 指向下一个节点
public Node(Object data) {// 数据封装
this.data = data;
}
public void addNode(Node newNode) {
if (this.next == null) {
this.next = newNode;
} else {
this.next.addNode(newNode);
}
}
public Object getNode(int index) {
if (LinkImpl.this.foot++ == index) {
return this.data;
} else {
return this.next.getNode(index);
}
}
public boolean containsNode(Object obj) {// 需要对象比较
if (this.data.equals(obj)) {
return true;
} else {
if (this.next != null) {
return this.next.containsNode(obj);
}
return false;
}
}
public void setNode(int index, Object obj) {
if (LinkImpl.this.foot++ == index) {
this.data = obj;
} else {
this.next.setNode(index, obj);
}
}
public void removeNode(Node previous, Object obj) {// 需要对象比较
if (this.data.equals(obj)) {
previous.next = this.next;
} else {
this.next.removeNode(this, obj);
}
}
public void toArrayNode() {
LinkImpl.this.retArray[foot++] = this.data;
if (this.next != null) {
this.next.toArrayNode();
}
}
}
@Override
public void add(Object obj) {
if (obj == null) {// 要保存的数据为空,则结束调用
return;
}
Node newNode = new Node(obj);// 封装数据为一节点
if (this.root == null) {
this.root = newNode;
} else {
this.root.addNode(newNode);
}
this.count++;// 数据增加成功,count 自增
}
@Override
public int size() {
return this.count;
}
@Override
public Object get(int index) {
if (this.isEmpty()) {// 链表为空
return null;
}
if (index >= this.count) {
return null;
}
this.foot = 0;// 保证数据是从第一个开始遍历
return this.root.getNode(index);
}
@Override
public boolean isEmpty() {
return this.count == 0 && this.root == null;// 根节点不存在或者链表中的元素个数为0,表示链表为空链表
}
@Override
public boolean contains(Object obj) {
if (this.isEmpty()) {
return false;
}
return this.root.containsNode(obj);
}
@Override
public void set(int index, Object obj) {
if (this.isEmpty()) {
return;
}
if (index >= this.count) {
return;
}
this.foot = 0;
this.root.setNode(index, obj);
}
@Override
public void remove(Object obj) {
if (!this.contains(obj)) {
return;
}
if (this.root.data.equals(obj)) {
this.root = this.root.next;
} else {
this.root.next.removeNode(this.root, obj);
}
this.count--;// 数据删除成功,count 自减
}
@Override
public void clean() {
this.count = 0;
this.root = null;
}
@Override
public Object[] toArray() {
if (this.isEmpty()) {
return null;
}
this.foot = 0;
this.retArray = new Object[this.count];
this.root.toArrayNode();
return this.retArray;
}
}
//4、编写测试数据:
public class TestDemo {
public static void main(String[] args) {
ILink all = new LinkImpl();// 通过向上转型实例化链表
System.out.println(all.size());
System.out.println(all.isEmpty());
all.add(new Phone("大米手机", 1999.0));
all.add(new Phone("白米手机", 999.0));
all.add(new Phone("黑族手机", 1999.0));
System.out.println(all.isEmpty());
System.out.println(all.size());
System.out.println(all.get(1));
System.out.println(all.contains(new Phone("黑族手机", 1999.0)));
all.set(2, new Phone("白族手机", 899.0));
System.out.println(all.get(2));
all.remove(new Phone("白米手机", 999.0));
System.out.println(all.size());
Object obj[] = all.toArray();
for (int x = 0; x < obj.length; x++) {
System.out.println(obj[x]);
}
}
}
java数据结构之手动实现单链表
猜你喜欢
转载自blog.csdn.net/ljh_learn_from_base/article/details/82829926
今日推荐
周排行