java 二维表_java算法--二维链表(单向)

package com.test3;import com.test3.Node;public class LinkDyadicTest {public static void main(String[] args) {LinkList link1 = new LinkList<>();link1.addLast(1);link1.addLast(2);link1.addLast(3);LinkList link2 = new LinkList<>();link2.addLast(4);link2.addLast(5);link2.addLast(6);LinkList link3 = new LinkList<>();link3.addLast(7);link3.addLast(8);link3.addLast(9);LinkDyadic dyadic = new LinkDyadic<>();dyadic.buildNewRow(link1);dyadic.buildNewRow(link2);dyadic.buildNewRow(link3);dyadic.printDyadicLink();System.out.println("get = "+ dyadic.get(1, 2));System.out.println("delete = "+dyadic.delete(2, 2));dyadic.printDyadicLink();}}class LinkList{private Node first;public Node getHead(){return this.first;}public LinkList(){this.first = null;}public void insertFirst(E data) {Node newNode = new Node(data);if (first == null) {first = newNode;} else {newNode.next = first;first = newNode;}}public void addLast(E data){Node newNode = new Node(data);if(first == null){first = newNode;}else{Node temp = first;while(temp.next != null){temp = temp.next;}temp.next = newNode;}}public void printLink(){Node temp = first;while(temp.next != null){System.out.print(temp.data);temp = temp.next;}System.out.println(temp.data);}}class LinkDyadic{private Node root;public LinkDyadic(){this.root = new Node();}//增加新一行public void buildNewRow(LinkList link){if(root.next == null){root.next = link.getHead();}else{Node newNode = addLength();newNode.next = link.getHead();}}//增加行的长度private Node addLength(){Node newNode = new Node();if(root == null){root = newNode;return newNode;}Node temp = root;while(temp.nextRow != null){temp = temp.nextRow;}temp.nextRow = newNode;return newNode;}public boolean isEmpty(){return root.next == null;}/** * 获取第x+1行,第y+1列的元素 * @param x * @param y * @return */public E get(int x, int y){Node tempRow = root;int xCount = 0;int yCount = 0;while(tempRow.nextRow != null && xCount < x){tempRow = tempRow.nextRow;xCount++;}if(xCount >= x){Node tempCol = tempRow.next;while(tempCol.next != null && yCount < y){tempCol = tempCol.next;yCount++;}if(yCount >= y){return tempCol.data;}}throw new ArrayIndexOutOfBoundsException();}/** * 删除第x+1行,第y+1列的元素 * @param x * @param y * @return * @author LiangYH */public E delete(int x, int y){Node tempRow = root;int xCount = 0;int yCount = 0;//操作行while(tempRow.nextRow != null && xCount < x){tempRow = tempRow.nextRow;xCount++;}//操作列if(xCount >= x){Node previous = tempRow;Node tempCol = tempRow.next;while(tempCol.next != null && yCount < y){previous = tempCol;tempCol = tempCol.next;yCount++;}if(yCount >= y){previous.next = tempCol.next;return tempCol.data;}}throw new ArrayIndexOutOfBoundsException();}public void printDyadicLink(){if(isEmpty()) return;Node temp = root;while(temp.nextRow != null){Node temp2 = temp.next;while(temp2.next != null){System.out.print(temp2.data+"; ");temp2 = temp2.next;}System.out.println(temp2.data);temp = temp.nextRow;}Node temp2 = temp.next;while(temp2.next != null){System.out.print(temp2.data+"; ");temp2 = temp2.next;}System.out.println(temp2.data);}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值