node類:
//節點類
public class Node {
protected Node next; //指針域
protected int data;//數據域
public Node( int data) {
this. data = data;
}
//顯示此節點
public void display() {
System. out.print( data + " ");
}
}
(一)單鏈表
1)構造鏈表
2)鏈表頭插入
// 插入一個頭節點
public void addFirstNode( int data) {
Node node = new Node(data);
node. next = first;
first = node;
}
3)鏈表頭刪除
只需要 first = first.next 即可
// 刪除一個頭結點,並返回頭結點
public Node deleteFirstNode() {
Node tempNode = first;
first = tempNode. next;
return tempNode;
}
4)鏈表任意位置添加
添加位置前後斷開,分別指向對應的引用即可
// 在任意位置插入節點 在index的後面插入
public void add(int index, int data) {
Node node = new Node(data);
Node current = first;
Node previous = first;
while ( pos != index) {
previous = current;
current = current. next;
pos++;
}
node. next = current;
previous. next = node;
pos = 0;
}
5)任意位置刪除
// 刪除任意位置的節點
public Node deleteByPos( int index) {
Node current = first;
Node previous = first;
while ( pos != index) {
pos++;
previous = current;
current = current. next;
}
if(current == first) {
first = first. next;
} else {
pos = 0;
previous. next = current. next;
}
return current;
}
6)鏈表遍歷
// 顯示出所有的節點信息
public void displayAllNodes() {
Node current = first;
while (current != null) {
current.display();
current = current. next;
}
System.out.println();
}
7)根據位置和根據節點查找
// 根據位置查找節點信息
public Node findByPos( int index) {
Node current = first;
if ( pos != index) {
current = current. next;
pos++;
}
return current;
}
// 根據數據查找節點信息
public Node findByData( int data) {
Node current = first;
while (current. data != data) {
if (current. next == null)
return null;
current = current. next;
}
return current;
}
(一)雙鏈表
1)它是這樣的
2)雙鏈表整體鳥覽
3)雙鏈表添加
4)雙鏈表刪除
5)雙鏈表代碼
/**
* 雙向鏈表
* @author yl
*
*/
public class LinkNode {
private Object obj;
private LinkNode child;
private LinkNode parent;
public LinkNode(Object obj){
this.obj=obj;
}
//定義方法
public Object getObj(){
return obj;
}
public void setObj(Object obj){
this.obj=obj;
}
public LinkNode getChild(){
return child;
}
public void setChild(LinkNode child){
this.child=child;
}
public LinkNode getParent(){
return parent;
}
public void setParent(LinkNode parent){
this.parent=parent;
}
}
再來對這個鏈表進行測試:
package cn.鏈表;
/**
* 雙向鏈表的測試
* @author Administrator
*
*/
public class LinkListTest {
private static LinkNode front=null;
private LinkNode last=front;
public static void main(String args[]){
LinkListTest list=new LinkListTest();
list.add("頭結點");
for(int i=0;i<10;i++){
list.add("結點"+i);
}
//測試指定位置下取得結點
Object obj=list.getLinkNode(3).getObj();
//測試在指定位置插入元素
list.add(3, "新來的元素");
System.out.println("<><><><><><><>"+obj);
list.printLinkNode(front);
System.out.println("<><><><><><><><><><><><><><><><><><><><><");
// list.remove(3);
// list.printLinkNode(front);
list.upDate(3, "值被改變的元素");
list.printLinkNode(front);
System.out.println("<><><><><><><><><><><><><><><><><><><><><");
list.printLinkNode1(3);
}
/**
* 在鏈表的後面插入元素
* @param obj
*/
public void add(Object obj){
//根據給定的值創建結點
LinkNode node=new LinkNode(obj);
if(front==null){
//如果鏈表爲空的時候
// front.setChild(node);
// node.setParent(front);
//第一個結點也即是最後一個結點
front=node;
last=front;
// System.out.println(front.getObj());
}
else{
//新插入的結點爲最後一個結點
last.setChild(node);
node.setParent(last);
last=node;
}
}
/**
* 在指定位置插入元素
* @param index
* @param obj
*/
public void add(int index,Object obj){
//先創建結點
LinkNode node=new LinkNode(obj);
//判斷傳入的下標
if(index<0||index>size()){
throw new RuntimeException("下標越界:size:"+size()+"index:"+index);
}else{
//傳入的下標符合要求
if(front==null){
//如果鏈表爲空的時候
front=node;
last=front;
}else if(index==size()){
add(node);
}else{
//鏈表不爲空,取得當前下標的結點
LinkNode nownode=getLinkNode(index);
//得到父結點
LinkNode fnode=nownode.getParent();
//重新定義新的引用關係
fnode.setChild(node);
node.setParent(fnode);
node.setChild(nownode);
nownode.setParent(node);
}
}
}
/**
* 根據下標,刪除當前的結點
* @param index
*/
public void remove(int index){
if(index<0||index>size()){
throw new RuntimeException("下標越界:size:"+size()+"index:"+index);
}else{
//傳入的下標符合要求
if(front==null){
//如果鏈表爲空的時候
System.out.println("鏈表爲空,不能刪除元素啦!!!");
}
else{
//鏈表不爲空,取得當前下標的結點
LinkNode nownode=getLinkNode(index);
//得到父結點
LinkNode fnode=nownode.getParent();
//得到父結點
LinkNode cnode=nownode.getChild();
//重新定義新的引用關係
fnode.setChild(cnode);
cnode.setParent(fnode);
}
}
}
/**
* 根據下標取得當前的結點
* @param index 下標值
* @return
*/
public LinkNode getLinkNode(int index){
//判斷傳入的下標
if(index<0||index>size()){
throw new RuntimeException("下標越界:size:"+size()+"index:"+index);
}else{
//先取得頭結點
LinkNode node=front;
int i=0;
while(i<index){
i++;
node=node.getChild();
}
return node;
}
}
/**
* 在指定的位置,更新該結點,結點的值爲obj
* @param index
* @param obj 更改的結點的值
*/
public void upDate(int index,Object obj){
if(index<0||index>size()){
throw new RuntimeException("下標越界:size:"+size()+"index:"+index);
}else{
//傳入的下標符合要求
if(front==null){
//如果鏈表爲空的時候
System.out.println("鏈表爲空,不能更新元素啦!!!");
}
else{
//鏈表不爲空,取得當前下標的結點
LinkNode nownode=getLinkNode(index);
//給結點重新賦值
nownode.setObj(obj);
}
}
}
/**
* 得到鏈表的長度
* @return
*/
public int size(){
if(front==null){
//鏈表爲空
return 0;
}else{
//不爲空
LinkNode node=front;
int count=0;
while(node!=null){
count++;
node=node.getChild();
}
return count;
}
}
/**
* 打印鏈表
* @param node 傳入鏈表的頭結點
*/
public void printLinkNode(LinkNode node){
if(front==null){
System.out.println("此鏈表爲空!!!");
}else{
//先取得頭結點
LinkNode n=front;
//遍歷鏈表
while(n!=null){
Object obj=n.getObj();
System.out.println(obj);
n=n.getChild();
}
}
}
/**
* 根據指定的位置來前後遍歷
* @param index
*/
public void printLinkNode1(int index){
if(index<0||index>size()){
throw new RuntimeException("下標越界:size:"+size()+"index:"+index);
}
LinkNode nownode=getLinkNode(index);
LinkNode cnode=nownode.getChild();
int i=index;
//往前遍歷
while(nownode!=null&&i>=0){
Object obj=nownode.getObj();
System.out.println(obj);
i--;
nownode=nownode.getParent();
}
nownode=getLinkNode(index);
//往後遍歷
while(nownode!=null&&i<size()){
Object obj=nownode.getObj();
System.out.println(obj);
i++;
nownode=nownode.getChild();
}
}
}
參考:
http://blog.csdn.net/tayanxunhua/article/details/11100097/
http://hiliangliang1130-126-com.iteye.com/blog/1144023
http://www.cnblogs.com/xilifeng/archive/2012/10/06/2713185.html
http://blog.sina.com.cn/s/blog_7d44748b01013fsf.html