日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LinkStack

發(fā)布時間:2025/4/5 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LinkStack 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 1 LinkStack的實現(xiàn)
      • 1.1 StaticStack的缺陷
      • 1.2 鏈式棧的存儲實現(xiàn)
      • 1.3 鏈式棧的設(shè)計要點
      • 1.4 繼承關(guān)系圖
    • 2 代碼實現(xiàn)
    • 3 棧的應(yīng)用實踐
    • 4 小結(jié)

1 LinkStack的實現(xiàn)

1.1 StaticStack的缺陷

由于StaticStack內(nèi)部使用了原聲數(shù)組,當存儲的元素為類類型時,StaticStack的對象在創(chuàng)建時,會多次調(diào)用元素類型的構(gòu)造函數(shù),影響效率。因此,我們需要鏈式棧來避免這種缺陷。

1.2 鏈式棧的存儲實現(xiàn)

1.3 鏈式棧的設(shè)計要點

  • 類模板,抽象父類Stack的直接子類。
  • 在內(nèi)部組合使用LinkList類,實現(xiàn)棧的鏈式存儲。
  • 只在單鏈表成員對象的頭部進行操作。

1.4 繼承關(guān)系圖


2 代碼實現(xiàn)

#ifndef LINKSTACK_H #define LINKSTACK_H#include "Stack.h" #include "LinkList.h" #include "Exception.h"namespace LemonLib { template <typename T> class LinkStack : Stack<T> { protected:LinkList<T> m_list;public:void push(const T& e){m_list.insert(0, e);}void pop(){if (m_list.length() > 0){m_list.remove(0);}else{THROW_EXCEPTION(InvalidOperationException, "No element in current stack ...");}}T top() const{if (m_list.length() > 0){return m_list.get(0);}else{THROW_EXCEPTION(InvalidOperationException, "No element in current stack ...");}}void clear(){m_list.clear();}int size() const{return m_list.length();} }; }#endif // LINKSTACK_H

3 棧的應(yīng)用實踐

符號匹配問題:
在C語言中有一些成對匹配出現(xiàn)的符號:

  • 括號:(),[],{},<>
  • 引號:’’,""

那么我們?nèi)绾螌崿F(xiàn)編譯器中的符號成對檢測呢?
算法思路:

  • 從第一個字符開始掃描

    • 當遇見普通字符時忽略
    • 當遇見左符號時壓入棧中
    • 當遇見右符號時彈出棧頂符號,并進行匹配
  • 結(jié)束

    • 成功:所有字符掃描完畢,并且棧為空
    • 失敗:匹配失敗或所有字符掃描完畢但棧非空
#include <iostream> #include "LinkStack.h"using namespace std; using namespace LemonLib;bool is_left(char c) {return ((c == '{') || (c == '(') || (c == '[') || (c == '<')); }bool is_right(char c) {return ((c == '}') || (c == ')') || (c == ']') || (c == '>')); }bool is_quote(char c) {return ((c == '\'') || (c == '\"')); }bool is_match(char l, char r) {return ((l == '{') && (r == '}')) ||((l == '(') && (r == ')')) ||((l == '[') && (r == ']')) ||((l == '<') && (r == '>')) ||((l == '\'') && (r == '\'')) ||((l == '\"') && (r == '\"')); }bool scan(const char* str) {bool ret = true;LinkStack<char> stack;int i = 0;str = (str == NULL) ? "" : str;while (ret && (str[i] != '\0')){if (is_left(str[i])){stack.push(str[i]);}else if (is_right(str[i])){if ((stack.size() > 0) && is_match(stack.top(), str[i])){stack.pop();}else{ret = false;}}else if (is_quote(str[i])){if ((stack.size() > 0) && (is_match(stack.top(), str[i]))){stack.pop();}else{stack.push(str[i]);}}i++;}return (ret && (stack.size() == 0)); }int main() {cout << scan("<a{b(\'x\')c}d>") << endl;return 0; }

4 小結(jié)

  • 鏈式棧的實現(xiàn)組合使用了單鏈表對象。
  • 在單鏈表的頭部進行操作能夠?qū)崿F(xiàn)高效的入棧和出棧操作。
  • 棧“后近先出”的特性適用于檢測成對出現(xiàn)的符號。
  • 棧非常適合于需要“就近匹配”的場合。

總結(jié)

以上是生活随笔為你收集整理的LinkStack的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。