本文目錄一覽:
- 1、java單鏈表遍歷,最後會輸出一個0,這個零是什麼,頭指針的引用嗎
- 2、用java單鏈表實現一元多項式相加的演算法?
- 3、java循環單鏈表實現約瑟夫環
- 4、用java如何創建一個單鏈表和雙鏈表
- 5、java單鏈表根據內容刪除節點
- 6、java 中單鏈表的equals方法
java單鏈表遍歷,最後會輸出一個0,這個零是什麼,頭指針的引用嗎
單鏈錶帶頭結點的遍歷,如果把temp!=null改成temp.next!=null遍歷就正常了,但是去掉.next就會多出一個0。這個0是一個未經初始化的內存中「殘存」的數字,這一次是零,可能在,下一次運行的時候,裡面出現的數字就可能不是0,而是其他不規則的數字。
用java單鏈表實現一元多項式相加的演算法?
public class Test {
public static void main(String[] args) {
try{
LinkList list1 = new LinkList();
LinkList list2 = new LinkList();
LinkList list3 = null;
list1.addAt(0, new Item(1, 5));
list1.addAt(1, new Item(-1.5, 3));
list1.addAt(2, new Item(1, 1));
list2.addAt(0, new Item(0.5, 5));
list2.addAt(1, new Item(0.5, 4));
list2.addAt(2, new Item(1.5, 3));
list2.addAt(3, new Item(3, 0));
list3 = mergeLinkList(list1, list2);
System.out.println(“一元多項式的相加過程:”);
list1.listAll();
System.out.println(” + “);
list2.listAll();
System.out.println(” = “);
list3.listAll();
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 一元多項式的一般項類
*/
class Item{
private double coef; //一元多項式的一般項的係數
private int exp; //一元多項式的一般項的指數
public Item(){
this.coef = 0.0;
this.exp = 0;
}
public Item(double coef, int exp){
this.coef = coef;
this.exp = exp;
}
public double getCoef(){
return this.coef;
}
public void setCoef(double coef){
this.coef = coef;
}
public int getExp(){
return this.exp;
}
public void setExp(int exp){
this.exp = exp;
}
}
/**
* 鏈表結點類
*/
class Node{
private Item data;
private Node next; //鏈表結點的指針域,指向直接後繼結點
public Node(){
data = null;
next = null;
}
public Node(Item data, Node next){
this.data = data;
this.next = next;
}
public Item getData(){
return this.data;
}
public void setData(Item data){
this.data = data;
}
public Node getNext(){
return this.next;
}
public void setNext(Node next){
this.next = next;
}
}
/**
* 鏈表類
*/
class LinkList{
private Node head = null; //頭結點指針
private int size = 0;
public LinkList(){
head = new Node();
size = 0;
}
//在i位置插入元素elem
public boolean addAt(int i, Item elem) {
if(i 0 || i size){
return false;
}
Node pre,curr;
int pos;
for(pre=head; i0 pre.getNext()!=null; i–,pre=pre.getNext());
curr = new Node(elem, pre.getNext());
pre.setNext(curr);
size++;
return true;
}
//刪除i位置的元素
public boolean removeAt(int i) {
if(i 0 || i = size){
return false;
}
Node pre,curr;
for(pre=head; i0 pre.getNext()!=null; i–,pre=pre.getNext());
curr = pre.getNext();
pre.setNext(curr.getNext());
size–;
return true;
}
java是一種可以撰寫跨平台應用軟體的面向對象的程序設計語言。Java 技術具有卓越的通用性、高效性、平台移植性和安全性,廣泛應用於PC、數據中心、遊戲控制台、科學超級計算機、行動電話和互聯網,同時擁有全球最大的開發者專業社群。
java循環單鏈表實現約瑟夫環
看了你的代碼,不是很明白,給你提幾個建議吧:
1、不需要tail節點
2、remove方法應該對刪除節點前面的節點操作,而不是使用數字找
給你我修改的LinkList類,你參考一下:
public class LinkList {
private Node head;
int curlen = 0;
// 創建鏈表
public void createlist(int code) throws Exception {
insert(curlen, code);
}
public void insert(int i, int code) throws Exception {
Node s = new Node(code);
if (i == 0) {
s.setNext(head);
head = s;
}
Node p = head;
int j = 0;
while (p != null j i – 1) {
p = p.getNext();
j++;
}
if (j i || p == null) {
throw new Exception(“插入位置不合理”);
}
s.setNext(p.getNext());
p.setNext(s);
// tail = s;
// tail.setNext(head);
curlen = curlen + 1;
}
public void remove(int i) throws Exception {
Node p = head, q = null;
int j = 0;
i = i – 1;
while (j i) {
q = p;
p = p.getNext();
j++;
}
if (j i || p == null)
throw new Exception(“刪除位置不合法”);
if (q == null) {
// tail.setNext(p.getNext());
head = head.getNext();
} else
q.setNext(p.getNext());
curlen = curlen – 1;
}
/**
* 按照節點刪除
* @param i
* @throws Exception
*/
public void remove(Node p) throws Exception {
if(p.getNext()==p){
p=null;
head=null;
}
else{
Node q = p.getNext();
p.setNext(q.getNext());
}
curlen = curlen – 1;
}
public void out(int m) throws Exception {
Node p = head;
if(m==1){
System.out.print(“按照順序出列”);
return;
}
int count = 1;
int n=m-1;
while (curlen 0) {
if (count == n) {
System.out.print(p.getNext().getData() + ” “);
remove(p);
count = 1;
} else {
count++;
}
p = p.getNext();
}
}
public void display() {
Node node = head;
for (int i = 0; i 2 * curlen; i++) {
System.out.print(node.getData() + ” “);
node = node.getNext();
}
System.out.println();
}
}
用java如何創建一個單鏈表和雙鏈表
單向鏈表
單向鏈表就是通過每個結點的指針指向下一個結點從而鏈接起來的結構。
單向鏈表的初始化:這裡我所講的鏈表都是頭結點不參與計算的,也就是說第一個結點都是頭結點後面的第一個結點。所以我要先申明一點,這裡我把鏈表的初始化放在了構造函數部分,然後析構函數負責釋放頭結點的內存。
單向鏈表的創建過程:鏈表的創建就是添加結點到鏈表的最後,開始是添加一個結點到head結點後面,然後添加一個結點到上次添加的結點後面,每次新建的結點的指針總是指向NULL指針。從上面的示意圖可以看出,我們需要一個輔助指針一直指向最後一個結點,這個輔助結點就是為了讓每次添加的結點都放置在最後一個位置。
單向鏈表插入結點過程:源代碼中的的插入結點函數我設置了一個指定位置,就是在指定位置插入結點。首先,通過位置變數position讓ptemp結點移動到要插入位置的前一個位置,然後接下來的過程就是和創建鏈表的過程是一樣的,把新建的結點添加到ptemp的後面。這裡變數position可以從1到鏈表長度加1,意思就是如果不算頭結點的話有3個結點,那你的position變數就可以從1到4,這是因為ptemp指針可以到第3個結點的位置,所以新建結點的位置就可以到4了。
單向鏈表刪除結點過程:源代碼中的刪除結點函數也有一個指定位置變數,為了刪除指定位置的結點。和插入結點一樣通過變數position把ptemp移動到要刪除結點的前一個位置,然後讓ptemp結點中的指針指向要刪除結點後面的一個結點,也就是ptemp結點的下一個的下一個結點,雖然這個結點可能為空,但是程序還是正常運行。但是這裡和插入結點不同的是變數position只能從1到鏈表的長度,是因為ptemp移動到最後一個結點的時候,它的下一個結點為空,所以不不需要參與刪除了。
雙向鏈表
1.聽名字可能就能猜到雙向鏈表就是鏈表結點包含兩個指針,一個指針是指向下一個結點的,另一個指針當然就是指向上一個結點的。
2.雙向鏈表的初始化:由於這裡的鏈表頭結點不參與計算,所以頭結點的pPre指針是一直指向NULL指針的。
3.雙向鏈表的創建過程:由於雙向鏈表的每個結點包含兩個指針那麼這個時候我們就要小心處理好每一個指針的指向,要不然會有很多意想不到的錯誤。同樣的,和單向鏈表的創建過程一樣,需要一個輔助指針來指向最後一個結點,然後每新建一個結點,這個結點的pNext指針都是指向NULL指針的,pPre指針指向上一個結點(這是和單向鏈表不同的地方),然後讓上一個指針的pNext指向新建的結點,這樣整個鏈表就連接起來了。
4.雙向鏈表插入結點過程:知道了雙向鏈表的創建過程,那麼插入結點的過程就大同小異 了,有一點需要特別注意的就是這裡的變數position範圍也是從1到鏈表長度加1,但是如果待插入的位置是最後一個位置的話,情況就不同了,看到下面的圖我們可以很好的理解,因為沒新建一個結點的時候都需要處理兩個指針,而且新建結點的下一個結點的pPre指針就需要指向這個新建的結點,但是有可能這個新建的結點可能就已經是最後一個結點了,那麼這個時候再執行
ptemp-pNext-pPre = pnew;
這條指令的時候就會報錯了,因為ptemp-pNext已經是個NULL指針了,那空指針哪裡還有pPre呢。因此在程序中要進行一次判斷,看看結點是否是最後一個結點。
5.雙向鏈表刪除結點的過程:要注意的問題和插入結點一樣,看看這個結點是否為NULL。這裡就不重複了。
java單鏈表根據內容刪除節點
代碼:
// 刪除下標為index的節點
public void remove(int index) {
if (index = modCount) {
// 拋異常
System.out.println(“indexOutOfBoundsException!”);// 最好自定義異常拋出,這裡演示
return;
}
Node node = head;
// 如果節點為第一個節點
if (index == 0) {
head = head.next; // 將頭節點指向第二個節點
modCount–;
return;
}
// 如果節點為最後一個節點,找到最後一個節點的前一個節點,不用管
if (index == modCount – 1) {
System.out.println(“ddd”);
// 找到最後一個節點的前一個節點
int j = 0;
while (node != null j index – 1) {
// 最後一個節點的前一個節點
node = node.next;
j++;
}
last = node; // 最後一個節點設置為前一個節點
modCount–;
return;
}
// 如果節點為中間節點
int j = 0;
while (node != null j index – 1) {
// 查找被刪除的節點的前一個節點
node = node.next;
j++;
}
node.next = node.next.next; // 被刪除節點的下一個節點設置為被刪除節點的下下個節點
modCount–;
}
java 中單鏈表的equals方法
LinkedList對equals的定義大致是這樣的:(下文將equals 寫作 相等)
兩個鏈表相等當且僅當其大小相等,並且每個對應元素也相等。
談到相等,必須談及hashCode方法,關係如下:
hashCode相等的 可能相等(相等與否需進一步檢測)
hashCode不等的 一定不等(排除相等情況)
一般情況下,hashCode的計算一定比equals快,而且『不等』的情況較多,因此作為判斷相等的加速方案,java會先檢測hashCode,若不等,則對象一定不等。
那麼,對於java的常見類,例如:基本類型包裝類、集合類、字元串類等 其 equals 與 hashCode已經寫好,不用我們操心。
若是自定義類,一定要重新equals與hashCode方法,滿足上述hashCode的2個關係。
例如自己的User類有name和pass,一個簡單的方案如下:
public class User{
String name,pass;
//get/set….
public int hashCode() {
return name.hashCode + pass.hashCode();
}
public boolean equals(Object o) {
if(o instanceof User) {
User u = (User)o;
return (name.equals(u.name))(pass.equals(u.pass));
}
return false;
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/308721.html