简体   繁体   中英

Implementation of stack in C++ without using <stack>

I want to make an implementation of stack, I found a working model on the internet, unfortunately it is based on the idea that I know the size of the stack I want to implement right away. What I want to do is be able to add segments to my stack as they are needed, because potential maximum amount of the slots required goes into 10s of thousands and from my understanding making the size set in stone (when all of it is not needed most of the time) is a huge waste of memory and loss of the execution speed of the program. I also do not want to use any complex prewritten functions in my implementation (the functions provided by STL or different libraries such as vector etc.) as I want to understand all of them more by trying to make them myself/with brief help.

        struct variabl {
            char *given_name;
            double value;
        };
        variabl* variables[50000];
        int c = 0;
        int end_of_stack = 0;

        class Stack
        {
        private:
            int top, length;
            char *z;
            int index_struc = 0;


        public:
            Stack(int = 0);
            ~Stack();
            char pop();
            void push();

        };

        Stack::Stack(int size) /* 
This is where the problem begins, I want to be able to allocate the size 
      dynamically.
    */
     {
            top = -1;
            length = size;
            z = new char[length];
        }
        void Stack::push()
        {
            ++top;
            z[top] = variables[index_struc]->value;
            index_struc++;
        }
        char Stack::pop()
        {
            end_of_stack = 0;
            if (z == 0 || top == -1)
            {
                end_of_stack = 1;
                return NULL;
            }
            char top_stack = z[top];
            top--;
            length--;

            return top_stack;
        }
        Stack::~Stack()
        {
            delete[] z;
        }

I had somewhat of a idea, and tried doing

Stack stackk
//whenever I want to put another thing into stack
stackk.push = new char;

but then I didnt completely understand how will it work for my purpose, I don't think it will be fully accessible with the pop method etc because it will be a set of separate arrays/variables right? I want the implementation to remain reasonably simple so I can understand it.

Change your push function to take a parameter, rather than needing to reference variables .

To handle pushes, start with an initial length of your array z (and change z to a better variable name ). When you are pushing a new value, check if the new value will mean that the size of your array is too small (by comparing length and top ). If it will exceed the current size, allocate a bigger array and copy the values from z to the new array, free up z , and make z point to the new array.

Here you have a simple implementation without the need of reallocating arrays. It uses the auxiliary class Node, that holds a value, and a pointer to another Node (that is set to NULL to indicate the end of the stack).

main() tests the stack by reading commands of the form

  • pc: push c to the stack
  • g: print top of stack and pop

    #include <cstdlib> #include <iostream> using namespace std; class Node { private: char c; Node *next; public: Node(char cc, Node *nnext){ c = cc; next = nnext; } char getChar(){ return c; } Node *getNext(){ return next; } ~Node(){} }; class Stack { private: Node *start; public: Stack(){ start = NULL; } void push(char c){ start = new Node(c, start); } char pop(){ if(start == NULL){ //Handle error cerr << "pop on empty stack" << endl; exit(1); } else { char r = (*start).getChar(); Node* newstart = (*start).getNext(); delete start; start = newstart; return r; } } bool empty(){ return start == NULL; } }; int main(){ char c, k; Stack st; while(cin>>c){ switch(c){ case 'p': cin >> k; st.push(k); break; case 'g': cout << st.pop()<<endl; break; } } return 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM