1.建立双向链表的结点Node2.建立类Linkedlist,成员head是指向表头的引用,在linkedlist中定义链表的操作方法。ps:插入删除操作要判断pos,如果在表尾,要特判防止出现空引用。
public class Node {//建立双向链表的结点 public Node left; public Node right; public int data; public Node(){ this.left=null; this.right=null; this.data=0; } public Node(int data) { this.data=data; } }
public class Linkedlist { public Node head;//指向表头地址的引用 public int length; public Linkedlist(){//建立头结点 length=0; head=new Node(); head.data=0; head.left=null; head.right=null; } public boolean Insert(int pos,Node node) {//在下标为pos-1和pos的结点之间插入node结点 int count=0; Node preNode=head; while(preNode!=null&&count<pos-1) { ++count; preNode=preNode.right; } if(preNode==null||count!=pos-1){ return false; } if(preNode.right==null) { preNode.right=node; node.left=preNode; node.right=null; ++length; return true; } Node nextNode=preNode.right; node.right=nextNode; node.left=preNode; preNode.right=node; nextNode.left=node; ++length; return true; } public boolean Delete(int pos) {//删除指定位置的结点 if(pos<0||pos>=length) { return false; } int count=0; Node curNode=head; while(count!=pos&&curNode!=null) { ++count; curNode=curNode.right; } if(count!=pos||curNode==null) { return false; } if(curNode.right==null) { curNode.left.right=null; curNode=null; } if(curNode.right!=null) {//前 curNode.left.right=curNode.right; curNode.right.left=curNode.left; } --length; return true; } public int Size() {//返回链表元素个数 return length; } public boolean isEmpty() { if(length==0) { return true; } return false; } public boolean Traverse() {//输出链表 Node preNode=head; while(preNode.right!=null) { preNode=preNode.right; System.out.print(preNode.data+" "); } return true; } public int getData(int pos) {//取得指定位置结点元素 Node preNode=head; int count=0; while(count!=pos) { preNode=preNode.right; ++count; } return preNode.data; } public boolean Delete(Node node) {//删除node结点 Node curNode=head; while(curNode!=node&&curNode!=null) { curNode=curNode.right; } if(curNode!=node) { return false; } if(curNode.right!=null) { curNode.left.right=curNode.right; curNode.right.left=curNode.left; --length; return true; } if(curNode.right==null) { curNode.left.right=null; --length; return true; } return false; } }
public class Main { public static void main(String[] args) { Linkedlist link=new Linkedlist(); //System.out.println(link.head==null); Node node1=new Node(1); Node node2=new Node(3); Node node3=new Node(5); Node node4=new Node(9); Node node5=new Node(2); boolean f=link.Insert(1,node1); f=link.Insert(2, node2); f=link.Insert(3, node3); f=link.Insert(4, node4); f=link.Insert(5, node5); //System.out.println(link.head.data); //System.out.println(link.head.right.data); //System.out.println(f); //Node node=node1; link.Delete(2); link.Delete(node5); boolean g=link.Delete(node3); // System.out.println("g="+g); //while(node!=null) { // System.out.println(node.data); // node=node.right; // } System.out.println("size="+link.Size()); System.out.println("isempty? "+link.isEmpty()); link.Traverse(); System.out.println(); System.out.println("data2="+link.getData(2)); } }
时间: 2024-10-15 09:32:05