下推棧實現(c++編程思想 p136)

來源:互聯網
上載者:User

標籤:

1 標頭檔Stack.h

 1 #ifndef STACK_H 2 #define STACK_H 3  4 struct Stack 5 { 6  7     struct Link 8     { 9         void* data;10         Link* next;11         void initialize(void* dat, Link* nxt);12     }* head;13 14     void initialize();15     void push(void* dat);16     void* peek();17     void* pop();18     void cleanup();19 };20 21 #endif // STACK_H

 

2 實現檔案Stack.cpp

 1 #include "Stack.h" 2 #include "../require.h" 3  4 using namespace std; 5  6 void Stack::Link::initialize(void* dat, Link* nxt) 7 { 8     data = dat; 9     next = nxt;10 }11 12 void Stack::initialize()13 {14     head = 0;15 }16 17 void Stack::push(void* dat)18 {19     Link *newLink = new Link;20     newLink->initialize(dat, head);21     head = newLink;22 }23 24 void* Stack::peek()25 {26     require(head !=0, "Stack empty");27     return head->data;28 }29 30 31 void* Stack::pop()32 {33     if (head == 0)34         return 0;35     void* result = head->data;36     Link* oldHead = head;37     head = head->next;38     delete oldHead;39     return result;40 }41 42 43 void Stack::cleanup()44 {45     require(head == 0, "Stack not empty");46 }

 

3 測試檔案main.cpp

 1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 #include "Stack.h" 5 #include "../require.h" 6  7 using namespace std; 8  9 int main(int argc, char* argv[])10 {11 12     requireArgs(argc, 1);13 14     ifstream in(argv[1]);15     assure(in, argv[1]);16 17     Stack textLines;18     textLines.initialize();19 20     string line;21     while (getline(in, line)) //檔案以行為單位入棧22         textLines.push(new string(line));23 24     string* s;25     while ((s = (string*) textLines.pop()) != 0) //出棧26     {27         cout << *s << endl;28         delete s;29     }30 31     textLines.cleanup();32     return 0;33 }

 

4 運行分析

 

附 輔助測試載入器require.h 

 1 #ifndef REQUIRE_H 2 #define REQUIRE_H 3  4 #include <cstdio> 5 #include <cstdlib> 6 #include <fstream> 7 #include <string> 8  9 inline void require(bool requirement, const std::string& msg = "Requirement failed")10 {11   using namespace std;12   if (!requirement)13   {14     fputs(msg.c_str(), stderr);15     fputs("\n", stderr);16     exit(1);17   }18 }19 20 inline void requireArgs(int argc, int args, const std::string& msg = "Must use %d arguments")21 {22   using namespace std;23    if (argc != args + 1)24    {25      fprintf(stderr, msg.c_str(), args);26      fputs("\n", stderr);27      exit(1);28    }29 }30 31 inline void requireMinArgs(int argc, int minArgs, const std::string& msg ="Must use at least %d arguments")32 {33   using namespace std;34   if(argc < minArgs + 1)35   {36     fprintf(stderr, msg.c_str(), minArgs);37     fputs("\n", stderr);38     exit(1);39   }40 }41 42 inline void assure(std::ifstream& in, const std::string& filename = "")43 {44   using namespace std;45   if(!in)46   {47     fprintf(stderr, "Could not open file %s\n",48       filename.c_str());49     exit(1);50   }51 }52 53 inline void assure(std::ofstream& out, const std::string& filename = "")54 {55   using namespace std;56   if(!out) {57     fprintf(stderr, "Could not open file %s\n",58       filename.c_str());59     exit(1);60   }61 }62 63 64 #endif // REQUIRE_H

 

下推棧實現(c++編程思想 p136)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.