Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add the following method to the UnorderedList class:Node-UnorderedList.py def count_greater(self, min): this method returns the count of elements greater than min. For instance, if list

Add the following method to the UnorderedList class:Node-UnorderedList.py

def count_greater(self, min): this method returns the count of elements greater than min.

For instance, if list aList had the elements 3, 4, 5, 10, 8, 2, 9, the call

aList.count_greater(8)would return 2 as both 10 and 9 are greater than 8..

Write a main method where you can test the method you added.

Unordered.py

# Implementation of an Unordered List ADT as a linked list.The list

# is accessed through a reference to the first element, head.

# Adopted from Section 3.9 of the textbook.

class Node:

'''

Create a Node object and initialize its data.

'''

def __init__(self, init_data):

self.data = init_data

self.next = None

'''

Accessor for node data

'''

def get_data(self):

return self.data

'''

Accessor for next reference

'''

def get_next(self):

return self.next

'''

Mutator for node data

'''

def set_data(self, new_data):

self.data = new_data

'''

Mutator for next reference

'''

def set_next(self, new_next):

self.next = new_next

class UnorderedList:

'''

List is empty upon creation and the head reference is None

'''

def __init__(self):

self.head = None

'''

Returns True if list is empty, False otherwise

'''

def is_empty(self):

return self.head == None

'''

Add an element to head of the list

'''

def add(self, item):

# Create a node using item as its data

temp = Node(item)

# make the next reference of the new node refer to the head

# of the list

temp.set_next(self.head)

# modify the list head so that it references the new node

self.head = temp

'''

Returns the size of the list

'''

def size(self):

# start at the head of the list

current = self.head

count = 0

# Traverse the list one element at a time.We know

# we reached the end when the next reference is None

while current != None:

count = count + 1

current = current.get_next()

return count

'''

Search for an item in the list.Returns True if found, False otherise.

'''

def search(self,item):

current = self.head

found = False

# As long as the element is not found and we haven't

# reached the end of the list

while current != None and not found:

if current.get_data() == item:

found = True

else:

# go to the next element

current = current.get_next()

return found

'''

Remove the first occurrence of item from the list.

'''

def remove(self, item):

# keep track of current and previous elements

current = self.head

previous = None

found = False

# traverse the list

while current != None and not found:

# if we have a match, stop

if current.get_data() == item:

found = True

# otherwise advance current and next references

else:

previous = current

current = current.get_next()

# the element to be deleted is the head of the list

if found:

if previous == None:

self.head = current.get_next()

# the element to be deleted is not the head

else:

previous.set_next(current.get_next())

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_2

Step: 3

blur-text-image_3

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions