diff --git a/period_1/02-linked_list/201_T01_A01_Node_Class/Node.py b/period_1/02-linked_list/201_T01_A01_Node_Class/Node.py new file mode 100644 index 0000000..addea28 --- /dev/null +++ b/period_1/02-linked_list/201_T01_A01_Node_Class/Node.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +"""Linked Lists -> Node Implementation: Tutorial 1 + +The goal of this tutorial is to learn how to create a custom node data structure. +In its most basic form, each node contains: + * data, + * and a reference (in other words, a link) to the next node in the sequence +A node class contains setters and getters methods to set and retrieve data of a node. +Similarly, to set and get a reference of the next node in sequence, + +Your task is to: + * locate the TODOs in this file + * complete the missing part from the code + * run the test of this tutorial located in same folder. + +To test run 'Node_t.py' in your command line + +Notes: + * do not change class structure or method signature to not break unit tests + * visit this url for more information on linked list: + https://realpython.com/linked-lists-python/ +""" +class Node: + # TODO1: Set the initial values of the current Node + def __init__(self, data=None, next=None): + self.data = data + self.next = next + + # TODO 2: Return the value inside a node + def getData(self): + return self.data + + # TODO 3: change or set the value of the current Node + def setData(self, data): + self.data = data + + # TODO 4: Get the reference to next node in the list + def getNext(self): + return self.next + + # TODO 5: Set the reference to the next Node in the list + def setNext(self, next): + self.next = next \ No newline at end of file diff --git a/period_1/02-linked_list/201_T01_A01_Node_Class/Node_t.py b/period_1/02-linked_list/201_T01_A01_Node_Class/Node_t.py new file mode 100644 index 0000000..56290fa --- /dev/null +++ b/period_1/02-linked_list/201_T01_A01_Node_Class/Node_t.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +""" +This test case will verify if the provided solution by a student for Node.py is correct. +""" +from Node import * + +# Instantiate the first node +A = Node('A') + +# Instantiate another node +B = Node() +B.setData('B') + +print(A) +print(B) + +# Get data and the reference to next Node of the node A +print(A.getData()) +print(A.getNext()) + +# Get data and the reference to next Node of the node B +A.setNext(B) +tmp = A.getNext() +print(tmp.getData()) + +print(B.getNext()) \ No newline at end of file diff --git a/period_1/02-linked_list/202_T02_A02_LinkedList_Class/LinkedList.py b/period_1/02-linked_list/202_T02_A02_LinkedList_Class/LinkedList.py new file mode 100644 index 0000000..14396ab --- /dev/null +++ b/period_1/02-linked_list/202_T02_A02_LinkedList_Class/LinkedList.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 +"""Linked Lists -> Linked List Implementation: Tutorial 2 + +The goal of this tutorial is to learn how to create a custom singly linked list data structure. +This data structure consists of a collection of nodes which together represent a sequence. +The LinkedList class should contain methods that can be performed on a linked lists: +Those methods include insertion, deletion and traversal. +In this tutorial we will implement only a selection of those methods such as: +insertion, length and traversal + +Your task is to: + * locate the TODOs in this file + * complete the missing part from the code + * run the test of this tutorial located in same folder. + +To test run LinkedList_t.py in your command line' + +Notes: + * do not change class structure or method signature to not break unit tests + * visit this url for more information on linked list: + https://realpython.com/linked-lists-python/ +""" +class Node: + def __init__(self, data=None, next=None): + self.data = data + self.next = next + + def getData(self): + return self.data + + def getNext(self): + return self.next + + def setData(self, data): + self.data = data + + def setNext(self,next): + self.next = next + +class LinkedList: + + def __init__(self): + self.first = None + + def append(self, item): + #TODO 1: Append an item to the end of the list + if self.first is None: + self.first = item + else: + current = self.first + + while current.getNext() is not None: + current = current.getNext() + + current.setNext(item) + + def getLen(self): + #TODO 2: Find the length of the list and return it + current = self.first + count = 1 + + while current.getNext() is not None: + count += 1 + current = current.getNext() + + return count + + def printAll(self): + #TODO 3: Traverse through all elements in the list from the head to the end and print each value + current = self.first + print(current.getData()) + + while current.getNext() is not None: + current = current.getNext() + print(current.getData()) \ No newline at end of file diff --git a/period_1/02-linked_list/202_T02_A02_LinkedList_Class/LinkedList_t.py b/period_1/02-linked_list/202_T02_A02_LinkedList_Class/LinkedList_t.py new file mode 100644 index 0000000..eed5a47 --- /dev/null +++ b/period_1/02-linked_list/202_T02_A02_LinkedList_Class/LinkedList_t.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +""" +This test case will verify if the provided solution by a student for LinkedList.py is correct. +""" +from LinkedList import * + +# Instantiate a node and set a value +new_node = Node() +new_node.setData('A') + +# Instantiate a linked list +llst1 = LinkedList() +# Insert a node to the list +llst1.append(new_node) +# Traverse through the list and print values +llst1.printAll() +# Print the length of the list +print(llst1.getLen()) + +# Instantiate aother node and set a value +new_node = Node('B') +# Insert another node to the list +llst1.append(new_node) +# Traverse through the list and print values +llst1.printAll() +# Print the length of the list +print(llst1.getLen()) + +# Insert multiple values to the list +for item in ['C', 'D', 'E', 'F']: + new_node = Node(item, None) + llst1.append(new_node) + +# Print all values in the list and its length +llst1.printAll() +print(llst1.getLen()) \ No newline at end of file diff --git a/period_1/02-linked_list/203_EX1_A03_Song_LinkedList/SongList.py b/period_1/02-linked_list/203_EX1_A03_Song_LinkedList/SongList.py new file mode 100644 index 0000000..78f2cf0 --- /dev/null +++ b/period_1/02-linked_list/203_EX1_A03_Song_LinkedList/SongList.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +"""Linked Lists -> Song List Implementation: Exercise 1 + +The goal of this exercise is to learn how to create a custom linked list for songs. +Each node in this linked list represent a song. +This data structure consists of a collection of songs(node) which together represent a playlist. +The SongList class contains methods to insert a song, and traversal through the list to print titles. + +Your task is to: + * locate the TODOs in this file + * complete the missing part from the code + * run the test of this tutorial located in same folder. + +To test run 'SongList_t.py' in your command line + +Notes: + * do not change class structure or method signature to not break unit tests + * visit this url for more information on linked list: + https://realpython.com/linked-lists-python/ +""" +class SongNode: + def __init__(self, song_title=None, next = None): + self.song_title = song_title + self.next = next + +class SongList: + def __init__(self): + self.head = None + + def printSongs(self): + # Traverse through the list and print every song titles + current = self.head + while current != None: + print(current.song_title) + current = current.next + + def AddNewSong(self, new_song_title): + # Insert a new song title to the end of the list + new_song = SongNode(new_song_title) + if self.head == None: + self.head = new_song + return + current = self.head + while current.next != None: + current = current.next + current.next = new_song \ No newline at end of file diff --git a/period_1/02-linked_list/203_EX1_A03_Song_LinkedList/SongList_t.py b/period_1/02-linked_list/203_EX1_A03_Song_LinkedList/SongList_t.py new file mode 100644 index 0000000..ab4aab4 --- /dev/null +++ b/period_1/02-linked_list/203_EX1_A03_Song_LinkedList/SongList_t.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +""" +This test case will verify if the provided solution by a student for SongList.py is correct. +""" +from SongList import * + +# Instantiate a song list +linkedlist = SongList() +print(linkedlist) + +# Instantiate some nodes with values +linkedlist.head = SongNode("A Hard Day's Night") +second = SongNode('A Day in the Life') +third = SongNode("Strawberry Fields Forever") + +# Link nodes instances +linkedlist.head.next = second +second.next = third +third.next = None + +# Traverse through the list and print each song title +linkedlist.printSongs() + +# Insert more songs +linkedlist.AddNewSong("She Loves You") +linkedlist.AddNewSong("Something") +linkedlist.printSongs() \ No newline at end of file diff --git a/period_1/02-linked_list/204_HW1_A04_LinkedList_Class/LinkedList.py b/period_1/02-linked_list/204_HW1_A04_LinkedList_Class/LinkedList.py new file mode 100755 index 0000000..3af2125 --- /dev/null +++ b/period_1/02-linked_list/204_HW1_A04_LinkedList_Class/LinkedList.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +"""Linked List -> Extended Linked List Implementation: Homework + +The goal of this homework is to implement a singly linked list data structure with additional functionalities. +In previous tutorials you have learned how a node and a linked list data structure in its basic form can be created. +However, a LinkedList class can have more methods to perform additional operations on a linked list, +such as: insertion (begin, end, or after a specific element), deletion, traversal, and sorting. + +Your task is to: + * locate the TODOs in this file + * complete the missing part from the code + * run the test of this homework located in same folder. + +To test run LinkedList_t.py in your command line' + +Notes: + * do not change class structure or method signature to not break unit tests + * visit this url for more information on linked list: + https://realpython.com/linked-lists-python/ +""" + +# Teuntje van Leeuwe 1007734 +# Ryan Bakkes 1001519 +class Node: + def __init__(self, data): + self.data = data + self.next = None + +class LinkedList: + def __init__(self): + self.head = None + + #TODO 1:Insert at the beginning of the list + def insertBeg(self, new_data): + new_node = Node(new_data) + new_node.next = self.head + self.head = new_node + + #TODO 2: Insert at the end + def insertEnd(self, new_data): + new_node = Node(new_data) + if self.head == None: + self.head = new_node + return + current = self.head + while current.next != None: + current = current.next + current.next = new_node + + #TODO 3: Insert after a specific node + def insertAfter(self, data, new_data): + # Insert after a specific node + new_node = Node(new_data) + current = self.head + while current != None: + if current.data == data: + new_node.next = current.next + current.next = new_node + return + current = current.next + + #TODO 4: Deleting a node at a specific index + def deleteIndex(self, index): + current = self.head + x = 0 + + if x == index: + self.head = current.next + return + + while current != None: + if x == index - 1: + current.next = current.next.next + return + current = current.next + x += 1 + + #TODO 5: Search an element + def find(self, key): + current = self.head + x = 0 + while current != None: + if current.data == key: + return x + current = current.next + x += 1 + return -1 + + #TODO 6: Sort the linked list + def sort(self, head): + current = head + index = None + + while current != None: + index = current.next + while index != None: + if current.data > index.data: + temp = current.data + current.data = index.data + index.data = temp + index = index.next + current = current.next + + #TODO 7: Print the linked list + def printList(self): + current = self.head + while current != None: + print(current.data) + current = current.next \ No newline at end of file diff --git a/period_1/02-linked_list/204_HW1_A04_LinkedList_Class/LinkedList_t.py b/period_1/02-linked_list/204_HW1_A04_LinkedList_Class/LinkedList_t.py new file mode 100755 index 0000000..edcf4a3 --- /dev/null +++ b/period_1/02-linked_list/204_HW1_A04_LinkedList_Class/LinkedList_t.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +""" +This test case will verify if the provided homework solution by a student for LinkedList.py is correct. +""" +from LinkedList import * + +# Instantiate an empty list +llist = LinkedList() + +# Insert some integer values in a specific order +llist.insertEnd(1) # Linked List: 1 +llist.insertBeg(2) # Linked List: 2 1 +llist.insertBeg(3) # Linked List: 3 2 1 +llist.insertEnd(4) # Linked List: 3 2 1 4 + +llist.insertAfter(2, 4) # This call will insert 4 after the first occurrence 2 (if there are more than one 2) + # Linked List: 3 2 4 1 4 +llist.insertAfter(4, 5) # This call will insert 6 after the first occurrence 4 (if there are more than one 2) + # Linked List: 3 2 4 6 1 4 +# 324514 +llist.printList() + +# Delete item at position 2 (3rd element) (list starts from position 0) +llist.deleteIndex(2) +# 32514 +llist.printList() + +# Find a node with a specific value and print its index +item_to_find = 2 +# 1 +index = llist.find(item_to_find) +if index !=-1: + print(str(item_to_find) + " is at index: " + str(index)) +else: + print(str(item_to_find) + " is not found ") + +# Sort the list and print all values after sorting +llist.sort(llist.head) +# 12345 +llist.printList() \ No newline at end of file