Thread: Creating Linked List using inheritance or composition?

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    22

    Creating Linked List using inheritance or composition?

    I know I can create a stack or queue from my base class of a linked list through composition or inheritance. My question is can I do this the other way around, I know most functions can be done, but I m particularly intrigued by removefromback since both stacks and queues remove from front. Would I have to create this function from scratch or is there a work around using inheritance or composition.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,390
    A linked list is neither a stack nor a queue, and I do not see why a linked list would have a stack or a queue, so this does not make sense to me.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    22
    I know, I'm talking about the use of its functions. A push function from stack could be used for insertatfront for a linked list etc... Am i making sense?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,600
    If you're asking if you can simulate a LL using a stack, the answer would be no (well not easily anyway).

    A LL supports insertion at any point in the list.

    I suppose you could fake random insertion by popping everything off one stack until you reach the insert point, put the new item in, then push everything back again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to implement linked list using inheritance
    By progmateur in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2014, 07:17 AM
  2. Inheritance of objects and linked list
    By rakeshkool27 in forum C++ Programming
    Replies: 7
    Last Post: 11-11-2010, 12:39 PM
  3. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM
  4. Inheritance with a linked list
    By myramains in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2002, 06:06 PM
  5. inheritance vs composition
    By razrektah in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 11:13 AM