0
class Node:
  def __init__(self, data = None, next = None):
    self.data = data
    self.next = next


class LinkedList(Node):
  def __init__(self, l_size = 0, head = None, tail = None):
    Node.__init__(self)
    self.l_size = 0
    self.head = head
    self.tail = tail

  def add(self, data):
    n = Node(data, None)
    if(self.l_size == 0):
      self.head.next = n
      self.head.data = n.data
    else:
      self.tail.next = n
      self.tail.data = n.data
      n = n.next
      print(n)
    self.tail = n 
    self.l_size += 1
    return True

l = LinkedList()
l.add(7)
l.add(8)
l.add(2)

I'm just trying to achieve (h)-> 7 -> 8 -> 2 <- (l) Where (h) and (l) are the head and tail pointers respectively. The way I'm implementing this the LL is basically the head and the tail pointers, the Nodes chain together on their own, that's why I made Node a super class.

2
  • In your example isn't head always None? So, in add(), head.next will fail. Commented Jun 6, 2017 at 9:32
  • You use the default value of head which is None, and it remains None throughout. So self.head.next won't work. Commented Jun 6, 2017 at 9:32

2 Answers 2

1

It should crash on the first addition - when you initialize your LinkedList you initialize it with self.head set to None (the default argument). Then when you call the add() method, since self.l_size is 0 you attempt to set self.head.next to the created node - but since it's set to None you'll get an AttributeError.

When your LinkedList is empty, you should set both its tail and head properties to the first added element, because in a list of length 1 both head and tail are the same element.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't get why you've decided to use this messy assignments. You should assign whole object and then move tail to next if needed:

class Node:
  def __init__(self, data = None, next = None):
    self.data = data
    self.next = next


class LinkedList:
  def __init__(self, l_size = 0, head = None, tail = None):
    Node.__init__(self)
    self.l_size = 0
    self.head = head
    self.tail = tail

  def add(self, data):
    n = Node(data, None)
    if self.l_size == 0:
      self.head = n
      self.tail = n
      print(n)
    else:
      self.tail.next = n
      self.tail = self.tail.next
      print(n)
    self.l_size += 1
    return True

l = LinkedList()
l.add(7)
l.add(8)
l.add(2)

EDIT deleted subclassing from Node

6 Comments

You should also drop the subclassing of LinkedList from Node, since you never use it and it doesn't make any sense anyway. After all, a linked list is not a node. I don't understand OP's point when he tries to explain why he did it this way.
I'm trying to make a library of datastructures. I figured that Node could be used in a lot of different places (Trees) so I thought it would be a good idea to break it up and make LinkedList just a Subclass of Node that just maintains pointers for its beginning and end. Could you elaborate on why this is a bad way to do it? Why is making everything one big object better?
Becaue the List consists of Nodes, but is not a Node. It's inheritance in OOP. Imagine you have a class Car and a class Engine. A Car has (uses) engine, but is not an Engine. Therefore Car can be subclassed from say, class Vehicle but not Engine.
@gonczor could you be a little clearer I don't quite understand your example. In this case how am I subclassing Car from engine in essence? Are you saying that Node should be the Parent of LinkedList and not the other way around, Why?
No. If you have an object, it has certain properties. In your case you have a data structure consisting of data "cells". An abstract data structure should allow adding, deleting, searching these cells. OK? So, you have a data structure called LinkedList and Nodes being these cells. You data structure uses these cells to add them. The idea behind OOP and subclassing is allowing to reuse similar structures. But LinkedList and a Node are not similar. Node is a cell and LinkedList is a data structure. You could have BinaryTree and LinkedList and they would have some common properties
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.