java【链表实现】
一般我们在学习集合之前,都会去了解了解数据结构的相关知识,最常见的莫过于链表,在 List 集合里面,linkedList 这个子类就是使用的链表的数据结果进行实现的,
链表的优势:新增数据,删除数据,会很容易
动态数组优势:检索快
下面的有一个关于链表的数据结构,是我写的一个测试,便于我们加深对链表结构的掌握。
package ITaljavaT3;
import org.junit.jupiter.api.Test;
interface Ilink<E>{
public abstract void add(E date);
public abstract int size();
public abstract boolean isEmpty();
public abstract Object[] toArray();
public abstract E getdate (int index);
public abstract void setdate(int index,E date);
public abstract boolean judge(E date);
public abstract void removedate(E date);
public abstract void clean();
}
class impLink<E> implements Ilink<E>{
private class Node{
private E date;
private Node next;
public Node(E date) {
this.date = date;
}
public void addnode(Node newnode) {
if(this.next == null) {
this.next = newnode;
}else {
this.next.addnode(newnode);
}
}
public void returndate() {
impLink.this.returndate[impLink.this.foot ++]=this.date;
if(this.next!=null) {
this.next.returndate();
}
}
public E getNode(int index) {
if(impLink.this.foot++==index) {
return this.date;
}
return this.next.getNode(index);
}
public void setNode(int index,E date ) {
if(impLink.this.foot++ ==index) {
this.date=date;
}else {
this.next.setNode(index,date);
}
}
public boolean judgeNode(E date) {
if(date.equals(this.date)) {
return true;
}else {
if(this.next==null) {
return false;
}else {
return this.next.judgeNode(date);
}
}
}
public void removeNode(Node before,E date) {
if(date.equals(this.date)) {
before.next=this.next;
}else {
this.next.removeNode(this, date);
}
}
}
private Node root;
private int count;
private int foot;
private Object[] returndate;
@Override
public void add(E date) {
if(date==null) {
return;
}
Node newnode = new Node(date);
if(this.root==null) {
this.root = newnode;
}else {
this.root.addnode(newnode);
}
this.count++;
}
public int size() {
return this.count;
}
public boolean isEmpty() {
return this.count==0;
}
public Object[] toArray() {
if(this.isEmpty()) {
return null;
}
this.foot=0;
this.returndate = new Object[this.count];
this.root.returndate();
return this.returndate;
}
public E getdate(int index) {
if(index>=count) {
return null;
}
this.foot=0;
return this.root.getNode(index);
}
public void setdate(int index,E date) {
if(index >= count) {
return;
}else {
this.foot=0;
this.root.setNode(index, date);
}
}
public boolean judge(E date) {
if(date == null) {
return false;
}
return this.root.judgeNode(date);
}
public void removedate(E date) {
if(this.judge(date)) {
if(date.equals(this.root.date)) {
this.root = this.root.next;
}else {
this.root.removeNode(this.root, date);
}
this.count--;
}
}
public void clean() {
this.root = null;
this.count = 0;
}
}
public class datascope {
@Test
public void testdataScope() {
Ilink<String> link = new impLink<String>();
System.out.println(String.format("开始 【长度】%s,【值】%s,【是否为空】%s", link.size(),link.toString(),link.isEmpty()));
link.add("第一个");
link.add("第二个");
link.add("第三个");
link.add("第四个");
link.add("第五个");
System.out.println(String.format("中间 【长度】%s,【值】%s,【是否为空】%s", link.size(),link.toString(),link.isEmpty()));
link.removedate("第一个");
link.setdate(3, "修改第三个数据成功");
System.out.println(String.format("修改 【长度】%s,【值】%s,【是否为空】%s", link.size(),link.toString(),link.isEmpty()));
link.clean();
System.out.println(String.format("结束 【长度】%s,【值】%s,【是否为空】%s", link.size(),link.toString(),link.isEmpty()));
Object[] result = link.toArray();
if(result!=null) {
for(Object temp:result) {
System.out.println(temp);
}
}
System.out.println("----------------指定数据的获取(索引)---------------");
if(!link.isEmpty()) {
System.out.println(link.getdate(1));
System.out.println(link.getdate(2));
System.out.println(link.getdate(3));
System.out.println(link.getdate(19));
}
System.out.println("----------------判断数据是否存在)---------------");
if(!link.isEmpty()) {
System.out.println(link.judge("第一个"));
System.out.println(link.judge("aaa"));
}
}
}
将上述的链表进行扩展,实现一个购物车,写一个购物车的实体类小 demo
package ITaljavaT4;
interface Ilink<E>{
public abstract void add(E date);
public abstract int size();
public abstract boolean isEmpty();
public abstract Object[] toArray();
public abstract E getdate (int index);
public abstract void setdate(int index,E date);
public abstract boolean judge(E date);
public abstract void removedate(E date);
public abstract void clean();
}
class impLink<E> implements Ilink<E>{
private class Node{
private E date;
private Node next;
public Node(E date) {
this.date=date;
}
public void addnode(Node newnode) {
if(this.next==null) {
this.next=newnode;
}else {
this.next.addnode(newnode);
}
}
public void returndate() {
impLink.this.returndate[impLink.this.foot ++]=this.date;
if(this.next!=null) {
this.next.returndate();
}
}
public E getNode(int index) {
if(impLink.this.foot++==index) {
return this.date;
}
return this.next.getNode(index);
}
public void setNode(int index,E date ) {
if(impLink.this.foot++ ==index) {
this.date=date;
}else {
this.next.setNode(index,date);
}
}
public boolean judgeNode(E date) {
if(date.equals(this.date)) {
return true;
}else {
if(this.next==null) {
return false;
}else {
return this.next.judgeNode(date);
}
}
}
public void removeNode(Node before,E date) {
if(date.equals(this.date)) {
before.next=this.next;
}else {
this.next.removeNode(this, date);
}
}
}
private Node root;
private int count;
private int foot;
private Object[] returndate;
@Override
public void add(E date) {
if(date==null) {
return;
}
Node newnode=new Node(date);
if(this.root==null) {
this.root=newnode;
}else {
this.root.addnode(newnode);
}
this.count++;
}
public int size() {
return this.count;
}
public boolean isEmpty() {
return this.count==0;
}
public Object[] toArray() {
if(this.isEmpty()) {
return null;
}
this.foot=0;
this.returndate=new Object[this.count];
this.root.returndate();
return this.returndate;
}
public E getdate(int index) {
if(index>=count) {
return null;
}
this.foot=0;
return this.root.getNode(index);
}
public void setdate(int index,E date) {
if(index>=count) {
return;
}else {
this.foot=0;
this.root.setNode(index, date);
}
}
public boolean judge(E date) {
if(date==null) {
return false;
}
return this.root.judgeNode(date);
}
public void removedate(E date) {
if(this.judge(date)) {
if(date.equals(this.root.date)) {
this.root=this.root.next;
}else {
this.root.removeNode(this.root, date);
}
this.count--;
}
}
public void clean() {
this.root=null;
this.count=0;
}
}
interface Igoods{
public String getName();
public double getprice();
}
interface IShop{
public void addshop(Igoods goods);
public void delete(Igoods goods);
public Object[] getall();
}
class Shop implements IShop{
private Igoods goods;
private Ilink<Igoods> shopgoogs =new impLink<Igoods>();
@Override
public void addshop(Igoods goods) {
this.shopgoogs.add(goods);
}
@Override
public void delete(Igoods goods) {
this.shopgoogs.removedate(goods);
}
@Override
public Object[] getall() {
return this.shopgoogs.toArray();
}
}
class bag implements Igoods{
private String name;
private double price;
public bag(String name,double price ) {
this.name=name;
this.price=price;
}
@Override
public String getName() {
return this.name;
}
@Override
public double getprice() {
return this.price;
}
public String toString() {
return "[商品信息:]"+this.name+" -- "+"[商品的价格:]"+this.price;
}
public boolean equals(Object obj) {
if(obj==null) {
return false;
}
if(!(obj instanceof bag)) {
return false;
}
if(obj==this) {
return true;
}
bag other=(bag)obj;
return this.name.equals(other.name) && this.price==other.price;
}
}
class book implements Igoods{
private String name;
private double price;
public book(String name,double price ) {
this.name=name;
this.price=price;
}
@Override
public String getName() {
return this.name;
}
@Override
public double getprice() {
return this.price;
}
public String toString() {
return "[商品信息:]"+this.name+" -- "+"[商品的价格:]"+this.price;
}
public boolean equals(Object obj) {
if(obj==null) {
return false;
}
if(!(obj instanceof book)) {
return false;
}
if(obj==this) {
return true;
}
book other=(book)obj;
return this.name.equals(other.name) && this.price==other.price;
}
}
class Cashier{
public double allprice(IShop shop) {
double allprice=0.0;
Object obj[]=shop.getall();
for(Object temp:obj) {
Igoods goods=(Igoods)temp;
allprice+=goods.getprice();
}
return allprice;
}
public int allcount(IShop shop) {
return shop.getall().length;
}
}
public class ITaljava01 {
public static void main(String[] args) {
IShop shop =new Shop();
shop.addshop(new bag("超级书包",18.9));
shop.addshop(new bag("中级书包",18.9));
shop.addshop(new bag("低级书包",11.9));
shop.addshop(new bag("低级书包",11.9));
shop.addshop(new book("c++",19.9));
shop.addshop(new book("java",88.9));
Object object1[] =shop.getall();
for(Object temp: object1) {
System.out.println(temp);
}
shop.delete(new bag("低级书包",11.9));
System.out.println("---------删除以后获取数据--------------");
Object object2[] =shop.getall();
for(Object temp: object2) {
System.out.println(temp);
}
System.out.println("--------计算购物车里面的价钱---------------");
Cashier cas=new Cashier();
System.out.println(cas.allprice(shop));
System.out.println(cas.allcount(shop));
}
}
有兴趣的可以分析一下代码,也比较简单,主要是理解集合里面的一些数据结构。
|