Header file
/*** function: Lower push stack (push_down stack) No more memory * Time: August 18, 2014 08:13:36* Author: cutter_point*/#ifndef stack_h_included#define Stack_h_ The includedstruct stack{ struct link { void* data;//This structure is link* next;//this pointer to this structure void Initialize (void* dat, link* nxt); Initialize this structure, and the elements and next point to }*head; void Initialize (); void push (void* dat); void* Peek (); Returns the element at the top of the stack, but does not delete the element void* pop (); void Cleanup ();}; #endif//stack_h_included
Definition file
/*** function: The lower push stack (Push_down stack) will not be out of memory, define * Time: August 18, 2014 08:12:07* Author: cutter_point*/#include "Stack.h" #include ". /require.h "using namespace std;//struct link inside the frontal function//void Initialize (void* dat, link* NXT) Initializes this structure, and the elements and next point to void Stack::link::initialize (void* dat, link* nxt) { data=dat; NEXT=NXT;} function inside Stack, initialize function// void Initialize (), void Stack::initialize () {head=nullptr;} void push (void* dat), void Stack::p ush (void* dat) { link* newlink=new Link; Newlink->initialize (DAT, head); This node is connected to the front head=newlink of the head junction ; Reset Head junction}// void* Peek (); Returns the element at the top of the stack, but does not delete the element void* stack::p eek () { require (head! = 0, "Stack empty"); return head->data;} void* pop (); void* Stack::p op () { if (head = = nullptr) return 0; void* result=head->data; link* Oldhead=head; head=head->next; Delete Oldhead; return result;} void Cleanup (), void Stack::cleanup () { require (head = = nullptr, "Stack not Empty");}
Test file
/*** function: Push stack (Push_down stack) will not be more memory, definition, test * time: August 18, 2014 08:13:31* Author: cutter_point*/#include "Stack.h" #include " Stack.cpp "#include". /require.h " //This header file is directly given, not written by yourself, will not be used #include<fstream> #include <iostream> #include <string> Using namespace Std;int main (int argc, char* argv[]) //argv[1] The path name of the program { //requireargs (argc, 1);//This has interference effect / /ifstream in (argv[1]); ARGV[1] The path name of the program, this does not seem to work here Ifstream in ("StackTest.cpp"); ARGV[1] Program path name //assure (in, argv[1]); Comment out, but can run Stack textlines; Textlines.initialize (); string line; Extract string while (Getline (in), line) Textlines.push (new string); In the linked list //Take out the output, reverse output string* s; while ((s= (string*) Textlines.pop ())! = 0) { cout<<*s<<endl; Delete s; } Textlines.cleanup (); return 0;}