{"id":10562,"date":"2024-02-18T16:33:25","date_gmt":"2024-02-18T16:33:25","guid":{"rendered":"https:\/\/adelnasim.com\/docs\/courses\/data-structures\/stack-implementation\/"},"modified":"2024-04-07T01:19:53","modified_gmt":"2024-04-07T01:19:53","slug":"stack-implementation","status":"publish","type":"docs","link":"https:\/\/adelnasim.com\/ar\/docs\/courses\/data-structures\/stack-implementation\/","title":{"rendered":"\u062a\u0646\u0641\u064a\u0630 \u0627\u0644\u0640Stack"},"content":{"rendered":"<p>\u064a\u0639\u0631\u0641 \u0647\u0630\u0627 \u0627\u0644\u0628\u0631\u0646\u0627\u0645\u062c stack class \u0628\u0633\u064a\u0637\u0629 \u0645\u0639 \u0639\u0645\u0644\u064a\u0627\u062a \u0623\u0633\u0627\u0633\u064a\u0629 \u0645\u062b\u0644 push, pop, getTop, \u0648print. \u0625\u0644\u064a\u0643 \u062a\u0641\u0627\u0635\u064a\u0644 \u0627\u0644\u0628\u0631\u0646\u0627\u0645\u062c:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nconst int MAX_SIZE = 100;\r\n\r\nclass Stack {\r\nprivate:\r\n    int top;            \/\/ Index of the top element in the stack\r\n    int item[MAX_SIZE]; \/\/ Array to store stack elements\r\n\r\npublic:\r\n    \/\/ Constructor: Initializes top to -1 (empty stack)\r\n    Stack() : top(-1) {}\r\n\r\n    \/\/ Function to push an element onto the stack\r\n    void push(int Element) {\r\n        if (top &gt;= MAX_SIZE - 1) {\r\n            cout &lt;&lt; \"Stack is full on push\" &lt;&lt; endl;\r\n        } else {\r\n            top++;\r\n            item[top] = Element;\r\n        }\r\n    }\r\n\r\n    \/\/ Function to check if the stack is empty\r\n    bool isEmpty() {\r\n        return top &lt; 0;\r\n    }\r\n\r\n    \/\/ Function to pop an element from the stack\r\n    void pop() {\r\n        if (isEmpty()) {\r\n            cout &lt;&lt; \"Stack is empty on pop\" &lt;&lt; endl;\r\n        } else {\r\n            top--;\r\n        }\r\n    }\r\n\r\n    \/\/ Function to get the top element of the stack\r\n    void getTop(int&amp; stackTop) {\r\n        if (isEmpty()) {\r\n            cout &lt;&lt; \"Stack is empty on getTop\" &lt;&lt; endl;\r\n        } else {\r\n            stackTop = item[top];\r\n            cout &lt;&lt; stackTop &lt;&lt; endl;\r\n        }\r\n    }\r\n\r\n    \/\/ Function to print the elements of the stack\r\n    void print() {\r\n        cout &lt;&lt; \"[\";\r\n        for (int i = top; i &gt;= 0; i--) {\r\n            cout &lt;&lt; item[i] &lt;&lt; \" \";\r\n        }\r\n        cout &lt;&lt; \"]\" &lt;&lt; endl;\r\n    }\r\n};\r\n\r\nint main() {\r\n    Stack s;\r\n\r\n    \/\/ Push elements onto the stack\r\n    s.push(5);\r\n    s.push(10);\r\n    s.push(15);\r\n    s.push(20);\r\n\r\n    \/\/ Print the stack\r\n    s.print();\r\n\r\n    \/\/ Get and print the top element of the stack\r\n    int y = 0;\r\n    s.getTop(y);\r\n\r\n    \/\/ Pop an element from the stack\r\n    s.pop();\r\n\r\n    \/\/ Push another element onto the stack\r\n    s.push(7);\r\n\r\n    \/\/ Print the stack again\r\n    s.print();\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p>\u0634\u0631\u062d:<\/p>\n<ul>\n<li>\u062a\u0646\u0641\u0630 Stack class \u0628\u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u0639\u0646\u0635\u0631 \u0645\u0635\u0641\u0648\u0641\u0629 \u0645\u0633\u0645\u0627\u0629 item . \u064a\u062d\u062a\u0648\u064a \u0639\u0644\u0649 \u062f\u0648\u0627\u0644 \u0627\u0644\u0623\u0639\u0636\u0627\u0621  push, pop, getTop, isEmpty, \u0648print.<\/li>\n<li>\u0641\u064a \u062f\u0627\u0644\u0629 push \u060c \u0625\u0630\u0627 \u0644\u0645 \u062a\u0643\u0646 \u0627\u0644\u0640stack \u0645\u0645\u062a\u0644\u0626\u0629\u060c \u0641\u0625\u0646\u0647\u0627 \u062a\u0632\u064a\u062f \u0627\u0644\u0640 top index \u0648\u062a\u0636\u064a\u0641 \u0627\u0644\u0639\u0646\u0635\u0631 \u0625\u0644\u0649 \u0645\u0635\u0641\u0648\u0641\u0629 \u0627\u0644\u0639\u0646\u0627\u0635\u0631 \u0641\u064a \u0627\u0644\u0640 top index.<\/li>\n<li>\u062a\u062a\u062d\u0642\u0642 \u0627\u0644\u062f\u0627\u0644\u0629 isEmpty \u0645\u0645\u0627 \u0625\u0630\u0627 \u0643\u0627\u0646 \u0627\u0644\u0640 stack \u0641\u0627\u0631\u063a\u064b\u0627 \u0639\u0646 \u0637\u0631\u064a\u0642 \u0645\u0642\u0627\u0631\u0646\u0629 \u0627\u0644\u0640 top index \u0628\u0640 -1.<\/li>\n<li>\u062a\u0642\u0648\u0645 \u0627\u0644\u062f\u0627\u0644\u0629 pop \u0628\u062a\u0642\u0644\u064a\u0644 \u0627\u0644\u0640 top index  \u0625\u0630\u0627 \u0644\u0645 \u064a\u0643\u0646 \u0627\u0644\u0640 stack \u0641\u0627\u0631\u063a\u064b\u0627.<\/li>\n<li>\u062a\u0642\u0648\u0645 \u0627\u0644\u062f\u0627\u0644\u0629 getTop \u0628\u0627\u0633\u062a\u0631\u062f\u0627\u062f \u0627\u0644\u0640 top element \u0644\u0644\u0633\u062a\u0627\u0643 \u0648\u062a\u062e\u0632\u064a\u0646\u0647 \u0641\u064a stackTop.<\/li>\n<li>\u062a\u0642\u0648\u0645 \u062f\u0627\u0644\u0629 \u0627\u0644\u0637\u0628\u0627\u0639\u0629 \u0628\u0637\u0628\u0627\u0639\u0629 \u0639\u0646\u0627\u0635\u0631 \u0627\u0644\u0633\u062a\u0627\u0643 \u0645\u0646 \u0627\u0644\u0623\u0639\u0644\u0649 \u0625\u0644\u0649 \u0627\u0644\u0623\u0633\u0641\u0644.<\/li>\n<li>\u0641\u064a \u0627\u0644\u062f\u0627\u0644\u0629 \u0627\u0644\u0631\u0626\u064a\u0633\u064a\u0629\u060c \u064a\u062a\u0645 \u0625\u0646\u0634\u0627\u0621 \u0643\u0627\u0626\u0646 \u0627\u0644\u0633\u062a\u0627\u0643 \u0648\u062a\u0646\u0641\u064a\u0630 \u0639\u0645\u0644\u064a\u0627\u062a \u0645\u062e\u062a\u0644\u0641\u0629 \u0645\u062b\u0644  push, pop, getTop, \u0648print.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>This code defines a simple stack class with basic operations like push, pop, getTop, and print. Here&#8217;s a breakdown of the code: #include &lt;iostream&gt; using namespace std; const int MAX_SIZE = 100; class Stack { private: int top; \/\/ Index of the top element in the stack int item[MAX_SIZE]; \/\/ Array to store stack elements [&hellip;]<\/p>","protected":false},"author":3,"featured_media":0,"parent":2564,"menu_order":63,"comment_status":"closed","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-10562","docs","type-docs","status-publish","hentry","no-post-thumbnail"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/10562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/comments?post=10562"}],"version-history":[{"count":3,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/10562\/revisions"}],"predecessor-version":[{"id":10565,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/10562\/revisions\/10565"}],"up":[{"embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/2564"}],"wp:attachment":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/media?parent=10562"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/doc_tag?post=10562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}