熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

用java簡單的實現單鏈表的基本操作

2013-11-23 19:15:55  來源: Java核心技術 

  此代碼僅供參考如有疑問歡迎評論

  package comtyxhlink;

  //節點類

  public class Node {

  protected Node next; //指針域

  protected int data;//數據域

  public Node( int data) {

  this data = data;

  }

  //顯示此節點

  public void display() {

  System outprint( data +

  }

  }

  package comtyxhlink;

  //單鏈表

  public class LinkList {

  public Node first; // 定義一個頭結點

  private int pos = ;// 節點的位置

  public LinkList() {

  this first = null;

  }

  // 插入一個頭節點

  public void addFirstNode( int data) {

  Node node = new Node(data)

  node next = first;

  first = node;

  }

  // 刪除一個頭結點並返回頭結點

  public Node deleteFirstNode() {

  Node tempNode = first;

  first = tempNode next;

  return tempNode;

  }

  // 在任意位置插入節點 在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 = ;

  }

  // 刪除任意位置的節點

  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 = ;

  previous next = current next;

  }

  return current;

  }

  // 根據節點的data刪除節點(僅僅刪除第一個)

  public Node deleteByData( int data) {

  Node current = first;

  Node previous = first; //記住上一個節點

  while (current data != data) {

  if (current next == null) {

  return null;

  }

  previous = current;

  current = current next;

  }

  if(current == first) {

  first = first next;

  } else {

  previous next = current next;

  }

  return current;

  }

  // 顯示出所有的節點信息

  public void displayAllNodes() {

  Node current = first;

  while (current != null) {

  currentdisplay()

  current = current next;

  }

  System outprintln()

  }

  // 根據位置查找節點信息

  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;

  }

  }

  package comtyxhlink;

  //測試類

  public class TestLinkList {

  public static void main(String[] args) {

  LinkList linkList = new LinkList()

  linkListaddFirstNode(

  linkListaddFirstNode(

  linkListaddFirstNode(

  //

  linkListadd( //

  linkListadd( //

  linkListadd( //

  linkListdisplayAllNodes()

  //        Node node = linkListdeleteFirstNode()

  //        Systemoutprintln(node : + nodedata)

  //        linkListdisplayAllNodes()

  //        node = linkListdeleteByPos(

  //        Systemoutprintln(node : + nodedata)

  //        linkListdisplayAllNodes()

  //        linkListdeleteFirstNode()

  Node node = linkListdeleteByData(

  //        Node node = linkListdeleteByPos(

  System outprintln( node : + node data)

  linkListdisplayAllNodes()

  Node node = linkListfindByPos(

  System outprintln( node: + node data)

  Node node = linkListfindByData(

  System outprintln( node: + node data)

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26549.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.