Sam1313
Sam1313

Reputation: 5

Why we make Node class and LinkedList class instead of doing it simple way like

i have just started learning data structures and algorithms and i want to know why do we make node and linkedlist classes separately rather doing everything in a single class like i did

class LinkedList():              
    def __init__(self):
        self.linked=[]

    def add(self,data):
        self.linked.append(data)
        return self.linked

    def get_size(self):
        return len(self.linked)

    def remove(self,data):
        if data in self.linked:
            while data in self.linked:
                z=self.linked.index(data)
                del self.linked[z]
                print self.linked
            return True
        else:
            return False
    def find(self,data):
        if data in self.linked:
            return "Found" + " " + str(data)
        else:
            return "Not found" + " " + str(data)

Is that because of space and time complexity or some other factors? Thanks again.

Upvotes: 0

Views: 205

Answers (2)

Chirag
Chirag

Reputation: 1

you can implement Linked list with traditional approach of c type structure using

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

type of definition. and using this class in Main Linked list class

class LinkedList( Node):

Upvotes: 0

Robin Krahl
Robin Krahl

Reputation: 5308

A linked list means that each element has a pointer (= link) to the next element in the list. A typical Node class stores that pointer. Your LinkedList class is just a wrapper for a standard Python list, not a linked list.

Upvotes: 1

Related Questions