{"id":"a43c060b-9d57-4ca9-bbf8-eb33b886a72a","name":"Dynamic Stack","description":"1. You are given the code of our CustomStack class which mimics the java.util.Stack class.\r\n2. Here is the list of functions that are written in the class...\r\n 2.1. push -> Accepts new data if there is space available in the underlying array or \r\n print \"Stack overflow\" otherwise\r\n 2.2. pop -> Removes and returns last data if available or print \"Stack underflow\" \r\n otherwise and returns -1\r\n 2.3. top -> Return last data if available or print \"Stack underflow\" otherwise and \r\n returns -1\r\n 2.4. size -> Returns the number of elements available in the stack\r\n 2.5. display -> Print the elements of stack in LIFO manner (space-separated) \r\n ending with a line-break.\r\n3. Input and Output are managed for you.\r\n4. You are required to change the body of push function to accept the element even when the data array is completely full. In that case, you are required to reallocate the data array (to twice it's size). You should not print \"Stack overflow\" ever.","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 \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 return tos + 1;\r\n }\r\n\r\n void display() {\r\n for (int i = tos; i >= 0; i--) {\r\n System.out.print(data[i] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n\r\n // change the code of this function according to question\r\n void push(int val) {\r\n if (tos == data.length - 1) {\r\n System.out.println(\"Stack overflow\");\r\n } else {\r\n tos++;\r\n data[tos] = val;\r\n }\r\n }\r\n\r\n int pop() {\r\n if (tos == -1) {\r\n System.out.println(\"Stack underflow\");\r\n return -1;\r\n } else {\r\n int val = data[tos];\r\n tos--;\r\n return val;\r\n }\r\n }\r\n\r\n int top() {\r\n if (tos == -1) {\r\n System.out.println(\"Stack underflow\");\r\n return -1;\r\n } else {\r\n return data[tos];\r\n }\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 your code here\n \n \n \n def pop(self):\n # write your code here\n \n \n def display(self):\n # write your 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\n60 50 40 30 20 10 \r\n60\r\n60\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","questionVideo":"https://www.youtube.com/embed/DaKNuSxraS4","hints":[],"associated":[{"id":"52361d62-532f-4ab6-a959-ed3d57fd7869","name":"what is the time complexity of this problem?(Q- Dynamic Stack)","slug":"what-is-the-time-complexity-of-this-problem-q-dynamic-stack","type":4},{"id":"8cb17bdc-baa6-4535-9853-c086dcdb1d55","name":"what data structure are we using to create a dynamic stack?(Q- Dynamic Stack)","slug":"what-data-structure-are-we-using-to-create-a-dynamic-stack-q-dynamic-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":"355aa2fc-edbc-4433-bbe9-669a3dd5d1b5","name":"Dynamic Stack","slug":"dynamic-stack","type":1}],"next":{"id":"b9959989-95fb-4c64-8524-7c5aa849ce3a","name":"Dynamic Stack","type":3,"slug":"dynamic-stack"},"prev":{"id":"d12a6223-f3d7-48e9-88a0-2e0beae50be8","name":"Normal Stack","type":3,"slug":"normal-stack"}}}

Dynamic Stack

1. You are given the code of our CustomStack class which mimics the java.util.Stack class. 2. Here is the list of functions that are written in the class... 2.1. push -> Accepts new data if there is space available in the underlying array or print "Stack overflow" otherwise 2.2. pop -> Removes and returns last data if available or print "Stack underflow" otherwise and returns -1 2.3. top -> Return last data if available or print "Stack underflow" otherwise and returns -1 2.4. size -> Returns the number of elements available in the stack 2.5. display -> Print the elements of stack in LIFO manner (space-separated) ending with a line-break. 3. Input and Output are managed for you. 4. You are required to change the body of push function to accept the element even when the data array is completely full. In that case, you are required to reallocate the data array (to twice it's size). You should not print "Stack overflow" ever.

{"id":"a43c060b-9d57-4ca9-bbf8-eb33b886a72a","name":"Dynamic Stack","description":"1. You are given the code of our CustomStack class which mimics the java.util.Stack class.\r\n2. Here is the list of functions that are written in the class...\r\n 2.1. push -> Accepts new data if there is space available in the underlying array or \r\n print \"Stack overflow\" otherwise\r\n 2.2. pop -> Removes and returns last data if available or print \"Stack underflow\" \r\n otherwise and returns -1\r\n 2.3. top -> Return last data if available or print \"Stack underflow\" otherwise and \r\n returns -1\r\n 2.4. size -> Returns the number of elements available in the stack\r\n 2.5. display -> Print the elements of stack in LIFO manner (space-separated) \r\n ending with a line-break.\r\n3. Input and Output are managed for you.\r\n4. You are required to change the body of push function to accept the element even when the data array is completely full. In that case, you are required to reallocate the data array (to twice it's size). You should not print \"Stack overflow\" ever.","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 \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 return tos + 1;\r\n }\r\n\r\n void display() {\r\n for (int i = tos; i >= 0; i--) {\r\n System.out.print(data[i] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n\r\n // change the code of this function according to question\r\n void push(int val) {\r\n if (tos == data.length - 1) {\r\n System.out.println(\"Stack overflow\");\r\n } else {\r\n tos++;\r\n data[tos] = val;\r\n }\r\n }\r\n\r\n int pop() {\r\n if (tos == -1) {\r\n System.out.println(\"Stack underflow\");\r\n return -1;\r\n } else {\r\n int val = data[tos];\r\n tos--;\r\n return val;\r\n }\r\n }\r\n\r\n int top() {\r\n if (tos == -1) {\r\n System.out.println(\"Stack underflow\");\r\n return -1;\r\n } else {\r\n return data[tos];\r\n }\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 your code here\n \n \n \n def pop(self):\n # write your code here\n \n \n def display(self):\n # write your 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\n60 50 40 30 20 10 \r\n60\r\n60\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","questionVideo":"https://www.youtube.com/embed/DaKNuSxraS4","hints":[],"associated":[{"id":"52361d62-532f-4ab6-a959-ed3d57fd7869","name":"what is the time complexity of this problem?(Q- Dynamic Stack)","slug":"what-is-the-time-complexity-of-this-problem-q-dynamic-stack","type":4},{"id":"8cb17bdc-baa6-4535-9853-c086dcdb1d55","name":"what data structure are we using to create a dynamic stack?(Q- Dynamic Stack)","slug":"what-data-structure-are-we-using-to-create-a-dynamic-stack-q-dynamic-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":"355aa2fc-edbc-4433-bbe9-669a3dd5d1b5","name":"Dynamic Stack","slug":"dynamic-stack","type":1}],"next":{"id":"b9959989-95fb-4c64-8524-7c5aa849ce3a","name":"Dynamic Stack","type":3,"slug":"dynamic-stack"},"prev":{"id":"d12a6223-f3d7-48e9-88a0-2e0beae50be8","name":"Normal Stack","type":3,"slug":"normal-stack"}}}
plane

Editor


Loading...

Dynamic Stack

easy

1. You are given the code of our CustomStack class which mimics the java.util.Stack class. 2. Here is the list of functions that are written in the class... 2.1. push -> Accepts new data if there is space available in the underlying array or print "Stack overflow" otherwise 2.2. pop -> Removes and returns last data if available or print "Stack underflow" otherwise and returns -1 2.3. top -> Return last data if available or print "Stack underflow" otherwise and returns -1 2.4. size -> Returns the number of elements available in the stack 2.5. display -> Print the elements of stack in LIFO manner (space-separated) ending with a line-break. 3. Input and Output are managed for you. 4. You are required to change the body of push function to accept the element even when the data array is completely full. In that case, you are required to reallocate the data array (to twice it's size). You should not print "Stack overflow" ever.

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 60 50 40 30 20 10 60 60 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

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode