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

  • Home
  • SEARCH
  • 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 3427816
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:53:00+00:00 2026-05-18T06:53:00+00:00

I’ve been trying to represent Stacks as a template, I used a struct and

  • 0

I’ve been trying to represent Stacks as a template, I used a struct and every thing is good, but every time I wanted to write a template function, I had to write the same template statement, which didn’t seem correct -although working-

So how can I write one template statement for all the functions?, here is my code :

template <typename T>
struct Stack
{
    T Value;
    Stack* next;
};
template <typename T>
void Push(T Value,Stack* &Top)
{
    Stack * Cell = new Stack();
    Cell->Value = Value;
    Cell->next = Top;
    Top = Cell;
};
template <typename T>
bool IsEmpty(Stack * Top)
{
    return (Top==0);
}
template <typename T>
void Pop(T &Value,Stack* &Top)
{
    if (IsEmpty(Top))
        cout  * Temp = Top;
        Value = Top->Value;
        Top = Top->next;
        delete Temp;
    }
}
template <typename T>
void GetTop(T &Value, Stack* &Top)
{
    if (IsEmpty(Top))
        cout Value;
}
template <typename T>
void EmptyStack(Stack * &Top)
{
    Stack * Temp;
    while (!(IsEmpty(Top)))
    {
        Temp = Top;
        Top = Top->next;
        delete Temp;
    }
}

Hope what I mean is clear now, sorry for the slight question 🙁

thanks in advance.

  • 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-18T06:53:01+00:00Added an answer on May 18, 2026 at 6:53 am

    If (as appears to be the case based on your comment) you want them as free functions, you can’t. You’ll also have to change the Stack parameter, something like this:

    template <typename T>
    void Push(T Value, Stack<T>* &Top)
    {
        Stack * Cell = new Stack();
        Cell->Value = Value;
        Cell->next = Top;
        Top = Cell;
    };
    

    As it stands, I’m not too excited about your design though. You try to use the Stack type as both an actual stack, and as a single node (Cell) in the stack. This is unnecessarily confusing at best.

    Edit: As far as stack vs. node goes, what I’m talking about is (as in the code immediately above): Stack *Cell = new Stack(); — you’re allocating a single Cell that goes in the stack, but the type you’re using for it is Stack.

    I’d do something like this instead:

    template <class T>
    struct Stack { 
        struct node { 
            T data;
            node *next;
        };
    
        node *head;
    };
    
    template <class T> 
    void push(T item, Stack<T> *&s) { 
        Stack<T>::node *n = new Stack<T>:node();       
        n->data = item;
        n->next = s->head;
        s->head = n;
    }
    

    It doesn’t make a lot of difference in what you’re really doing, but when you’re putting something onto a stack, allocating a Stack<T>::node seems (at least to me) to make a lot more sense than allocating a Stack<T>. A stack containing multiple nodes makes sense — a Stack containing multiple stacks really doesn’t.

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

Sidebar

Related Questions

No related questions found

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.