"Thinkinginc++" 26, Push Stack (push_down stack) no more memory

Source: Internet
Author: User

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;}








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.