Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

''' >>> lst = LinkedList() >>> lst.add(4) >>> lst.add(5) >>> lst.add(6) >>> lst Head:Node(6) Tail:Node(4) List:6 -> 5 -> 4 >>> lst.duplicate(6) >>> lst Head:Node(6)

''' >>> lst = LinkedList() >>> lst.add(4) >>> lst.add(5) >>> lst.add(6) >>> lst Head:Node(6) Tail:Node(4) List:6 -> 5 -> 4 >>> lst.duplicate(6) >>> lst Head:Node(6) Tail:Node(4) List:6 -> 6 -> 5 -> 4 >>> lst.duplicate(13) >>> lst Head:Node(6) Tail:Node(4) List:6 -> 6 -> 5 -> 4 >>> lst.add(1) >>> lst.duplicate(6) >>> lst Head:Node(1) Tail:Node(4) List:1 -> 6 -> 6 -> 6 -> 6 -> 5 -> 4 >>> lst.duplicate(4) >>> lst Head:Node(1) Tail:Node(4) List:1 -> 6 -> 6 -> 6 -> 6 -> 5 -> 4 -> 4 '''

class Node:

def __init__(self, value):

self.value = value

self.next = None

def __str__(self):

return "Node({})".format(self.value)

__repr__ = __str__

class LinkedList:

def __init__(self):

self.head=None

self.tail=None

def __str__(self):

temp=self.head

out=[]

while temp:

out.append(str(temp.value))

temp=temp.next

out=' -> '.join(out)

return 'Head:{} Tail:{} List:{}'.format(self.head,self.tail,out)

__repr__=__str__

def isEmpty(self):

return self.head==None

def __len__(self):

current=self.head

count=0

while current is not None:

count += 1

current = current.next

return count

def add(self, value):

newNode=Node(value)

if self.isEmpty():

self.head=newNode

self.tail=newNode

else:

newNode.next=self.head

self.head=newNode

def duplicate(self, item):

--- Start Here ---

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

Students also viewed these Databases questions

Question

Ensure continued excellence in people management.

Answered: 1 week ago

Question

13-6 How will MIS help my career?

Answered: 1 week ago