大话数据结构 java源代码_大话数据结构(八)Java程序——双向链表的实现
packagecom.aclie.dataStructe4.sqeList;public classMyDoubleLinkList {private int length =0;//當前長度
private Node head;//頭結點
private Node tail;//當前結點結點
publicMyDoubleLinkList(){
initLink();
}public voidinitLink(){
head= new Node(null);
tail= new Node(null);this.head =tail;
length++;
}//獲取鏈表長度
public intgetSize(){returnlength;
}//判斷鏈表是否為空
public booleangetEmpty(){return getSize()==0;
}//根據索引查找元素 從第一個有效值開始
public Node getNode(intindex){
Node p;if(index < 0 || index >length ){
System.out.println("參數錯誤");
}if(index < this.length/2){
p= this.head;for(int i=0; i
p=p.next;
}
}else{
p= this.tail;for(int i= length; i>index;i--){
p=p.prev;
}
}returnp;
}public Object getData(intindex){returngetNode(index).data;
}//在頭結點處插入
public booleanaddHead(Object e){//前驅引用為null,后繼為node
Node node = new Node(e, null, this.head);//改變頭結點的前驅后繼
this.head.prev =node;this.head =node;if(tail == null){
tail= this.head;
}
length++;return true;
}//在尾結點插入
public booleanaddTail(Object e){if(this.head == null){this.head = new Node(e,null,null);this.tail = this.head;
}else{
Node node= new Node(e,this.tail,null);this.tail.next =node;this.tail =node;
}
length++;return true;
}//在指定位置插入元素
public boolean addData(intindex,Object ele){if(index <0 || index > this.length){
System.out.println("參數錯誤");
}if(this.head == null){this.addTail(ele);//用尾插法
}else{if(index == 0){
addHead(ele);//用頭插法
}else{
Node p= this.getNode(index);//要插入處的結點
Node n =p.next;
Node node= new Node(ele,p,n);//要插入的結點
n.prev =node;
p.next=node;
length++;
}
}return true;
}public void removeData(intindex){if(index < 0 || index >length){
System.out.println("參數錯誤");
}else{
Node del= null;if(index == 0){
del= this.head;this.head = this.head.next;this.head.prev = null;
length--;
}else if(index == (length-1)){
Node p= this.getNode(index-1);//得到要刪除結點的前驅結點
del = p.next;//要刪除的結點
p.next =del.next;if(del.next != null){
del.next.prev=p;
}
del.next= null;
del.prev= null;
length--;this.tail.next = null;this.tail.prev =p;this.tail =p;
}else{
Node p= this.getNode(index-1);//要刪除結點的前驅結點
del = p.next;//要刪除的結點
p.next =del.next;if(del.next != null){
del.next.prev=p;
}
del.prev= null;
del.next= null;
length--;
}
}
}//打印所有鏈表中的元素
public voidprint(){
Node current= this.head;while(current != null){
System.out.println(current.data);
current=current.next;
}
}//反向打印鏈表
public voidreversePrint(){
Node current= this.tail;while(current != null){
System.out.println(current.data);
current=current.prev;
}
}public static voidmain(String args[]){
MyDoubleLinkList linkList= newMyDoubleLinkList();
linkList.addHead("aaaa");//System.out.println(linkList.getData(1));
linkList.addTail("bbbb");//System.out.println(linkList.getData(3));
linkList.addData(2, "eeee");//linkList.print();
linkList.removeData(2);
linkList.print();
System.out.println(".....");
linkList.reversePrint();
}
}classNode{
Node prev;//指針域中前驅
Node next;//指針域中后繼
Object data;//數據域
publicNode(Node current){
prev=current;
next=current;
}//雙鏈表前驅后繼及數字域
publicNode(Object d, Node p,Node n){this.data =d;this.prev =p;this.next =n;
}publicNode getPrev() {returnprev;
}public voidsetPrev(Node prev) {this.prev =prev;
}publicNode getNext() {returnnext;
}public voidsetNext(Node next) {this.next =next;
}
}
總結
以上是生活随笔為你收集整理的大话数据结构 java源代码_大话数据结构(八)Java程序——双向链表的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: yii2 java_YII2 自定义日志
- 下一篇: java io 文件路径格式_java中