跳至主要內容

Exercise 6 LSE Home MA407

AI悦创原创2023年11月9日python 1v1数据结构一对一留学生辅导留学生作业辅导伦敦政经LSE Home大约 9 分钟...约 2552 字

Please submit the solutions to these questions on Gradescope in two separate Python files: LinkedList.py and SortedDLL.py.

Exercise 6.1

Consider the implementation of a singly linked list LinkedList.py, discussed in the lecture and available from Moodle. This implementation just has a head pointer. It also comes with just two methods: isEmpty(self) and add(self,item). The first one checks whether the linked list is empty; the latter one adds an item at the front of the linked list.

class Node:
    def __init__(self, item, next):
        self.item = item
        self.next = next
class LinkedList:
    def __init__(self):
        self.head = None
    def isEmpty(self):
        return self.head == None
    def add(self ,item):
        temp = Node(item, self.head)
        self.head = temp

(a) Suppose we want to have an additional tail pointer in our data structure. Implement the necessary changes in Python.

(b) Implement a method remove() to remove an element from the front of a singly linked list in Python. What is the time complexity of this method?

(c) Implement a method removeAtEnd() to remove the element at the end of the singly linked list in Python. What is the time complexity of this method?

Solution 6.1

code1

Exercise 6.2

This exercise asks you to implement a doubly linked list that is always sorted and allows multiple entries with the same value. Consider the implementation of the following functionalities of a sorted doubly linked list in file SortedDoublyLinkedList.py.

(a) Each item in the list stores an integer value, a pointer to the next item in the list and a pointer to the previous item in the list. Use a Node class to implement the list items and a Sdll class to represent a sorted doubly linked list object.

(b) Add a method insert(int x) (to Sdll class) inserts a new item with value x into the list, so that the list remains sorted (and each item keeps correct pointers to the next and previous items). Multiple entries in the list with the same value are allowed.

(c) Add a method delete(int x) (to Sdll class) deletes all items in the list with value x. (Use that the list is sorted to avoid scanning through the whole list if possible.)

(d) Add a method __str__ (to Sdll class) which returns a string representation of the list, in order from the start to the end. Also implement a method reverseString() which returns a string representation of the list, in reverse order, from the end to the start. (Having both of these methods allows you to check that your insert/delete operations keep the pointers in your list structure intact.)

The following code

mysortedlist = Sdll()
mysortedlist.insert(3)
mysortedlist.insert(1)
mysortedlist.insert(6)
mysortedlist.insert(3)
mysortedlist.insert(1)
mysortedlist.insert(3)
print(mysortedlist)
print(mysortedlist.reverseString())
mysortedlist.delete(3)
print(mysortedlist)
print(mysortedlist.reverseString())

should print

None <- 1 <-> 1 <-> 3 <-> 3 <-> 3 <-> 6 -> None
None <- 6 <-> 3 <-> 3 <-> 3 <-> 1 <-> 1 -> None

None <- 1 <-> 1 <-> 6 -> None
None <- 6 <-> 1 <-> 1 -> None

Solution 6.2

code1
公众号:AI悦创【二维码】

AI悦创·编程一对一

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh

C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh

方法一:QQ

方法二:微信:Jiabcdefh

通知
关于编程私教&加密文章