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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:07:48+00:00 2026-06-18T14:07:48+00:00

First making a copy constructor for linked lists was difficult and now this stack.

  • 0

First making a copy constructor for linked lists was difficult and now this stack. I almost wanted to hit my head with something but then I thought of Stack Overflow guys. So here is the problem:
Consider One thing You can not change the list.h or stack.h or its constructor even.
// In list.h

template <class T>
struct ListItem
{
    T value;
    ListItem<T> *next;
    ListItem<T> *prev;

    ListItem(T theVal)
    {
        this->value = theVal;
        this->next = NULL;
        this->prev = NULL;
    }
};

/* This is the generic List class */
template <class T>
class List
{
    ListItem<T> *head;

public:

    // Constructor
    List();

    // Copy Constructor
    List(const List<T>& otherList);

}
// In list.cpp

template <class T>
List<T>::List()
{
    head=NULL;
}

template <class T>
List<T>::List(const List<T>& otherList)
{
// I have code working for this part        
}

template <class T>
List<T>::~List()
{
}
// In stack.h (includes list.cpp)

template <class T>
class Stack
{
    List<T> list;

public:

    Stack();

    Stack(const Stack<T>& otherStack);

    ~Stack();

    void push(T item);
    T top();
    T pop();
};
// remember top(); pop(); push() functions are working properly in stack.cpp file.
// In stack.cpp (includes stack.h)

    Stack(const Stack<T>& otherStack){
    }
template <class T>
void Stack<T>::push(T item)
{
}

template <class T>
T Stack<T>::top()
{
}

template <class T>
T Stack<T>::pop()
{
}

there is an object s containing elem from 0 to 100. 100 being on the top.
Now we copy something like this:-

Stack<int> s2(s);

I don’t know how on Earth to access otherStack elements. I mean of course its a linked list. But it is in Stack.cpp, what can I do to access it and also how to make a copy constructor for this stack (Working code would be preferable). And Please be supportive this time. Thanks.
NOTE: You can’t change any constructor. It has to be the way it is. I hope every body this time gets my query.

  • 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-18T14:07:49+00:00Added an answer on June 18, 2026 at 2:07 pm

    Assuming that your List‘s copy constructor works correctly, your Stack should be fine using the copy constructor generated implicitly by the compiler. That being the case, the complete code for a stack can look something like this:

    template <class T>
    class Stack { 
        List<T> data;
    public:
        void push(T d) { data.add(data.begin(), d); }
        T front() { assert(!data.empty()); return data.front(); }
        void pop() { asset(!data.empty()); data.delete(data.begin()); }
    };
    

    We don’t have to define a copy constructor for Stack, because List’s copy constructor will be used to copy the data member (and that’s the only data member of Stack, so copying it is also sufficient to copy the Stack).

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

Sidebar

Related Questions

This is my first time making a hash table. I'm trying to associate strings
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
Constructor Conundrum, I have these two constructors. One is for making a copy of
I am having problem in writing copy constructor for pointers to objects. This is
I'm making my first D3D simple game. My game uses many images. This is
This is my first time making a custom rails generator and I want to
I'm making my first iphone application and it's a wine chart application. It's with
I am making my first android app. I want to save an array of
I am making my first android application, and it requires switching between activities very
I am making my first major iPhone app and need some advice in saving

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.