Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Singly Linked List Help implementing append, insert and pop into linked list class The implementation should define the following classes: Node Class: Fields: data,

Python Singly Linked List 

Help implementing append, insert and pop into linked list class

The implementation should define the following classes:

Node Class:

Fields: data, next

Methods: __init__(self, data), get_data(self), set_data(self, new_data), get_next(self), set_next(self, new_next), __str__(self)

LinkedList Class:

Fields: head,

Methods: __init__(self), add(self, item), search(self, item), remove(self, item), size(self), is_empty(self), __str__(self), append(self, item), insert(self, index, item), pop(self, index=None)

 class Node: def __init__(self, value): self.data = value self.next = None def get_data(self): return self.data def set_data(self, new_data): self.data = new_data def get_next(self): return self.next def set_next(self, new_next): self.next = new_next def __str__(self): return str(self.data) # LinkedList class class LinkedList: def __init__(self): self.head = None def add(self, item): temp = Node(item) temp.set_next(self.head) self.head = temp temp = None def search(self, item): current = self.head found = False while current is not None and not found: if current.get_data() == item: found = True else: current = current.get_next() current = None return found def remove(self, item): current = self.head previous = None found = False while current is not None and not found: if current.get_data() == item: found = True else: previous = current current = current.get_next() if found: if previous is None: self.head = current.get_next() current.set_next(None) else: previous.set_next(current.get_next()) current.set_next(None) current = None previous = None def size(self): current = self.head count = 0 while current is not None: count = count + 1 current = current.get_next() return count def is_empty(self): return self.head is None def __str__(self): list_str = "" current = self.head while current is not None: list_str += str(current.get_data()) + "->" current = current.get_next() list_str += "None" return list_str def append(self, item): def insert(self, index, item): def pop(self, index=None): def main(): groceries = LinkedList() groceries.add("Eggs") groceries.add("Milk") groceries.add("Apples") print(groceries) print("Is LinkedList empty?", groceries.is_empty()) print("LinkedList size:", groceries.size()) print("Search - Milk. Result:", groceries.search("Milk")) groceries.remove("Milk") print(groceries) print("Search - Milk, Result:", groceries.search("Milk")) groceries.add("Cheese") print(groceries) groceries.append("Yogurt") print(groceries) groceries.insert("Bread") print(groceries) groceries.pop("Bread") print(groceries) main()

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

Databases Illuminated

Authors: Catherine M. Ricardo

1st Edition

0763733148, 978-0763733148

More Books

Students also viewed these Databases questions