Pauls awesome Stack of death

Paul.Esson 0 Tallied Votes 148 Views Share

My second C++ lab, bit of fun if i do say so my self.

Its a stack that uses a linked list in C++ alot of debugging stuff still in here, but I like the debugging output anyway.

Sorry about the lack of comments and

-------------------------------------------------stack.h-----------------------------------------

#include <iostream.h>

typedef struct TAG_NODE {
        int data;
        TAG_NODE* NOS;  //Next on stack
} node;

class Stack{
private:
        node* TOS;
public:
        Stack();

        int pop();

        void push(int myData);
};

-------------------------------------------------stack.cpp--------------------------------------

/*
* Pauls Great Stack of C++
*/

#include "stack.h"

/*
* Stack constuctor.
*/

Stack::Stack() {
        //Set the top of stack to NULL
        this->TOS = NULL;
}

int Stack::pop(){
        int data;
        if (TOS != NULL){
                // Node to temporaly store the TOS
                node* currentTop;
                currentTop = TOS;               // get the current TOS for returning                                                  
                data = currentTop->data;        //Get the data in TOS
                cout << "Data on TOS: "<< data;
                TOS = currentTop->NOS;  // Make current tos = the next on the stack
                cout << " Current TOS: "  << TOS << endl;
                //Free What is currently on the stack
                free(currentTop);
        }
        else data = -1;                         //Change this to null
        return data;                            //Return the value at TOS
}

void Stack::push(int myData){
        node *newNode = (node*)malloc(sizeof(node));
        cout << "Creating New Node at " << newNode << " with data : ";
        newNode->data = myData;
        cout << newNode->data << " and Next On Stack :" ;
        newNode->NOS = TOS;             // meke a new node
        cout << newNode->NOS << endl;
        TOS = newNode;              // make TOS = the new node
}

------------------------------------------stacklab2.cpp-------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include "stack.h"
//#include <sstream>                                  

int main(){
        Stack *myStack = new Stack();
        int i=0;
        cout << "pushing" << endl;
        while( i < 1000000000)
        {
                myStack->push(i);
                i++;
        }

        cout << "popping" <<endl;

        i = 0;
        while ( i < 9){
                myStack->pop();
                i++;
        }
        while ( i == 9 ){}   //infinate loop :D
        return 1;
}

----------------------------------------------To Compile-------------------------------------

g++ stack.cpp stacklab2.cpp