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 9253199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:09:27+00:00 2026-06-18T11:09:27+00:00

Please have a look at the following code template <typename T> class Stack {

  • 0

Please have a look at the following code

template <typename T>

class Stack
{
public:
    Stack(int number)
    {
        maxSize = number;
        top = -1;
        stackData = new T*(maxSize);
    }

    ~Stack()
    {
        delete [] stackData;
    }

    int count()
    {

    }

    bool isEmpty()
    {
        if(top==-1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    bool isFull()
    {
        if(top== (maxSize-1))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    *T pop()
    {
        if(!isEmpty())
        {
            return stackData[top--]; // Remove Item From Stack
        }
    }

    *T peek();

    void push(T *pushValue)
    {
        if(!isFull())
        {
            stackData[++top] = pushValue;
        }
    }

private:
    int maxSize;
    T ** stackData;
    int top;
};

In the above code, the commented line says “Removing Item from the Stack”. But actually, it is not removing, it is simply providing the value which is one value behind, right? In here, I refer removing as completely destroying that particular value from the stack.

ex: In an array which contains data 1,2,3,4 I remove ‘2’. So now it is 1,3,4

Second, what should happen inside the “peek()” method?

Third, are there are any errors that I didn’t detect?

Please help!

  • 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-06-18T11:09:29+00:00Added an answer on June 18, 2026 at 11:09 am

    Conceptually, there is no difference between decrementing top and “removing” the top item. The word “remove” is a conceptual abstraction to describe the idea that the top item in the stack is no longer an element in the stack. The fact that it is not literally removed from that location in memory is irrelevant.

    If you mean you want to “destroy” the top item, i.e. invoke it’s destructor and deallocate it, you need to consider the larger implications of how your Stack class manages memory. If the stack is supposed to take ownership of each T object, and each T object has been allocated using new, then you can have your pop() function delete the top item before decrementing top. (But then pop() can’t return a pointer to the deleted element.) If the stack doesn’t take ownership of each item, then it is up to the caller of pop() to manage the lifetime/deallocation of the element.

    Next, the peek() method simply returns a pointer to the top item, without removing it from the stack.

    And finally, you are not correctly allocating the array of T* pointers. The syntax should be:

    stackData = new T*[maxSize];
    

    The code you posted uses parentheses after new instead of brackets, which is not what you want here.

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

Sidebar

Related Questions

Please have a look at the following code, Stack.h template <typename T> class Stack
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code Calculator.h #pragma once #include <iostream> template<class
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Please have a look at the following code #include <iostream> using namespace std; int
Please have a look at the following code DatabaseHandler.java public List getDetails(String name) {
Please have a look at the following code package normal; //This class if s
Please have a look the following code public String setEmailAccount(String account,String userName, String password)
Please have a look at the following code GameObject.h #pragma once class GameObject {

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.