溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java數(shù)據(jù)結構中單鏈表與雙向鏈表的實現(xiàn)方法

發(fā)布時間:2021-08-02 09:28:29 來源:億速云 閱讀:121 作者:chen 欄目:開發(fā)技術

這篇文章主要介紹“java數(shù)據(jù)結構中單鏈表與雙向鏈表的實現(xiàn)方法”,在日常操作中,相信很多人在java數(shù)據(jù)結構中單鏈表與雙向鏈表的實現(xiàn)方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java數(shù)據(jù)結構中單鏈表與雙向鏈表的實現(xiàn)方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

目錄
  • 單鏈表:

    • 實現(xiàn)思路

    • 代碼實現(xiàn)

  • 雙向鏈表:

    • 實現(xiàn)思路

    • 代碼實現(xiàn)


單鏈表

每個數(shù)據(jù)是以節(jié)點的形式存在的

每個節(jié)點分為數(shù)據(jù)域和指針域

數(shù)據(jù)域中保存該節(jié)點的數(shù)據(jù)

指針域中保存指向下一個節(jié)點的指針

實現(xiàn)思路

節(jié)點類SingleNode中保存數(shù)據(jù)和指向下一個節(jié)點的指針

單鏈表類SingleLinkedList中保存鏈表的頭節(jié)點,實現(xiàn)相關鏈表方法

對于鏈表方法,涉及到位置查找,如在指定位置增加、刪除節(jié)點,需要使用一個臨時變量temp從頭節(jié)點開始遍歷,直至找到對應的位置。

對于節(jié)點的增加刪除,只需要修改相關結點的指針指向即可

代碼實現(xiàn)

節(jié)點類SingleNode:

public class SingleNode {
	public int data;
	public SingleNode next;
	public SingleNode(int data) {
		this.data = data;
	}
	@Override
	public String toString() {
		return "SingleNode{" + "data=" + data + '}';
	}
}

單鏈表類SingleLinkedList

public class SingleLinkedList {
	private SingleNode head = new SingleNode(0);
    //獲取頭結點
	public SingleNode getHead(){
		return head;
	}
	//添加節(jié)點
	public void add(SingleNode singleNode) {
		SingleNode temp = head;
		//找到鏈表最后一個節(jié)點
		while (temp.next != null) {
			temp = temp.next;
		}
		temp.next = singleNode;
	}
	//按順序添加節(jié)點
	public void addByOrder(SingleNode singleNode) {
		SingleNode temp = head;
		while (true) {
			if (temp.next == null) {
				//已經(jīng)遍歷到鏈表最后了
				break;
			} else if (temp.next.data > singleNode.data) {
				//找到應插入的位置
				break;
			}
			temp = temp.next;
		}
		singleNode.next = temp.next;
		temp.next = singleNode;
	}
	//刪除節(jié)點
	public void delete(int data) {
		SingleNode temp = head;
		boolean flag = false;    //標志是否找到待刪除節(jié)點的前一個節(jié)點
		while (true) {
			if (temp.next == null) {
				//已經(jīng)遍歷到鏈表最后了
				break;
			} else if (temp.next.data == data) {
				//找到待刪除節(jié)點的前一個節(jié)點
				flag = true;
				break;
			}
			temp = temp.next;
		}
		if (flag) {
			temp.next = temp.next.next;
		} else {
			System.out.println("要刪除的節(jié)點不存在");
		}
	}
	//輸出鏈表
	public void show() {
		//判斷鏈表是否為空
		if (head.next == null) {
			System.out.println("鏈表為空");
			return;
		}
		SingleNode temp = head.next;
		while (temp != null) {
			System.out.println(temp);
			temp = temp.next;
		}
	}
}

雙向鏈表

每個節(jié)點中除了保存了指向后一個節(jié)點的指針外,還保存了指向前一個節(jié)點的指針

實現(xiàn)思路

相關方法實現(xiàn)與單鏈表類似,不同點在于需要增加對指向前一個節(jié)點的指針的更改

代碼實現(xiàn)

節(jié)點類DoubleNode

public class DoubleNode {
	public int data;
	public DoubleNode next;
	public DoubleNode pre;
	public DoubleNode(int data) {
		this.data = data;
	}
	@Override
	public String toString() {
		return "DoubleNode{" + "data=" + data + '}';
	}
}

雙向鏈表類DoubleLinkedList

public class DoubleLinkedList {
	public DoubleNode head = new DoubleNode(0);
	//獲取頭結點
	public DoubleNode getHead() {
		return head;
	}
	//添加節(jié)點
	public void add(DoubleNode doubleNode) {
		DoubleNode temp = head;
		//找到鏈表最后一個節(jié)點
		while (temp.next != null) {
			temp = temp.next;
		}
		temp.next = doubleNode;
		doubleNode.pre = temp;
	}
	//刪除節(jié)點
	public void delete(int data) {
		DoubleNode temp = head.next;
		boolean flag = false;    //標志是否找到待刪除節(jié)點的前一個節(jié)點
		while (true) {
			if (temp == null) {
				//已經(jīng)遍歷到鏈表最后了
				break;
			} else if (temp.data == data) {
				//找到待刪除節(jié)點
				flag = true;
				break;
			}
			temp = temp.next;
		}
		if (flag) {
			temp.pre.next = temp.next;
			if (temp.next != null) {
				//若刪除節(jié)點不為最后一個節(jié)點
				temp.next.pre = temp.pre;
			}
		} else {
			System.out.println("要刪除的節(jié)點不存在");
		}
	}
	//輸出鏈表
	public void show() {
		//判斷鏈表是否為空
		if (head.next == null) {
			System.out.println("鏈表為空");
			return;
		}
		DoubleNode temp = head.next;
		while (temp != null) {
			System.out.println(temp);
			temp = temp.next;
		}
	}
}

到此,關于“java數(shù)據(jù)結構中單鏈表與雙向鏈表的實現(xiàn)方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI