Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} rp-code9 / programming Public Notifications You must be signed in to change notification settings Fork 1 Star 0 Code Issues 0 Pull requests 0 Actions Projects Security and quality 0 Insights Additional navigation options Code Issues Pull requests Actions Projects Security and quality Insights FilesExpand file tree masterBreadcrumbsprogramming/problems/stack/DynamicStack.javaCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory67 lines (55 loc) · 1.7 KB masterBreadcrumbsprogramming/problems/stack/DynamicStack.javaCopy pathTopFile metadata and controlsCodeBlame67 lines (55 loc) · 1.7 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import java.util.NoSuchElementException; class DynamicStack { private String[] stack; private int top; public DynamicStack() { top = 0; stack = new String[2]; } private void resize(int capacity) { String[] copy = new String[capacity]; for (int i = 0; i < top; ++i) { copy[i] = stack[i]; } stack = copy; } public int size() { return top; } public boolean isEmpty() { return top == 0; } public void push(String s) { if (top == stack.length) { resize(stack.length * 2); } stack[top++] = s; } public String pop() { if (isEmpty()) { throw new NoSuchElementException("Stack underflow"); } String item = stack[top - 1]; stack[top - 1] = null; top--; if (top > 0 && top == stack.length / 4) { resize(stack.length / 2); } return item; } public static void main(String[] args) { String[] string = {"a", "b", "c", "d", "e"}; DynamicStack stack = new DynamicStack(); System.out.println("IsEmpty : " + stack.isEmpty() + ", size: " + stack.size()); for (String s : string) { System.out.print("Push : " + s + " , "); stack.push(s); System.out.println("IsEmpty : " + stack.isEmpty() + ", size: " + stack.size()); } while(!stack.isEmpty()) { System.out.print("Pop : " + stack.pop() + " "); System.out.println("IsEmpty : " + stack.isEmpty() + ", size: " + stack.size()); } System.out.println("IsEmpty : " + stack.isEmpty() + ", size: " + stack.size()); }} You can’t perform that action at this time.