Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

WHAT IS THE PROBLEM WITH THIS CODE? We have used the node structure and a linked-list structure as shown below: Class Node: def __init__(self,initdata): self.data

WHAT IS THE PROBLEM WITH THIS CODE?

We have used the node structure and a linked-list structure as shown below:

Class Node:

def __init__(self,initdata):

self.data = initdata

self.next = none

Class OrderedList:

def __init__(self):

self.head = None

For an ordered linked-list, write an algorithm called count which will count the number of occurrences of a specific piece of data. First, you have to create the linked list using the following input (note: this should be an ordered list - how will we do this?).

2, 5, 10, 14, 18, 23, 23, 23, 25, 26, 29, 31, 35, 37.

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

def getData(self): return self.data

def getNext(self): return self.next

def setData(self,newdata): self.data = newdata

def setNext(self,newnext): self.next = newnext

#This is ordered linked list where all the elements in the linked list are sorted

class OrderedList: def __init__(self): self.head = None

#add method is responsible for adding items at the correct position in linked list def add(self,item) : #start iterating over linked list from head node curr = self.head prev = None #This flag will be used to stop the iteration when the correct position is found stop = False while curr != None and not stop: #If we found node with large value then item then we found the correct position so now need to stop the loop if curr.getData() > item: stop = True else: #If we have not found correct position then need to move forward prev = curr curr = curr.getNext() #creating node to insert in linked list temp = Node(item) #for first node linked list will be empty so need to set head node here if prev == None: temp.setNext(self.head) self.head = temp #if list is not empty then need to insert node between prev and curr else: temp.setNext(curr) prev.setNext(temp)

#count method return the count of any item in this linked list def count(self,item): count = 0 curr = self.head found = False #iterating till we found the node which has larger value than the given item while current != None: if found and current.getData() > item: break elif current.getData()==item: #Incrementing counter here count += 1 found = True #moving forward in the linked list current = current.getNext() return count

#driver code to test the functionality #2, 5, 10, 14, 18, 23, 23, 23, 25, 26, 29, 31, 35, 37 list = OrderedList() list.add(2) list.add(5) list.add(10) list.add(14) list.add(18) list.add(23) list.add(23) list.add(23) list.add(25) list.add(26) list.add(29) list.add(31) list.add(35) list.add(37) print(list.count(23))

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

Students also viewed these Databases questions