Java 單鏈表資料結構的增刪改查教程
我就廢話不多說了,大家還是直接看程式碼吧~
package 連結串列; /** * *1)單鏈表的插入、刪除、查詢操作; * 2)連結串列中儲存的是int型別的資料; **/ public class SinglyLinkedList { private Node head = null; //查詢操作 public Node findByValue(int value){ Node p = head; //從連結串列頭部開始查詢 while(p.next != null && p.data != value){//如果資料不相等並且下一個節點不為null,繼續查詢 p = p.next; } return p; } //通過index查詢 public Node findByIndex(int index){ Node p = head; //從連結串列頭部開始查詢 int count = 0; //指標計數器 while(p.next != null && index != count){ //當下個節點不為null,並且計數器不等於index的時候繼續查詢 p = p.next; count++; } return p; } //無頭部節點(哨兵),表頭部插入一個值,這種操作和輸入的順序相反,逆序 public void insertToHead(int value){ Node newNode = new Node(value,null); insertToHead(newNode); } //無頭部節點(哨兵),表頭部插入新節點,逆序 public void insertToHead(Node newNode){ if(head == null){ head = newNode; }else{ newNode.next = head; head = newNode; } } //連結串列尾部插入,按順序插入,時間複雜度平均為O(n),這個可以優化,定義多一個尾部節點,不儲存任何資料,時間複雜度未O(1) public void insertTail(int value){ Node newNode = new Node(value,null); if(head == null){//連結串列為空 head = newNode; }else{//直接從連結串列頭開始找,知道找到鏈尾節點 Node curr = head; while(curr.next != null){ curr = curr.next; } curr.next = newNode; } } //在指定節點後面插入新節點,直接在這個節點後面斷開連線,直接插入 public void insertAfter(Node p,int value){ Node newNode = new Node(value,null); insertAfter(p,newNode); } //在指定節點後面插入新節點,Node newNode){ if(p == null){ return; } newNode.next = p.next; p.next = newNode; } //在指定節點前面插入新節點 public void insertBefore(Node p,null); insertBefore(p,newNode); } //在指定節點前面插入新節點 public void insertBefore(Node p,Node newNode){ if(p == null){ return; } if(p == head){//如果指定節點是頭節點 insertToHead(p); return; } Node curr = head;//當前節點,(查詢指定節點p的前一個節點,當curr的下個節點等於指定節點時候,curr就是指定節點的前一個節點 while(curr != null && curr.next != p){//當前節點不為null,當前節點的下個節點不等於指點節點,則繼續查詢 curr = curr.next; } if(curr == null){//未找到指定節點p return; } newNode.next = p; curr.next = newNode; } //刪除指定節點 public void deleteByNode(Node p){ if(p == null || p == head){ return; } Node curr = head;//從鏈頭開始查詢,curr是當前節點,查詢指定節點p的前一個節點,curr就是指定節點的前一個節點 while(curr != null && curr.next != p){//當前節點不為null並且,下個節點不等於指定節點時候繼續查詢 curr = curr.next; } if(curr == null){//未找到指定節點 return; } curr.next = curr.next.next; } //刪除指定值 public void deleteByValue(int value){ if(head == null){ return; } Node curr = head;//當前節點,從連結串列頭開始查詢 Node pre = null;//當前節點的前一個節點,找查詢指定的過程,要不斷地儲存當前節點的前一個節點 while(curr != null && curr.data != value){ pre = curr; curr = curr.next; } if(curr == null){//未找到指定的值 return ; } if(pre == null){//連結串列頭資料就是指定的值 head = head.next; }else{ pre.next = pre.next.next;//或者pre.next = curr.next; } } //列印連結串列 public void printAll() { Node curr = head; while(curr != null){ System.out.println(curr.data); curr = curr.next; } } //單鏈表資料結構類,以儲存int型別資料為例 public class Node{ private int data; private Node next; public Node(int data,Node next) { this.data = data; this.next = next; } public int getData(){ return data; } } public static void main(String[]args) { 老師程式碼.linkedlist06.SinglyLinkedList link = new 老師程式碼.linkedlist06.SinglyLinkedList(); System.out.println("hello"); int data[] = {1,2,5,3,1}; for (int i = 0; i < data.length; i++) { //link.insertToHead(data[i]); link.insertTail(data[i]); } System.out.println("列印原始:"); link.printAll(); } }
補充知識:Hbase+Spring Aop 配置Hbase連結的開啟和關閉
Spring 提供了HbaseTemplate 對Hbase資料庫的常規操作進行了簡單的封裝。
get,find方法分別對應了單行資料查詢和list查詢。
這些查詢都要開啟和關閉Hbase資料庫連結
@Override public <T> T execute(String tableName,TableCallback<T> action) { Assert.notNull(action,"Callback object must not be null"); Assert.notNull(tableName,"No table specified"); HTableInterface table = getTable(tableName); try { boolean previousFlushSetting = applyFlushSetting(table); T result = action.doInTable(table); flushIfNecessary(table,previousFlushSetting); return result; } catch (Throwable th) { if (th instanceof Error) { throw ((Error) th); } if (th instanceof RuntimeException) { throw ((RuntimeException) th); } throw convertHbaseAccessException((Exception) th); } finally { releaseTable(tableName,table); } } private HTableInterface getTable(String tableName) { return HbaseUtils.getHTable(tableName,getConfiguration(),getCharset(),getTableFactory()); } private void releaseTable(String tableName,HTableInterface table) { HbaseUtils.releaseTable(tableName,table,getTableFactory()); } HTableInterface table = getTable(tableName); 獲取資料庫連結 releaseTable(tableName,table); 釋放連結
在HbaseUtils.getHTable:
if (HbaseSynchronizationManager.hasResource(tableName)) { return (HTable) HbaseSynchronizationManager.getResource(tableName); }
看見這個大家應該都有是曾相似的感覺吧,這和Spring事務管理核心類TransactionSynchronizationManager很像,而實現也基本一樣
都是通過ThreadLocal將連結儲存到當前執行緒中。
我們要做的就是要像Srping 事務配置一樣,在進入service方法時通過Aop機制將tableNames對應的連結加入到執行緒中。
Spring提供了這個Aop方法攔截器 HbaseInterceptor:
public Object invoke(MethodInvocation methodInvocation) throws Throwable { Set<String> boundTables = new LinkedHashSet<String>(); for (String tableName : tableNames) { if (!HbaseSynchronizationManager.hasResource(tableName)) { boundTables.add(tableName); HTableInterface table = HbaseUtils.getHTable(tableName,getTableFactory()); HbaseSynchronizationManager.bindResource(tableName,table); } } try { Object retVal = methodInvocation.proceed(); return retVal; } catch (Exception ex) { if (this.exceptionConversionEnabled) { throw convertHBaseException(ex); } else { throw ex; } } finally { for (String tableName : boundTables) { HTableInterface table = (HTableInterface) HbaseSynchronizationManager.unbindResourceIfPossible(tableName); if (table != null) { HbaseUtils.releaseTable(tableName,table); } else { log.warn("Table [" + tableName + "] unbound from the thread by somebody else; cannot guarantee proper clean-up"); } } } }
很明顯在
Object retVal = methodInvocation.proceed();
也就是我們的service方法執行前去獲取Hbase連結並通過HbaseSynchronizationManager.bindResource(tableName,table);繫結到執行緒中。
finally中releaseTable。
Aop配置如下:
<!-- 自動掃描beans+註解功能註冊 --> <context:component-scan base-package="com.xxx.xxx" /> <!-- 根據配置檔案生成hadoopConfiguration --> <hdp:configuration resources="classpath:/hbase-site.xml" /> <!-- hadoopConfiguration == hdp:configuration --> <!-- <hdp:hbase-configuration configuration-ref="hadoopConfiguration" /> --> <bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate"> <!-- hadoopConfiguration == hdp:configuration --> <property name="configuration" ref="hadoopConfiguration" /> </bean> <bean id="hbaseInterceptor" class="org.springframework.data.hadoop.hbase.HbaseInterceptor"> <property name="configuration" ref="hadoopConfiguration" /> <property name="tableNames"> <list> <value>table_name1</value> <value>table_name2</value> </list> </property> </bean> <!-- 使用aop增強,織入hbase資料庫連結的開啟和關閉 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.xxx.xxx.*.service..*(..))" /> <aop:advisor advice-ref="hbaseInterceptor" pointcut-ref="allManagerMethod" /> </aop:config>
Hbase的資料庫錶鏈接跟傳統資料庫不太一樣, 開啟連結必需要表名, 所以HbaseInterceptor中必需設定private String[] tableNames;
在進入servcie方法時,tableNames中對應的錶鏈接都會開啟。這必然會造成浪費,因為並不是每個service都會把表都查詢一遍。
以上這篇Java 單鏈表資料結構的增刪改查教程就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。