建立乙個雙向鍊錶的節點:
class
listnode
}
關於頭插:
如何將鍊錶列印出來
public
void
display()
} system.out.
println
("]");
system.out.
println
("反向");
system.out.
print
("[");
for(listnode cur = head.prev ; cur != head;cur = cur.prev)
} system.out.
println
("]");
}
尾插(重點是找到prev)
public
void
addlast
(int date)
頭插尾插複雜度都是o(1)
總**如下:
class
listnode
}public
class
dlinklist
public
void
addfirst
(int date)
public
void
addlast
(int date)
public
void
display()
} system.out.
println
("]");
system.out.
println
("反向");
system.out.
print
("[");
for(listnode cur = head.prev ; cur != head;cur = cur.prev)
} system.out.
println
("]");
}public
void
addindex
(int index ,
int date)
if(index ==0)
if(index == size)
listnode next =
getpos
(index)
; listnode prev = next.prev;
listnode newnode =
newlistnode
(date)
; newnode.next = next;
next.prev = newnode;
prev.next = newnode;
newnode.prev = prev;
}public
boolean
contains
(int tofind)
}return
false;}
public
void
remove
(int key)
listnode next = toremove.next;
listnode prev = toremove.prev;
prev.next = next;
next.prev = prev;
}public
void
removeall
(int key)
listnode next = toremove.next;
listnode prev = toremove.prev;
prev.next = next;
next.prev = prev;}}
public listnode tofind
(int key)
}return null;
}public listnode getpos
(int index)
return cur;
}public
intsize()
return size;
}public
void
claer()
}
測試**:
public
class
test
public
static
void
testaddfirst()
public
static
void
testaddlast()
public
static
void
testaddindext()
public
static
void
testcontains()
public
static
void
testremove()
public
static
void
testremoveall()
}
雙向迴圈帶頭節點鍊錶
include include struct dblnode typedef struct dblnode dblnode typedef struct dblnode dbllink void create link dbllink head 建立鍊錶 void create newnode db...
帶頭結點帶環的雙向鍊錶的實現
在寫這個 的實現之前我們先來了解下相關的知識。首先,帶頭結點帶環的雙向鍊錶的特點 帶頭節點 建立乙個結點表示空鍊錶,這個節點就是頭結點,並且頭結點中的資料不具有實際的意義。但是我們一般不關心頭結點中的元素,只起 帶頭 作用。雙向要求每個結點中都有乙個next指標指向下乙個,乙個prev指標指向下乙個...
雙向迴圈帶頭結點鍊錶的常見操作
include dlist.h include malloc.h include assert.h include pdlnode buydlist dldatatype data pnewnode pnext null 剛開始給節點並不知道位置所以給null pnewnode ppre null ...