{"id":"3088a44f-d460-4a6f-b5ea-b0fd53909f86","name":"Normal Stack","description":"1. You are required to complete the code of our CustomStack class. The class should mimic the behaviour of java.util.Stack class and implement LIFO semantic.\r\n2. Here is the list of functions that you are supposed to complete\r\n 2.1. push -> Should accept new data if there is space available in the underlying \r\n array or print \"Stack overflow\" otherwise.\r\n 2.2. pop -> Should remove and return last data if available or print \"Stack \r\n underflow\" otherwise and return -1.\r\n 2.3. top -> Should return last data if available or print \"Stack underflow\" \r\n otherwise and return -1.\r\n 2.4. size -> Should return the number of elements available in the stack.\r\n 2.5. display -> Should print the elements of stack in LIFO manner (space- \r\n separated) ending with a line-break.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <exception>\nusing namespace std;\n\nclass Stack{\n \n public:\n int *arr;\n int tos;\n int MaxCapacity;\n \n public:\n Stack(int size){\n this->MaxCapacity = size;\n this->arr = new int[this->MaxCapacity];\n this->tos = -1;\n }\n \n public:\n Stack(){\n Stack(10);\n }\n \n public:\n int size(){\n // write your code here\n }\n \n public:\n void push(int data){\n // write your code here\n }\n \n public:\n int top()\n {\n // write your code here\n }\n \n public:\n int pop()\n {\n // write your code here\n }\n \n public: \n void display() {\n // write your code here\n }\n};\n\nint main(){\n int n;\n cin>>n;\n Stack st(n);\n string str;\n cin>>str;\n \n while(str!=\"quit\"){\n if(str==\"push\"){\n int val;\n cin>>val;\n st.push(val);\n } else if(str==\"pop\"){\n int val = st.pop();\n if(val != -1){\n cout<<val<<endl;\n }\n } else if(str==\"top\"){\n int val = st.top();\n if(val != -1){\n cout<<val<<endl;\n }\n \n } else if(str==\"size\"){\n cout<<st.size()<<endl;\n } else if(str==\"display\"){\n st.display();\n }\n cin>>str;\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class CustomStack {\r\n int[] data;\r\n int tos;\r\n\r\n public CustomStack(int cap) {\r\n data = new int[cap];\r\n tos = -1;\r\n }\r\n\r\n int size() {\r\n // write ur code here\r\n }\r\n\r\n void display() {\r\n // write ur code here\r\n }\r\n\r\n void push(int val) {\r\n // write ur code here\r\n }\r\n\r\n int pop() {\r\n // write ur code here\r\n }\r\n\r\n int top() {\r\n // write ur code here\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n CustomStack st = new CustomStack(n);\r\n\r\n String str = br.readLine();\r\n while(str.equals(\"quit\") == false){\r\n if(str.startsWith(\"push\")){\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push(val);\r\n } else if(str.startsWith(\"pop\")){\r\n int val = st.pop();\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n } else if(str.startsWith(\"top\")){\r\n int val = st.top();\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n } else if(str.startsWith(\"size\")){\r\n System.out.println(st.size());\r\n } else if(str.startsWith(\"display\")){\r\n st.display();\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class CustomStack:\n \n def __init__(self , cap):\n self.cap = cap\n self.tos = -1\n self.arr = []\n self.arr = [0] * cap\n \n def size(self):\n # write your code here\n \n\n def push(self , data):\n # write your code here\n \n \n def top(self):\n # write ur code here\n \n \n \n def pop(self):\n # write ur code here\n \n \n def display(self):\n # write ur code here\n \n\n\ndef main():\n \n n = int(input());\n \n inpStr = str(input()).split(\" \")\n st = CustomStack(n)\n \n while inpStr[0] != \"quit\":\n if inpStr[0].strip() == \"push\":\n val = inpStr[1]\n st.push(val)\n elif inpStr[0].strip() == \"pop\":\n val = st.pop()\n if val != -1:\n print(val)\n elif inpStr[0].strip() == \"top\":\n val = st.top()\n if val != -1:\n print(val)\n elif inpStr[0].strip() == \"size\":\n print(st.size())\n elif inpStr[0].strip() == \"display\":\n st.display()\n \n inpStr = str(input()).split(\" \")\n \n \n \n \nmain()"}},"points":10,"difficulty":"easy","sampleInput":"5\r\npush 10\r\ndisplay\r\npush 20\r\ndisplay\r\npush 30\r\ndisplay\r\npush 40\r\ndisplay\r\npush 50\r\ndisplay\r\npush 60\r\ndisplay\r\ntop\r\npop\r\ndisplay\r\ntop\r\npop\r\ndisplay\r\ntop\r\npop\r\ndisplay\r\ntop\r\npop\r\ndisplay\r\ntop\r\npop\r\ndisplay\r\ntop\r\npop\r\nquit","sampleOutput":"10 \r\n20 10 \r\n30 20 10 \r\n40 30 20 10 \r\n50 40 30 20 10 \r\nStack overflow\r\n50 40 30 20 10 \r\n50\r\n50\r\n40 30 20 10 \r\n40\r\n40\r\n30 20 10 \r\n30\r\n30\r\n20 10 \r\n20\r\n20\r\n10 \r\n10\r\n10\r\n\r\nStack underflow\r\nStack underflow","questionVideo":"https://www.youtube.com/embed/54al81dc_EE","hints":[],"associated":[{"id":"10a58ef6-4ad7-4dd5-a9f4-19433a52e96b","name":"What if the stack is already full in pop(), which means that tos is equal to ( data.length - 1) ?(Q- normal Stack)","slug":"what-if-the-stack-is-already-full-in-pop-which-means-that-tos-is-equal-to-data-length-1-q-normal-stack","type":4},{"id":"1300cddb-1b53-4070-ba50-873e59bf758d","name":"push(), pop(), top() and size() function have same time and space complexties, what are those ?9Q- normal Stack)","slug":"push-pop-top-and-size-function-have-same-time-and-space-complexties-what-are-those-9q-normal-stack","type":4},{"id":"3d7f52c0-72d8-418b-a59c-de0bc87a87a3","name":"What is the time and space complexity of display function ?(Q- Normal Stack)","slug":"what-is-the-time-and-space-complexity-of-display-function-q-normal-stack","type":4},{"id":"5740af08-c846-40cc-b355-53df0614d7cc","name":"The value stored at tos is collected and returne in which function ?(Q- Normal Stack)","slug":"the-value-stored-at-tos-is-collected-and-returne-in-which-function-q-normal-stack","type":4},{"id":"acd3cf55-7423-42c2-9b73-c7565de3118d","name":"How elements are pushed in stack ?(Q- Normal Stack)","slug":"how-elements-are-pushed-in-stack-q-normal-stack","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"9847c2b3-e3ad-4b1c-97d1-00206b1be68d","name":"Stacks And Queues For Beginners","slug":"stacks-and-queues-for-beginners","type":0},{"id":"cf0d4ae9-3544-4801-856f-2630d4518942","name":"Normal Stack","slug":"normal-stack","type":1}],"next":{"id":"d12a6223-f3d7-48e9-88a0-2e0beae50be8","name":"Normal Stack","type":3,"slug":"normal-stack"},"prev":{"id":"46380ad7-ec29-4801-92e5-7e034387bd5f","name":"OOPS CONSTRUCTORS AND THIS","type":3,"slug":"oops-constructors-and-this"}}}

Normal Stack

1. You are required to complete the code of our CustomStack class. The class should mimic the behaviour of java.util.Stack class and implement LIFO semantic. 2. Here is the list of functions that you are supposed to complete 2.1. push -> Should accept new data if there is space available in the underlying array or print "Stack overflow" otherwise. 2.2. pop -> Should remove and return last data if available or print "Stack underflow" otherwise and return -1. 2.3. top -> Should return last data if available or print "Stack underflow" otherwise and return -1. 2.4. size -> Should return the number of elements available in the stack. 2.5. display -> Should print the elements of stack in LIFO manner (space- separated) ending with a line-break. 3. Input and Output is managed for you.

{"id":"3088a44f-d460-4a6f-b5ea-b0fd53909f86","name":"Normal Stack","description":"1. You are required to complete the code of our CustomStack class. The class should mimic the behaviour of java.util.Stack class and implement LIFO semantic.\r\n2. Here is the list of functions that you are supposed to complete\r\n 2.1. push -> Should accept new data if there is space available in the underlying \r\n array or print \"Stack overflow\" otherwise.\r\n 2.2. pop -> Should remove and return last data if available or print \"Stack \r\n underflow\" otherwise and return -1.\r\n 2.3. top -> Should return last data if available or print \"Stack underflow\" \r\n otherwise and return -1.\r\n 2.4. size -> Should return the number of elements available in the stack.\r\n 2.5. display -> Should print the elements of stack in LIFO manner (space- \r\n separated) ending with a line-break.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <exception>\nusing namespace std;\n\nclass Stack{\n \n public:\n int *arr;\n int tos;\n int MaxCapacity;\n \n public:\n Stack(int size){\n this->MaxCapacity = size;\n this->arr = new int[this->MaxCapacity];\n this->tos = -1;\n }\n \n public:\n Stack(){\n Stack(10);\n }\n \n public:\n int size(){\n // write your code here\n }\n \n public:\n void push(int data){\n // write your code here\n }\n \n public:\n int top()\n {\n // write your code here\n }\n \n public:\n int pop()\n {\n // write your code here\n }\n \n public: \n void display() {\n // write your code here\n }\n};\n\nint main(){\n int n;\n cin>>n;\n Stack st(n);\n string str;\n cin>>str;\n \n while(str!=\"quit\"){\n if(str==\"push\"){\n int val;\n cin>>val;\n st.push(val);\n } else if(str==\"pop\"){\n int val = st.pop();\n if(val != -1){\n cout<<val<<endl;\n }\n } else if(str==\"top\"){\n int val = st.top();\n if(val != -1){\n cout<<val<<endl;\n }\n \n } else if(str==\"size\"){\n cout<<st.size()<<endl;\n } else if(str==\"display\"){\n st.display();\n }\n cin>>str;\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class CustomStack {\r\n int[] data;\r\n int tos;\r\n\r\n public CustomStack(int cap) {\r\n data = new int[cap];\r\n tos = -1;\r\n }\r\n\r\n int size() {\r\n // write ur code here\r\n }\r\n\r\n void display() {\r\n // write ur code here\r\n }\r\n\r\n void push(int val) {\r\n // write ur code here\r\n }\r\n\r\n int pop() {\r\n // write ur code here\r\n }\r\n\r\n int top() {\r\n // write ur code here\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n CustomStack st = new CustomStack(n);\r\n\r\n String str = br.readLine();\r\n while(str.equals(\"quit\") == false){\r\n if(str.startsWith(\"push\")){\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push(val);\r\n } else if(str.startsWith(\"pop\")){\r\n int val = st.pop();\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n } else if(str.startsWith(\"top\")){\r\n int val = st.top();\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n } else if(str.startsWith(\"size\")){\r\n System.out.println(st.size());\r\n } else if(str.startsWith(\"display\")){\r\n st.display();\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class CustomStack:\n \n def __init__(self , cap):\n self.cap = cap\n self.tos = -1\n self.arr = []\n self.arr = [0] * cap\n \n def size(self):\n # write your code here\n \n\n def push(self , data):\n # write your code here\n \n \n def top(self):\n # write ur code here\n \n \n \n def pop(self):\n # write ur code here\n \n \n def display(self):\n # write ur code here\n \n\n\ndef main():\n \n n = int(input());\n \n inpStr = str(input()).split(\" \")\n st = CustomStack(n)\n \n while inpStr[0] != \"quit\":\n if inpStr[0].strip() == \"push\":\n val = inpStr[1]\n st.push(val)\n elif inpStr[0].strip() == \"pop\":\n val = st.pop()\n if val != -1:\n print(val)\n elif inpStr[0].strip() == \"top\":\n val = st.top()\n if val != -1:\n print(val)\n elif inpStr[0].strip() == \"size\":\n print(st.size())\n elif inpStr[0].strip() == \"display\":\n st.display()\n \n inpStr = str(input()).split(\" \")\n \n \n \n \nmain()"}},"points":10,"difficulty":"easy","sampleInput":"5\r\npush 10\r\ndisplay\r\npush 20\r\ndisplay\r\npush 30\r\ndisplay\r\npush 40\r\ndisplay\r\npush 50\r\ndisplay\r\npush 60\r\ndisplay\r\ntop\r\npop\r\ndisplay\r\ntop\r\npop\r\ndisplay\r\ntop\r\npop\r\ndisplay\r\ntop\r\npop\r\ndisplay\r\ntop\r\npop\r\ndisplay\r\ntop\r\npop\r\nquit","sampleOutput":"10 \r\n20 10 \r\n30 20 10 \r\n40 30 20 10 \r\n50 40 30 20 10 \r\nStack overflow\r\n50 40 30 20 10 \r\n50\r\n50\r\n40 30 20 10 \r\n40\r\n40\r\n30 20 10 \r\n30\r\n30\r\n20 10 \r\n20\r\n20\r\n10 \r\n10\r\n10\r\n\r\nStack underflow\r\nStack underflow","questionVideo":"https://www.youtube.com/embed/54al81dc_EE","hints":[],"associated":[{"id":"10a58ef6-4ad7-4dd5-a9f4-19433a52e96b","name":"What if the stack is already full in pop(), which means that tos is equal to ( data.length - 1) ?(Q- normal Stack)","slug":"what-if-the-stack-is-already-full-in-pop-which-means-that-tos-is-equal-to-data-length-1-q-normal-stack","type":4},{"id":"1300cddb-1b53-4070-ba50-873e59bf758d","name":"push(), pop(), top() and size() function have same time and space complexties, what are those ?9Q- normal Stack)","slug":"push-pop-top-and-size-function-have-same-time-and-space-complexties-what-are-those-9q-normal-stack","type":4},{"id":"3d7f52c0-72d8-418b-a59c-de0bc87a87a3","name":"What is the time and space complexity of display function ?(Q- Normal Stack)","slug":"what-is-the-time-and-space-complexity-of-display-function-q-normal-stack","type":4},{"id":"5740af08-c846-40cc-b355-53df0614d7cc","name":"The value stored at tos is collected and returne in which function ?(Q- Normal Stack)","slug":"the-value-stored-at-tos-is-collected-and-returne-in-which-function-q-normal-stack","type":4},{"id":"acd3cf55-7423-42c2-9b73-c7565de3118d","name":"How elements are pushed in stack ?(Q- Normal Stack)","slug":"how-elements-are-pushed-in-stack-q-normal-stack","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"9847c2b3-e3ad-4b1c-97d1-00206b1be68d","name":"Stacks And Queues For Beginners","slug":"stacks-and-queues-for-beginners","type":0},{"id":"cf0d4ae9-3544-4801-856f-2630d4518942","name":"Normal Stack","slug":"normal-stack","type":1}],"next":{"id":"d12a6223-f3d7-48e9-88a0-2e0beae50be8","name":"Normal Stack","type":3,"slug":"normal-stack"},"prev":{"id":"46380ad7-ec29-4801-92e5-7e034387bd5f","name":"OOPS CONSTRUCTORS AND THIS","type":3,"slug":"oops-constructors-and-this"}}}
plane

Editor


Loading...

Normal Stack

easy

1. You are required to complete the code of our CustomStack class. The class should mimic the behaviour of java.util.Stack class and implement LIFO semantic. 2. Here is the list of functions that you are supposed to complete 2.1. push -> Should accept new data if there is space available in the underlying array or print "Stack overflow" otherwise. 2.2. pop -> Should remove and return last data if available or print "Stack underflow" otherwise and return -1. 2.3. top -> Should return last data if available or print "Stack underflow" otherwise and return -1. 2.4. size -> Should return the number of elements available in the stack. 2.5. display -> Should print the elements of stack in LIFO manner (space- separated) ending with a line-break. 3. Input and Output is managed for you.

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

5 push 10 display push 20 display push 30 display push 40 display push 50 display push 60 display top pop display top pop display top pop display top pop display top pop display top pop quit

Sample Output

10 20 10 30 20 10 40 30 20 10 50 40 30 20 10 Stack overflow 50 40 30 20 10 50 50 40 30 20 10 40 40 30 20 10 30 30 20 10 20 20 10 10 10 Stack underflow Stack underflow

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode