Push-down stack implementation (C + + programming thought p136)

Source: Internet
Author: User

1 Head Files Stack.h

1 #ifndef Stack_h2 #defineStack_h3 4 structStack5 {6 7     structLink8     {9         void*data;Tenlink*Next; One         voidInitializevoid* Dat, link*NXT); A}*head; -  -     voidInitialize (); the     voidPushvoid*dat); -     void*Peek (); -     void*pop (); -     voidCleanup (); + }; -  + #endif //Stack_h

2 Implementing File Stack.cpp

1#include"Stack.h"2#include".. /require.h"3 4 using namespacestd;5 6 voidStack::link::initialize (void* Dat, link*NXT)7 {8data =dat;9Next =NXT;Ten } One  A voidstack::initialize () - { -Head =0; the } -  - voidStack::p Ush (void*dat) - { +Link *newlink =NewLink; -Newlink->Initialize (DAT, head); +Head =NewLink; A } at  - void*Stack::p eek () - { -Require (head! =0,"Stack Empty"); -     returnHead->data; - } in  -  to void*Stack::p op () + { -     if(Head = =0) the         return 0; *     void* result = Head->data; $link* Oldhead =head;Panax NotoginsengHead = head->Next; -     DeleteOldhead; the     returnresult; + } A  the  + voidStack::cleanup () - { $require (head = =0,"Stack not empty"); $}

3 Test File Main.cpp

1#include <iostream>2#include <fstream>3#include <string>4#include"Stack.h"5#include".. /require.h"6 7 using namespacestd;8 9 intMainintargcChar*argv[])Ten { One  ARequireargs (ARGC,1); -  -Ifstreaminch(argv[1]); theAssureinch, argv[1]); -  - Stack Textlines; - textlines.initialize (); +  -     stringLine ; +      while(Getline (inch, line)) The file is in the stack in the behavior unit ATextlines.push (New string(line)); at  -     string*s; -      while((s = (string*) Textlines.pop ())! =0)//out of the stack -     { -cout << *s <<Endl; -         Deletes; in     } -  to Textlines.cleanup (); +     return 0; -}

4 Run Analysis

Auxiliary Test Tool Require.h

1 #ifndef Require_h2 #defineRequire_h3 4#include <cstdio>5#include <cstdlib>6#include <fstream>7#include <string>8 9InlinevoidRequireBOOLRequirement,ConstSTD::string& msg ="Requirement failed")Ten { One   using namespacestd; A   if(!requirement) -   { - fputs (Msg.c_str (), stderr); theFputs"\ n", stderr); -Exit1); -   } - } +  -InlinevoidRequireargs (intargcintArgsConstSTD::string& msg ="must use%d arguments") + { A   using namespacestd; at    if(argc! = args +1) -    { - fprintf (stderr, Msg.c_str (), args); -Fputs"\ n", stderr); -Exit1); -    } in } -  toInlinevoidRequireminargs (intargcintMinargs,ConstSTD::string& msg ="must use at least%d arguments") + { -   using namespacestd; the   if(ARGC < Minargs +1) *   { $ fprintf (stderr, Msg.c_str (), Minargs);Panax NotoginsengFputs"\ n", stderr); -Exit1); the   } + } A  theInlinevoidAssure (std::ifstream&inch,ConstSTD::string& filename ="") + { -   using namespacestd; $   if(!inch) $   { -fprintf (stderr,"Could not open file%s\n", - filename.c_str ()); theExit1); -   }Wuyi } the  -InlinevoidAssure (std::ofstream& out,ConstSTD::string& filename ="") Wu { -   using namespacestd; About   if(! out) { $fprintf (stderr,"Could not open file%s\n", - filename.c_str ()); -Exit1); -   } A } +  the  - #endif //Require_h

Push-down stack implementation (C + + programming thought p136)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.