MA214 Algorithms and Data Structures 2023/24 Exercises 6Python

Java Python MA214 Algorithms and Data Structures

2023/24

Exercises 6

(linked lists, stacks, queues, hashing)

Exercise 6.1. Linked lists                                  4 pts

Consider the implementation of a singly linked list LinkedList.py, discussed in the lec-ture 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.

1 class Node :

2 def __init__ ( self , item , next ) :

3 self . item = item

4 self . next = next

5

6 class LinkedList :

7 def __init__ ( self ) :

8 self . head = None

9

10 def isEmpty ( self ) :

11 return self . head == None

12

13 def add ( self , item ) :

14 temp = Node ( item , self . head )

15 self . head = temp

(a) Explain in words which changes would be required to have a head and a tail pointer. Implement these changes in Python.

(b) Explain in words how to remove an element from the front of a singly linked list. Implement this method in Py MA214 Algorithms and Data Structures 2023/24 Exercises 6Python thon. What is the time complexity of this method? Justify your answer.

(c) Explain how to remove the element at the end of the singly linked list. Implement this method in Python. What is the time complexity of this method? Justify your answer.

Exercise 6.2. Stacks and queues                                3 pts

(a) Explain how to implement a queue using two stacks. Analyse the running time of the queue operations enqueue and dequeue.

(b) Explain how to implement a stack using two queues. Analyse the running time of the stack operations pop and push.

Exercise 6.3. Hashing                                                  3 pts

Suppose you need to insert unique 3-character IDs into a hash table, where each ID is made up of some combination of two of the capital letters A-D, followed by one of the lower case letters x-z, such as: ABx, DCy, BBz, etc. Repeat letters are allowed in an ID.

(a) How many unique 3-character IDs are there?

(b) What is the smallest length of the keys list for which we can guarantee that no LinkedList in keys has length more than 2?

(c) Describe a hash function for this setting that guarantees that no key collides with more than one other key         

  • 9
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构和算法是计算机科学中非常重要的概念和工具。它们用来解决各种计算问题,并帮助我们设计高效的程序。 在Python中,有许多常用的数据结构和算法可用于处理和操作数据。以下是一些常见的数据结构和算法: 1. 数组(Array):数组是一种存储固定大小元素的数据结构。在Python中,我们可以使用列表(List)来表示数组。 2. 链表(Linked List):链表是一种动态数据结构,它通过节点(Node)的方式来存储和访问数据。在Python中,我们可以使用列表(List)或自定义类来实现链表。 3. 栈(Stack)和队列(Queue):栈和队列都是一种特殊的数据结构,用来存储和访问数据。在Python中,我们可以使用列表(List)来实现栈和队列。 4. 树(Tree)和图(Graph):树和图是一种常见的非线性数据结构,用来表示层次关系和连接关系。在Python中,我们可以使用自定义类来实现树和图。 5. 排序算法:排序算法是一种将数据按照特定顺序进行排序的算法。在Python中,我们可以使用内置的sorted()函数来进行排序,或者实现一些常见的排序算法,如冒泡排序、插入排序、选择排序、快速排序等。 6. 查找算法:查找算法是一种在数据集中查找指定元素的算法。在Python中,我们可以使用内置的in关键字来进行简单的查找,或者实现一些高级的查找算法,如二分查找、哈希表等。 除了上述提到的数据结构和算法外,Python还提供了许多其他实用的数据结构和算法,如堆(Heap)、哈希表(Hash Table)、图算法(Graph Algorithms)等,可以根据具体问题选择合适的工具。 总而言之,了解和掌握数据结构和算法对于Python开发者来说是非常重要的,它们可以帮助我们提高程序效率、解决复杂的计算问题,并在面试和竞赛中展示出色的编程能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值