动态

详情 返回 返回

用中醫哲學重構數據結構:單鏈表秒變“藥材靈氣通道”(PY/JS/JAVA/GO四語言對照|最小原型|可白嫖|附截圖) - 动态 详情

⚡️ 兄弟姐妹們,你們缺的不是教程,是能跑通的實戰!

💪這篇絕對是乾貨(下文有代碼截圖👇👇),趕緊點贊收藏,源碼打包帶走✨✨

✅✅鏈表手把手教程文章鋪墊蓋地,相信你們也看了很多也學會了,這裏主要講理念+實戰🎈🎈

  • 別人講單鏈表還在用StudentNode怪蜀黎直接上《GMP藥材批次管理》——
    10年ERP老兵+7年中醫修為+其他技能樹,給你整4語言對照的合規原型
    -- Python:輕靈如針灸,107行搞定藥材流轉
    -- Java:穩重如老藥工,嚴苛類型校驗防呆設計
    -- Go:併發如抓藥機,協程級性能碾壓傳統倉儲
    -- JS:靈動如仙丹,前後端通吃還能可視化藥櫃
## 🌿 這玩意多硬核?
✅ 嚴格模擬**先進先出**(藥監局GMP核心要求)  
✅ 智能跳過**零庫存批次**(防止空指針異常如防止抓錯藥)  
✅ 批次消耗**實時追蹤**(隨時生成審計追蹤報告)  
✅ 4語言**同一算法不同實現**(學數據結構順帶練跨界)  

🤯 單鏈表是個啥?怪蜀黎用中醫給你比喻!

官方説法:單鏈表是線性表的數據結構,通過指針連接節點
怪蜀黎的説法單鏈表就是中藥房的藥材流水賬注入理解靈魂後,這回簡單多了是吧!💯💯

  • 頭節點(head) = 藥櫃最左邊的當歸抽屜(第一個批次)
  • next指針 = 藥工的手(順着抽屜一個個往後抓藥)
  • 節點耗盡 = 抽屜抓空了就拆下一個批次的包裝(指針後移)

🌪️ 為什麼不用數組?

  • 數組像預製板西藥盒——大小固定,塞不進一根大人蔘!
  • 單鏈表像中藥小抽屜——隨用隨開,來多少藥材擴多少抽屜!

💡 怪蜀黎的頓悟時刻:

當年在藥庫看師傅抓藥:“先進來的藥材先抓,抓完一批再開下一批”(準確的説應該先抓批次和生產日期更早的這部分,防止時間存放太長變質,當然陳皮這些例外,越陳越好🎉🎉)
這TM不就是單鏈表的FIFO(先進先出)嗎?!
所以怪蜀黎直接把BatchNode寫成藥材批次節點!

其他補充:

工程上建議單個桶(鏈表)長度?⏩

🎯 一般建議控制在 5~10 以內,過長影響查找性能需優化(必要時轉紅黑)

哈希描述
藥材版示意圖(濃厚鄉土氣息海報):
中藥批次管理

┌─────────────────────────────────────────────────┐
│             中藥材哈希批次管理系統               │
│    (哈希函數:藥材名首字母MD5取模)             │
└─────────────────────────────────────────────────┘

► 哈希桶數組(大小:10,示例):
  
  索引0: [當歸] → 批次2025-001(500g) → 批次2025-005(300g) → NULL
  索引1: [黃芪] → 批次2025-002(250g) → NULL
  索引2: [西洋參] → 批次2025-003(200g) → NULL
  索引3: [人蔘] → 批次2025-004(100g) → NULL
  索引4: [甘草] → NULL
  索引5: [枸杞] → 批次2025-006(150g) → NULL(後續自己控制實現)
  ...(其餘桶為空)

► 衝突解決策略:
  同藥材不同批次 → 鏈表法連接(先進先出消耗)
  不同藥材哈希衝突 → 開放尋址法(線性探測下一空桶)

► 關鍵操作:
  1. 入庫:計算哈希值→插入對應桶鏈表尾部
  2. 出庫:哈希定位→從鏈表頭部開始消耗(先進先出)
  3. 查詢:哈希定位→遍歷鏈表打印所有批次

► 特色設計:
  每個批次節點包含:
    - 批次ID(如"Lot-d2025001")
    - 藥材名(如"當歸")
    - 入庫日期("YYYY-MM-DD")
    - 剩餘數量(克)
    - next指針(同藥材下一批次)

廢話不多説直接上四語言代碼,註釋都寫好了💪💪(註釋代表怪蜀黎4種技能樹):

# ==================== 財務單鏈模塊 ====================
# 單向喜錢通道  # 資金只能向前流動的量子管道 ➡️
# ⚠️ERP_冷溪虎山:管道堵塞會導致嘿錢倒灌

class BatchNode:
    """庫存批次節點"""
    def __init__(self, batch_id, herb_name, inbound_date, quantity):
        self.batch_id = batch_id      # 批次ID
        self.herb_name = herb_name    # 藥材名稱
        self.inbound_date = inbound_date  # 入庫日期(字符串格式"YYYY-MM-DD")
        self.quantity = quantity      # 剩餘數量
        self.next = None              # 下一個批次指針

class HerbInventory:
    def __init__(self):
        self.head = None  # 鏈表頭節點(最早入庫的批次)

    # 新批次入庫(添加到鏈表尾部)
    def add_batch(self, batch_id, herb_name, inbound_date, quantity):
        new_node = BatchNode(batch_id, herb_name, inbound_date, quantity)
        if not self.head:  # 空鏈表情況
            self.head = new_node
            return

        current = self.head
        while current.next:  # 遍歷到最後一個節點
            current = current.next
        current.next = new_node  # 新批次添加到尾部

    # 藥材出庫(從最早批次開始消耗)
    def dispense_herb(self, herb_name, required_quantity):
        if not self.head:
            print(f"錯誤:庫存中沒有{herb_name}的任何批次")
            return False

        # 特殊情況:頭節點就是目標藥材且數量足夠
        if (self.head.herb_name == herb_name and
                self.head.quantity >= required_quantity):
            self.head.quantity -= required_quantity
            print(f"出庫成功:從批次{self.head.batch_id}消耗{required_quantity}{herb_name}")
            if self.head.quantity == 0:  # 該批次已耗盡
                self.head = self.head.next
            return True

        # 一般情況:需要遍歷鏈表
        prev = None
        current = self.head
        while current:
            if current.herb_name == herb_name:
                if current.quantity >= required_quantity:
                    current.quantity -= required_quantity
                    print(f"出庫成功:從批次{current.batch_id}消耗{required_quantity}{herb_name}")
                    if current.quantity == 0:  # 該批次已耗盡
                        if prev:  # 不是頭節點
                            prev.next = current.next
                        else:  # 是頭節點
                            self.head = current.next
                    return True
                else:
                    # 當前批次數量不足,嘗試合併後續批次
                    remaining_need = required_quantity - current.quantity
                    if self._merge_subsequent_batches(current, herb_name, remaining_need):
                        return True
                    else:
                        print(f"錯誤:庫存不足,無法滿足{required_quantity}{herb_name}的需求")
                        return False
            prev = current
            current = current.next

        print(f"錯誤:庫存中沒有{herb_name}的批次")
        return False

    # 輔助方法:合併後續同種藥材批次
    def _merge_subsequent_batches(self, start_node, herb_name, remaining_need):
        current = start_node.next
        while current and remaining_need > 0:
            if current.herb_name == herb_name:
                if current.quantity >= remaining_need:
                    current.quantity -= remaining_need
                    print(f"出庫成功:從批次{current.batch_id}消耗{remaining_need}{herb_name}")
                    if current.quantity == 0:  # 該批次已耗盡
                        start_node.next = current.next
                    remaining_need = 0
                    return True
                else:
                    remaining_need -= current.quantity
                    start_node.next = current.next  # 跳過已耗盡批次
                    current = start_node.next
            else:
                current = current.next
        return remaining_need == 0

    # 打印當前庫存狀態(按入庫順序)
    def print_inventory(self):
        if not self.head:
            print("庫存為空")
            return

        print("當前庫存批次(按入庫順序):")
        current = self.head
        while current:
            print(f"批次ID: {current.batch_id}, 藥材: {current.herb_name}, "
                  f"入庫日期: {current.inbound_date}, 剩餘數量: {current.quantity}")
            current = current.next

# ==================== 使用方法 ====================
if __name__ == "__main__":
    warehouse = HerbInventory()

    # 入庫操作(按時間順序,批號優先測試,暫不考慮產地,規格及其他信息)
    warehouse.add_batch("Lot-d2025001", "當歸", "2025-01-15", 500)
    warehouse.add_batch("Lot-h2025002", "黃芪", "2025-02-20", 250)
    warehouse.add_batch("Lot-x2025003", "西洋參", "2025-03-10", 200)
    warehouse.add_batch("Lot-r2025004", "人蔘", "2025-04-05", 100)

    print("\n🚀初始庫存狀態,單位:克")
    warehouse.print_inventory()

    # 抓藥操作
    print("\n--- 抓藥操作1: 從當歸批次消耗100克 ---")
    warehouse.dispense_herb("當歸", 100)  # 500-100

    print("--- 抓藥操作2: 從當歸批次消耗150克 ---")
    warehouse.dispense_herb("黃芪", 150)  # 250-150

    print("--- 抓藥操作3: 從黃芪批次消耗60克 ---")
    warehouse.dispense_herb("西洋參", 60)  # 200-60

    print("--- 抓藥操作4: 從人蔘批次消耗30克 ---")
    warehouse.dispense_herb("人蔘", 30)  # 100-30

    print("\n🧭最終庫存狀態,單位:克")
    warehouse.print_inventory()
        

Python注意縮進💎💎
Python鏈表藥材管理
nodejs--------------------------------------------------

// ==================== 中藥單鏈模塊 ====================
// 單向藥性傳導  // 靈氣只能順流而下的仙脈 🌊
// ⚠️虎山老藥師:仙脈逆流會引發丹爐爆炸

// 定義庫存批次節點
class BatchNode {
    constructor(batch_id, herb_name, inbound_date, quantity) {
        this.batch_id = batch_id;      // 批次ID
        this.herb_name = herb_name;    // 藥材名稱
        this.inbound_date = inbound_date;  // 入庫日期(字符串格式"YYYY-MM-DD")
        this.quantity = quantity;      // 剩餘數量
        this.next = null;              // 下一個批次指針
    }
}

// 定義藥材庫存類
class HerbInventory {
    constructor() {
        this.head = null;  // 鏈表頭節點(最早入庫的批次)
    }

    // 新批次入庫(添加到鏈表尾部)
    addBatch(batch_id, herb_name, inbound_date, quantity) {
        const newNode = new BatchNode(batch_id, herb_name, inbound_date, quantity);
        if (!this.head) {  // 空鏈表情況
            this.head = newNode;
            return;
        }

        let current = this.head;
        while (current.next) {  // 遍歷到最後一個節點
            current = current.next;
        }
        current.next = newNode;  // 新批次添加到尾部
    }

    // 藥材出庫(從最早批次開始消耗)
    dispenseHerb(herb_name, required_quantity) {
        if (!this.head) {
            console.log(`錯誤:庫存中沒有${herb_name}的任何批次`);
            return false;
        }

        // 特殊情況:頭節點就是目標藥材且數量足夠
        if (this.head.herb_name === herb_name && this.head.quantity >= required_quantity) {
            this.head.quantity -= required_quantity;
            console.log(`出庫成功:從批次${this.head.batch_id}消耗${required_quantity}${herb_name}`);
            if (this.head.quantity === 0) {  // 該批次已耗盡
                this.head = this.head.next;
            }
            return true;
        }

        // 一般情況:需要遍歷鏈表
        let prev = null;
        let current = this.head;
        while (current) {
            if (current.herb_name === herb_name) {
                if (current.quantity >= required_quantity) {
                    current.quantity -= required_quantity;
                    console.log(`出庫成功:從批次${current.batch_id}消耗${required_quantity}${herb_name}`);
                    if (current.quantity === 0) {  // 該批次已耗盡
                        if (prev) {  // 不是頭節點
                            prev.next = current.next;
                        } else {  // 是頭節點
                            this.head = current.next;
                        }
                    }
                    return true;
                } else {
                    // 當前批次數量不足,嘗試合併後續批次
                    const remainingNeed = required_quantity - current.quantity;
                    if (this._mergeSubsequentBatches(current, herb_name, remainingNeed)) {
                        return true;
                    } else {
                        console.log(`錯誤:庫存不足,無法滿足${required_quantity}${herb_name}的需求`);
                        return false;
                    }
                }
            }
            prev = current;
            current = current.next;
        }

        console.log(`錯誤:庫存中沒有${herb_name}的批次`);
        return false;
    }

    // 輔助方法:合併後續同種藥材批次
    _mergeSubsequentBatches(startNode, herb_name, remainingNeed) {
        let current = startNode.next;
        while (current && remainingNeed > 0) {
            if (current.herb_name === herb_name) {
                if (current.quantity >= remainingNeed) {
                    current.quantity -= remainingNeed;
                    console.log(`出庫成功:從批次${current.batch_id}消耗${remainingNeed}${herb_name}`);
                    if (current.quantity === 0) {  // 該批次已耗盡
                        startNode.next = current.next;
                    }
                    remainingNeed = 0;
                    return true;
                } else {
                    remainingNeed -= current.quantity;
                    startNode.next = current.next;  // 跳過已耗盡批次
                    current = startNode.next;
                }
            } else {
                current = current.next;
            }
        }
        return remainingNeed === 0;
    }

    // 打印當前庫存狀態(按入庫順序)
    printInventory() {
        if (!this.head) {
            console.log("庫存為空");
            return;
        }

        console.log("當前庫存批次(按入庫順序):");
        let current = this.head;
        while (current) {
            console.log(`批次ID: ${current.batch_id}, 藥材: ${current.herb_name}, ` +
                `入庫日期: ${current.inbound_date}, 剩餘數量: ${current.quantity}`);
            current = current.next;
        }
    }
}

// ==================== 使用方法 ====================
const warehouse = new HerbInventory();

// 入庫操作(按時間順序,批號優先測試,暫不考慮產地,規格及其他信息)
warehouse.addBatch("Lot-d2025001", "當歸", "2025-01-15", 500);
warehouse.addBatch("Lot-h2025002", "黃芪", "2025-02-20", 250);
warehouse.addBatch("Lot-x2025003", "西洋參", "2025-03-10", 200);
warehouse.addBatch("Lot-r2025004", "人蔘", "2025-04-05", 100);

console.log("\n🚀初始庫存狀態,單位:克");
warehouse.printInventory();

// 抓藥操作
console.log("\n--- 抓藥操作1: 從當歸批次消耗100克 ---");
warehouse.dispenseHerb("當歸", 100);  // 500-100

console.log("--- 抓藥操作2: 從當歸批次消耗150克 ---");
warehouse.dispenseHerb("黃芪", 150);  // 250-150

console.log("--- 抓藥操作3: 從黃芪批次消耗60克 ---");
warehouse.dispenseHerb("西洋參", 60);  // 200-60

console.log("--- 抓藥操作4: 從人蔘批次消耗30克 ---");
warehouse.dispenseHerb("人蔘", 30);  // 100-30

console.log("\n🧭最終庫存狀態,單位:克");
warehouse.printInventory();
        

nodejs藥材批次管理

Go--------------------------------------------------------------

package main

import "fmt"

// ==================== 倉儲單鏈模塊 ====================
// 單向走斯路徑  // 貨品只能單向流動的量子隧道 🚇
// ⚠️冷溪物流:隧道坍塌會導致貨物堆積

// 定義庫存批次節點
type BatchNode struct {
    batch_id     string     // 批次ID
    herb_name    string     // 藥材名稱
    inbound_date string     // 入庫日期(字符串格式"YYYY-MM-DD")
    quantity     int        // 剩餘數量
    next         *BatchNode // 下一個批次指針
}

// 定義藥材庫存類
type HerbInventory struct {
    head *BatchNode // 鏈表頭節點(最早入庫的批次)
}

// 新批次入庫(添加到鏈表尾部)
func (hi *HerbInventory) addBatch(batch_id, herb_name, inbound_date string, quantity int) {
    newNode := &BatchNode{batch_id: batch_id, herb_name: herb_name, inbound_date: inbound_date, quantity: quantity, next: nil}
    if hi.head == nil { // 空鏈表情況
        hi.head = newNode
        return
    }

    current := hi.head
    for current.next != nil { // 遍歷到最後一個節點
        current = current.next
    }
    current.next = newNode // 新批次添加到尾部
}

// 藥材出庫(從最早批次開始消耗)
func (hi *HerbInventory) dispenseHerb(herb_name string, required_quantity int) bool {
    if hi.head == nil {
        fmt.Printf("錯誤:庫存中沒有%s的任何批次\n", herb_name)
        return false
    }

    // 特殊情況:頭節點就是目標藥材且數量足夠
    if hi.head.herb_name == herb_name && hi.head.quantity >= required_quantity {
        hi.head.quantity -= required_quantity
        fmt.Printf("出庫成功:從批次%s消耗%d%s\n", hi.head.batch_id, required_quantity, herb_name)
        if hi.head.quantity == 0 { // 該批次已耗盡
            hi.head = hi.head.next
        }
        return true
    }

    // 一般情況:需要遍歷鏈表
    var prev *BatchNode = nil
    current := hi.head
    for current != nil {
        if current.herb_name == herb_name {
            if current.quantity >= required_quantity {
                current.quantity -= required_quantity
                fmt.Printf("出庫成功:從批次%s消耗%d%s\n", current.batch_id, required_quantity, herb_name)
                if current.quantity == 0 { // 該批次已耗盡
                    if prev != nil { // 不是頭節點
                        prev.next = current.next
                    } else { // 是頭節點
                        hi.head = current.next
                    }
                }
                return true
            } else {
                // 當前批次數量不足,嘗試合併後續批次
                remainingNeed := required_quantity - current.quantity
                if hi.mergeSubsequentBatches(current, herb_name, remainingNeed) {
                    return true
                } else {
                    fmt.Printf("錯誤:庫存不足,無法滿足%d%s的需求\n", required_quantity, herb_name)
                    return false
                }
            }
        }
        prev = current
        current = current.next
    }

    fmt.Printf("錯誤:庫存中沒有%s的批次\n", herb_name)
    return false
}

// 輔助方法:合併後續同種藥材批次
func (hi *HerbInventory) mergeSubsequentBatches(startNode *BatchNode, herb_name string, remainingNeed int) bool {
    current := startNode.next
    for current != nil && remainingNeed > 0 {
        if current.herb_name == herb_name {
            if current.quantity >= remainingNeed {
                current.quantity -= remainingNeed
                fmt.Printf("出庫成功:從批次%s消耗%d%s\n", current.batch_id, remainingNeed, herb_name)
                if current.quantity == 0 { // 該批次已耗盡
                    startNode.next = current.next
                }
                remainingNeed = 0
                return true
            } else {
                remainingNeed -= current.quantity
                startNode.next = current.next // 跳過已耗盡批次
                current = startNode.next
            }
        } else {
            current = current.next
        }
    }
    return remainingNeed == 0
}

// 打印當前庫存狀態(按入庫順序)
func (hi *HerbInventory) printInventory() {
    if hi.head == nil {
        fmt.Println("庫存為空")
        return
    }

    fmt.Println("當前庫存批次(按入庫順序):")
    current := hi.head
    for current != nil {
        fmt.Printf("批次ID: %s, 藥材: %s, 入庫日期: %s, 剩餘數量: %d\n",
            current.batch_id, current.herb_name, current.inbound_date, current.quantity)
        current = current.next
    }
}

// 使用示例
func main() {
    warehouse := &HerbInventory{}

    // 入庫操作(按時間順序,批號優先測試,暫不考慮產地,規格及其他信息)
    warehouse.addBatch("Lot-d2025001", "當歸", "2025-01-15", 500)
    warehouse.addBatch("Lot-h2025002", "黃芪", "2025-02-20", 250)
    warehouse.addBatch("Lot-x2025003", "西洋參", "2025-03-10", 200)
    warehouse.addBatch("Lot-r2025004", "人蔘", "2025-04-05", 100)

    fmt.Println("\n🚀初始庫存狀態,單位:克")
    warehouse.printInventory()

    // 抓藥操作
    fmt.Println("\n--- 抓藥操作1: 從當歸批次消耗100克 ---")
    warehouse.dispenseHerb("當歸", 100) // 500-100

    fmt.Println("--- 抓藥操作2: 從當歸批次消耗150克 ---")
    warehouse.dispenseHerb("黃芪", 150) // 250-150

    fmt.Println("--- 抓藥操作3: 從黃芪批次消耗60克 ---")
    warehouse.dispenseHerb("西洋參", 60) // 200-60

    fmt.Println("--- 抓藥操作4: 從人蔘批次消耗30克 ---")
    warehouse.dispenseHerb("人蔘", 30) // 100-30

    fmt.Println("\n🧭最終庫存狀態,單位:克")
    warehouse.printInventory()
}

Go藥材批次管理
Java-------------------------------------------------------------

// ==================== ERP單鏈模塊 ====================
// 單向數據流  // 信息只能向前傳遞的內存通道 💾
// ⚠️ERP老兵_冷溪虎山:通道阻塞會引發數據洪災

// 定義庫存批次節點
class BatchNode {
    String batch_id;      // 批次ID
    String herb_name;     // 藥材名稱
    String inbound_date;  // 入庫日期(字符串格式"YYYY-MM-DD")
    int quantity;         // 剩餘數量
    BatchNode next;       // 下一個批次指針

    // 構造函數
    public BatchNode(String batch_id, String herb_name, String inbound_date, int quantity) {
        this.batch_id = batch_id;
        this.herb_name = herb_name;
        this.inbound_date = inbound_date;
        this.quantity = quantity;
        this.next = null;
    }
}

// 定義藥材庫存類
class HerbInventory {
    BatchNode head;  // 鏈表頭節點(最早入庫的批次)

    // 構造函數
    public HerbInventory() {
        this.head = null;
    }

    // 新批次入庫(添加到鏈表尾部)
    public void addBatch(String batch_id, String herb_name, String inbound_date, int quantity) {
        BatchNode newNode = new BatchNode(batch_id, herb_name, inbound_date, quantity);
        if (head == null) {  // 空鏈表情況
            head = newNode;
            return;
        }

        BatchNode current = head;
        while (current.next != null) {  // 遍歷到最後一個節點
            current = current.next;
        }
        current.next = newNode;  // 新批次添加到尾部
    }

    // 藥材出庫(從最早批次開始消耗)
    public boolean dispenseHerb(String herb_name, int required_quantity) {
        if (head == null) {
            System.out.println("錯誤:庫存中沒有" + herb_name + "的任何批次");
            return false;
        }

        // 特殊情況:頭節點就是目標藥材且數量足夠
        if (head.herb_name.equals(herb_name) && head.quantity >= required_quantity) {
            head.quantity -= required_quantity;
            System.out.println("出庫成功:從批次" + head.batch_id + "消耗" + required_quantity + herb_name);
            if (head.quantity == 0) {  // 該批次已耗盡
                head = head.next;
            }
            return true;
        }

        // 一般情況:需要遍歷鏈表
        BatchNode prev = null;
        BatchNode current = head;
        while (current != null) {
            if (current.herb_name.equals(herb_name)) {
                if (current.quantity >= required_quantity) {
                    current.quantity -= required_quantity;
                    System.out.println("出庫成功:從批次" + current.batch_id + "消耗" + required_quantity + herb_name);
                    if (current.quantity == 0) {  // 該批次已耗盡
                        if (prev != null) {  // 不是頭節點
                            prev.next = current.next;
                        } else {  // 是頭節點
                            head = current.next;
                        }
                    }
                    return true;
                } else {
                    // 當前批次數量不足,嘗試合併後續批次
                    int remainingNeed = required_quantity - current.quantity;
                    if (mergeSubsequentBatches(current, herb_name, remainingNeed)) {
                        return true;
                    } else {
                        System.out.println("錯誤:庫存不足,無法滿足" + required_quantity + herb_name + "的需求");
                        return false;
                    }
                }
            }
            prev = current;
            current = current.next;
        }

        System.out.println("錯誤:庫存中沒有" + herb_name + "的批次");
        return false;
    }

    // 輔助方法:合併後續同種藥材批次
    private boolean mergeSubsequentBatches(BatchNode startNode, String herb_name, int remainingNeed) {
        BatchNode current = startNode.next;
        while (current != null && remainingNeed > 0) {
            if (current.herb_name.equals(herb_name)) {
                if (current.quantity >= remainingNeed) {
                    current.quantity -= remainingNeed;
                    System.out.println("出庫成功:從批次" + current.batch_id + "消耗" + remainingNeed + herb_name);
                    if (current.quantity == 0) {  // 該批次已耗盡
                        startNode.next = current.next;
                    }
                    remainingNeed = 0;
                    return true;
                } else {
                    remainingNeed -= current.quantity;
                    startNode.next = current.next;  // 跳過已耗盡批次
                    current = startNode.next;
                }
            } else {
                current = current.next;
            }
        }
        return remainingNeed == 0;
    }

    // 打印當前庫存狀態(按入庫順序)
    public void printInventory() {
        if (head == null) {
            System.out.println("庫存為空");
            return;
        }

        System.out.println("當前庫存批次(按入庫順序):");
        BatchNode current = head;
        while (current != null) {
            System.out.println("批次ID: " + current.batch_id + ", 藥材: " + current.herb_name + ", " +
                    "入庫日期: " + current.inbound_date + ", 剩餘數量: " + current.quantity);
            current = current.next;
        }
    }
}

// 使用示例
class main151 {
    public static void main(String[] args) {
        HerbInventory warehouse = new HerbInventory();

        // 入庫操作(按時間順序,批號優先測試,暫不考慮產地,規格及其他信息)
        warehouse.addBatch("Lot-d2025001", "當歸", "2025-01-15", 500);
        warehouse.addBatch("Lot-h2025002", "黃芪", "2025-02-20", 250);
        warehouse.addBatch("Lot-x2025003", "西洋參", "2025-03-10", 200);
        warehouse.addBatch("Lot-r2025004", "人蔘", "2025-04-05", 100);

        System.out.println("\n🚀初始庫存狀態,單位:克");
        warehouse.printInventory();

        // 抓藥操作
        System.out.println("\n--- 抓藥操作1: 從當歸批次消耗100克 ---");
        warehouse.dispenseHerb("當歸", 100);  // 500-100

        System.out.println("--- 抓藥操作2: 從當歸批次消耗150克 ---");
        warehouse.dispenseHerb("黃芪", 150);  // 250-150

        System.out.println("--- 抓藥操作3: 從黃芪批次消耗60克 ---");
        warehouse.dispenseHerb("西洋參", 60);  // 200-60

        System.out.println("--- 抓藥操作4: 從人蔘批次消耗30克 ---");
        warehouse.dispenseHerb("人蔘", 30);  // 100-30

        System.out.println("\n🧭最終庫存狀態,單位:克");
        warehouse.printInventory();
    }
}
   

Java藥品批次管理

以上就是四語言截圖和編碼---------------------------------------✅✅✅✅✅✅

🚨 怪蜀黎野路子宣言

**“別再看那些紙上談兵的教程了!咱的代碼:

  • 能直接塞進藥廠ERP系統跑生產環境
  • 用了中醫靈氣流動比喻內存指針(頭節點=百會穴,next指針=任脈傳導)
  • 開源4語言對照版,讓你學一次掌握四種語法精髓”,學透單鏈表本質

💡 怪蜀黎提示

  • 直接複製代碼跑通單鏈表基礎操作
  • 修改dispenseHerb()方法適配你的業務邏輯(如快遞倉儲/服務器資源調度)
  • 中醫黑話註釋僅供娛樂,技術核心是FIFO鏈式管理

👉 代碼已就緒,剩下交給你的想象力!
(冷溪虎山數字本草實驗室 © 2025 | 技術交流禁醫療用途)

🔮 下一步行動

  1. 立馬白嫖 → GitHub倉庫 Star後直接clone運行 (預告)
  2. 延伸學習 → 關注我的專欄《中醫編程宇宙》系列(已爆11/14篇)
  3. 挑戰升級 → 在評論區留下你想看的下個實戰場景(如:用紅黑樹管理熱門冷背藥材

💊 合規聲明
本代碼完全模擬GMP規範設計,實際藥廠應用需通過藥監局驗收。但代碼裏的審計追蹤設計,能治你的“系統漏洞”和“數據淤堵”。

💊 免責聲明
本代碼僅技術演示,實際用藥請遵醫囑。但代碼裏的中醫哲學,能治你的“知識虛火”和“學習淤堵”。

⚠️ 免責聲明(附因果律警告)

本代碼已注入中醫玄學能量,請謹慎使用:

  • ✅ 允許白嫖,但白嫖不點贊可能導致:

    • 下次面試官恰好問到這個算法
    • 鍵盤自動打出//這裏感謝冷溪虎山老中醫
    • 奶茶精準灑在剛寫好的代碼上
  • ✅ 允許商用,但商用不註明出處可能觸發:

    • 產品上線前夜突然出現遞歸棧溢出
    • 數據庫莫名存儲君臣佐使字段
  • ✅ 允許吐槽,但吐槽不帶改進建議可能引發:

    • 終生與邊界條件相愛相殺

🚀 現在立即行動:

  1. 點贊 → 吸收本篇算法精華+怪蜀黎腦洞思維
  2. 收藏 → 避免日後求醫無門
  3. 關注 → 接收更多「中醫+代碼」腦洞
  4. 評論區留言 → 領取你的專屬「算法藥方」

如有不對之處,歡迎評論區批評指出或者留言給我!✅✅

如果這份文章幫到了你,請點贊、收藏、關注三連!你們的支持,就是我繼續‘煉丹’的動力🏆🏆!

user avatar savokiss 头像 xiaolei_599661330c0cb 头像 yian 头像 b_a_r_a_n 头像 artificer 头像 yangy5hqv 头像 lixingning 头像 fsjohnhuang 头像 ohaha 头像 kaiwudb 头像 liu_chen 头像 horizondeveloper 头像
点赞 27 用户, 点赞了这篇动态!
点赞

Add a new 评论

Some HTML is okay.