Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 710613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:38:26+00:00 2026-05-14T04:38:26+00:00

I have a question considering a program that stimulates a stack (not using any

  • 0

I have a question considering a program that stimulates a stack (not using any built-in stack features or such).

stack2= 1 2 3 4 5 //single dimension array of 5 elements

By calling the method pop the stack should look like the following, basically taking an element off each time the stack is being called up again.

stack2= 1 2 3 4 0  
stack2= 1 2 3 0 0  
stack2= 1 2 0 0 0  
stack2= 1 0 0 0 0  
stack2= 0 0 0 0 0

Here is my code:

for (int i = 1; i <= 6; i++)
{
    number= TryPop(s2);
    //use number
    ShowStack(s2, "s2");
}

public void Push(int g)
{
    if (top == Max)
    {
        throw new Exception("Stack overflow...");
    }
    else
    {
        tabel[top] = g;
        top++;
    }
}/*Push*/

I already have code that fills my array with values (through a push method). The pop method should take the last value and place it on 0, then calls up the next stack and place the following on 0 (like shown above in stack2).

The current pop method that keeps track of the top index (0 elements = 0 top, 1 element = 1 top etc..) already includes an underflow warning if this goes on 0 or below (which is correct).

public int Pop()
{
    if(top <= 0)
    {
        throw new Exception("Stack underflow...");
    }
    else
    {
        for (int j = tabel.Length - 1; j >= 0; j--)  
        {
            //...Really not sure what to do here.
        }
    }

    return number; 

}/*Pop*/

Since in the other class I already have a loop (for loop shown above) that simulates 6 times the s2 stack. (first stack: 1 2 3 4 0, second stack 1 2 3 0 0 and so on.)
How exactly do I take an element off each time? Either I have the entire display on 0 or the 0 in the wrong places / out of index errors.

Edit: Working Pop method:

public int Pop()
{
    if(top <= 0)
    {
        throw new Exception("Stack underflow...");
    }

    top--;
    tabel[top] = 0;
    number = tabel[top];

    return number;
}/*Pop*/
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:38:27+00:00Added an answer on May 14, 2026 at 4:38 am

    To implement a stack, you need an array and a “pointer” to the top of the stack.

    empty       _ _ _ _ _
                ↑
    

    In your code, table is the array and top is the pointer (as array index).

    Push

    To push an item to a stack, put the item at top of the stack and advance the pointer by one:

    push 1      1 _ _ _ _
                  ↑
    
    push 2      1 2 _ _ _
                    ↑
    
    push 3      1 2 3 _ _
                      ↑
    

    That’s what your code already does:

    table[top] = g;    // insert `g` at `top` into `table`
    top++;             // advance `top` by one
    

    Pop

    To pop an item, move the pointer back by one and return+erase the item at the top:

    pop 3       1 2 _ _ _
                    ↑
    
    pop 2       1 _ _ _ _
                  ↑
    
    pop 1       _ _ _ _ _
                ↑
    

    Now try to translate your solution for Push to do the reverse as shown here!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try public bool IsDivisible(int x, int n) { return (x… May 15, 2026 at 4:03 pm
  • Editorial Team
    Editorial Team added an answer You're most likely correct - Reading that many files is… May 15, 2026 at 4:03 pm
  • Editorial Team
    Editorial Team added an answer This is causing an ifinate loop, youo will want to… May 15, 2026 at 4:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.