標籤:單鏈表 資料結構
單鏈表的基本操作包括建立單鏈表、尋找運算(按序尋找和按值尋找)、插入運算(前插和後插)和刪除運算。下面給出具體的java實現程式:
package com.zpp.test;//首先建立一個節點類public class Node { private Node next; //指標域 private int data;//資料域 public Node( int data) { this. data = data; } }package com.zpp.test;public class LinkList { public Node first; // 定義一個前端節點 private int pos = 0;// 節點的位置 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 insertAfter(int index, int data) { Node node = new Node(data); Node current = first; while ( pos != index) { current = current. next; pos++; } node. next = current.next; current. next = node; pos = 0; } // 在指定位置index的前面插入節點 public void insertBefore(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; } // 刪除指定位置的節點 public void 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; } } // 根據節點的data刪除節點(僅僅刪除第一個) public void deleteByData( int data) { Node current = first; Node previous = first; //記住上一個節點 while (current. data != data) { if (current. next == null) { System.out.println("沒有找到對應節點,無法進行刪除操作!"); } previous = current; current = current. next; } if(current == first) { first = first. next; } else { previous. next = current. next; } } // 根據位置尋找節點資訊 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; } // 顯示出所有的節點資訊 public void displayAllNodes() { Node current = first; while (current != null) { System.out.print(current.data+" "); current = current. next; } System.out.println(); }}package com.zpp.test;//測試類別public class TestLinkList { public static void main(String[] args) { LinkList linkList = new LinkList(); linkList.addFirstNode(20); linkList.addFirstNode(21); linkList.addFirstNode(19); //19,21,20 linkList.insertBefore(1, 22); //19,22,21,20 linkList.insertBefore(2, 23); //19,22,23,21,20 linkList.insertAfter(3, 99); //19,22,23,21,99,20 linkList.displayAllNodes();// Node node = linkList.deleteFirstNode();// System.out.println("node : " + node.data);// linkList.displayAllNodes();// node = linkList.deleteByPos(2);// System.out.println("node : " + node.data);// linkList.displayAllNodes();// linkList.deleteFirstNode(); linkList.deleteByData(19);// Node node = linkList.deleteByPos(0); //System. out.println( "node : " + node. data); linkList.displayAllNodes(); Node node1 = linkList.findByPos(0); System. out.println( "node1: " + node1. data); Node node2 = linkList.findByData(22); System. out.println( "node2: " + node2. data); }}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
資料結構複習--java實現單鏈表基本操作