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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:51:54+00:00 2026-06-10T08:51:54+00:00

How to create generic stack in python? My stack implementation in python: class Node(object):

  • 0

How to create generic stack in python?
My stack implementation in python:

class Node(object):
    def __init__(self, d):
        self.data = d
        self.nextNode = None

class Stack(object):
    def __init__(self):
        self.top = None

    def push(self, item):
        newNode = Node(item)
        newNode.nextNode = self.top
        self.top = newNode

    def pop(self):
        if self.top == None:
            return None
        item = self.top.data
        self.top = self.top.nextNode
        return item

Now I am putting objects of class Node, but how implement generic Stack so that I can put there anything. For example, if I want create new type of nodes

class NodeWithMin:
    def __init__(self, value, minval):
        self.data = value
        self.minval = minval

And be able create stack based on these type of nodes, so it should be something like this (of course it does not work):

class StackWithMin(qs.Stack):
    def push(self, val):
        if self.peek() != None:
            minval = min(self.peek().value, val)
        else:
            minval = val
        qs.Stack.push(NodeWithMinV2(val, minval))

any idea?

EDIT:
it did not work because I have next error:

unbound method push() must be called with Stack instance as first argument (got NodeWithMinV2 instance instead)

I missed self

  • 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-10T08:51:56+00:00Added an answer on June 10, 2026 at 8:51 am

    You can just use a list, but I totally understand how you might want something better abstracted.

    You can also use a deque, in the collections module. It can add or remove from either end, and is better abstracted than a list.

    What you have looks good. If you want to put something with a minimum in it, just create a new class, and pass instances of it into your push method as the item argument.

    I’m kind of guessing here, but if what you want is a priority queue, where you can pop the least valued node rather than just the thing at a given end by push chronology, you could check out the heapq module. It’s not terrifically abstracted either, but it works, and it’s fast, and there’s nothing to stop you from abstracting it better yourself.

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

Sidebar

Related Questions

Is it possible to create generic class as following? public class AnyClassTypeWithValue<Class<T>, E> {
I want to create a generic method to serizlize a class to text (for
I want to create a generic IEnumerable implementation, to make it easier to wrap
I'm attempting to create a generic controller, ie: public class MyController<T> : Controller where
I'm trying to create a generic class in PHP that will provide a way
I want to create a generic stack. I want to implement it with a
I can create a stack class quite easily, using push and pop accessor methods
How to create a generic method which can call overloaded methods? I tried but
Trying to create a generic video plugin for Expression Engine 2 where I can
I want to create a generic html table to excel file view that can

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.