Add chapter 2 and 4 labs for 2436 and database prog, respectively
This commit is contained in:
parent
1f87bd00ad
commit
f8b2dca3a3
18 changed files with 666 additions and 160 deletions
31
labs/cosc-2436/chapter-4/Python/Node.py
Normal file
31
labs/cosc-2436/chapter-4/Python/Node.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
class Node:
|
||||
# Initializes this node with the specified numerical data value, next
|
||||
# pointer, and previous pointer.
|
||||
def __init__(self, initial_data, next_node=None, previous_node=None):
|
||||
self.data = initial_data
|
||||
self.next = next_node
|
||||
self.previous = previous_node
|
||||
|
||||
# Returns this node's data.
|
||||
def get_data(self):
|
||||
return self.data
|
||||
|
||||
# Sets this node's data.
|
||||
def set_data(self, new_data):
|
||||
self.data = new_data
|
||||
|
||||
# Gets this node's next pointer.
|
||||
def get_next(self):
|
||||
return self.next
|
||||
|
||||
# Sets this node's next pointer.
|
||||
def set_next(self, new_next):
|
||||
self.next = new_next
|
||||
|
||||
# Gets this node's previous pointer.
|
||||
def get_previous(self):
|
||||
return self.previous
|
||||
|
||||
# Sets this node's previous pointer.
|
||||
def set_previous(self, new_previous):
|
||||
self.previous = new_previous
|
78
labs/cosc-2436/chapter-4/Python/README.md
Normal file
78
labs/cosc-2436/chapter-4/Python/README.md
Normal file
|
@ -0,0 +1,78 @@
|
|||
# 4.16 LAB: Sorted number list implementation with linked lists - Python
|
||||
|
||||
## Step 1: Inspect the Node.py file
|
||||
|
||||
Inspect the class declaration for a doubly-linked list node in Node.py. Access Node.py by clicking on the orange arrow next to main.py at the top of the coding window. The Node class has three attributes:
|
||||
|
||||
- a floating-point data value,
|
||||
- a reference to the next node, and
|
||||
- a reference to the previous node.
|
||||
|
||||
Node.py is read only, since no changes are required.
|
||||
Step 2: Implement the insert() method
|
||||
|
||||
A class for a sorted, doubly-linked list is declared in SortedNumberList.py. Implement the SortedNumberList class's insert() method. The method must create a new node with the parameter value, then insert the node into the proper sorted position in the linked list. Ex: Suppose a SortedNumberList's current list is `23 → 47.25 → 86`, then `insert(33.5)` is called. A new node with data value 33.5 is created and inserted between 23 and 47.25, thus preserving the list's sorted order and yielding: `23 → 35.5 → 47.25 → 86`
|
||||
|
||||
## Step 3: Test in develop mode
|
||||
|
||||
Code in main() takes a space-separated list of numbers and inserts each into a SortedNumberList. The list is displayed after each insertion. Ex: If input is
|
||||
|
||||
```txt
|
||||
77 15 -42 63.5
|
||||
```
|
||||
|
||||
then output is:
|
||||
|
||||
```txt
|
||||
List after inserting 77:
|
||||
77
|
||||
List after inserting 15:
|
||||
15 77
|
||||
List after inserting -42:
|
||||
-42 15 77
|
||||
List after inserting 63.5:
|
||||
-42 15 63.5 77
|
||||
```
|
||||
|
||||
Try various program inputs, ensuring that each outputs a sorted list.
|
||||
|
||||
## Step 4: Implement the remove() method
|
||||
|
||||
Implement the SortedNumberList class's remove() method. The method takes a parameter for the number to be removed from the list. If the number does not exist in the list, the list is not changed and false is returned. Otherwise, the first instance of the number is removed from the list and true is returned.
|
||||
|
||||
Uncomment the commented-out part in main() that reads a second input line and removes numbers from the list. Test in develop mode to ensure that insertion and removal both work properly, then submit code for grading.
|
||||
|
||||
Ex: If input is
|
||||
|
||||
```txt
|
||||
84 72 19 61
|
||||
19 84
|
||||
```
|
||||
|
||||
then output is:
|
||||
|
||||
```txt
|
||||
List after inserting 84:
|
||||
84
|
||||
List after inserting 72:
|
||||
72 84
|
||||
List after inserting 19:
|
||||
19 72 84
|
||||
List after inserting 61:
|
||||
19 61 72 84
|
||||
List after removing 19:
|
||||
61 72 84
|
||||
List after removing 84:
|
||||
61 72
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```txt
|
||||
PASS: Inserting 25.5 yields ( 25.5 )
|
||||
PASS: Inserting 75.75 yields ( 25.5, 75.75 )
|
||||
PASS: Inserting 88.5 yields ( 25.5, 75.75, 88.5 )
|
||||
FAIL: Insert 30:
|
||||
Expected list: ( 25.5, 30, 75.75, 88.5 )
|
||||
Actual list: ( 30, 75.75, 88.5 )
|
||||
```
|
63
labs/cosc-2436/chapter-4/Python/SortedNumberList.py
Normal file
63
labs/cosc-2436/chapter-4/Python/SortedNumberList.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
from Node import Node
|
||||
|
||||
|
||||
class SortedNumberList:
|
||||
def __init__(self):
|
||||
self.head = None
|
||||
self.tail = None
|
||||
|
||||
# Inserts the number into the list in the correct position such that the
|
||||
# list remains sorted in ascending order.
|
||||
def insert(self, number):
|
||||
if self.head == None:
|
||||
self.head = Node(number)
|
||||
self.tail = self.head
|
||||
else:
|
||||
current_node = self.head
|
||||
new_node = Node(number)
|
||||
while True:
|
||||
# handles most stuff
|
||||
# probably got an error cuz this is confusing
|
||||
if current_node != None and current_node.get_data() >= number:
|
||||
new_node.set_next(current_node)
|
||||
new_node.set_previous(current_node.get_previous())
|
||||
|
||||
if new_node.get_previous() != None:
|
||||
new_node.get_previous().set_next(new_node)
|
||||
else:
|
||||
self.head = new_node
|
||||
|
||||
current_node.set_previous(new_node)
|
||||
break
|
||||
|
||||
# handles being at the end
|
||||
if current_node == None:
|
||||
self.tail.set_next(new_node)
|
||||
new_node.set_previous(self.tail)
|
||||
self.tail = new_node
|
||||
break
|
||||
|
||||
current_node = current_node.get_next()
|
||||
|
||||
# Removes the node with the specified number value from the list. Returns
|
||||
# True if the node is found and removed, False otherwise.
|
||||
def remove(self, number):
|
||||
current_node = self.head
|
||||
while True and current_node != None:
|
||||
if current_node.get_data() == number:
|
||||
next_node = current_node.get_next()
|
||||
previous_node = current_node.get_previous()
|
||||
if next_node != None:
|
||||
next_node.set_previous(previous_node)
|
||||
else:
|
||||
self.tail = previous_node
|
||||
|
||||
if previous_node != None:
|
||||
previous_node.set_next(next_node)
|
||||
else:
|
||||
self.head = next_node
|
||||
return True
|
||||
|
||||
current_node = current_node.get_next()
|
||||
|
||||
return False
|
BIN
labs/cosc-2436/chapter-4/Python/__pycache__/Node.cpython-311.pyc
Normal file
BIN
labs/cosc-2436/chapter-4/Python/__pycache__/Node.cpython-311.pyc
Normal file
Binary file not shown.
Binary file not shown.
35
labs/cosc-2436/chapter-4/Python/main.py
Normal file
35
labs/cosc-2436/chapter-4/Python/main.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
from SortedNumberList import SortedNumberList
|
||||
|
||||
|
||||
def main():
|
||||
# Read the line of input numbers
|
||||
terms = input().split()
|
||||
|
||||
sorted_list = SortedNumberList()
|
||||
# Insert each value and show the sorted list's contents after each insertion
|
||||
for term in terms:
|
||||
number = float(term)
|
||||
print(f"""List after inserting {number}: """)
|
||||
sorted_list.insert(number)
|
||||
print_list(sorted_list)
|
||||
|
||||
# Read the input line with numbers to remove
|
||||
terms = input().split()
|
||||
for term in terms:
|
||||
number = float(term)
|
||||
print(f"""List after removing {number}: """)
|
||||
sorted_list.remove(number)
|
||||
print_list(sorted_list)
|
||||
|
||||
|
||||
# Prints the sorted_number_list's contents, in order from head to tail
|
||||
def print_list(lst):
|
||||
node = lst.head
|
||||
while node != None:
|
||||
print(node.get_data(), end=' ')
|
||||
node = node.get_next()
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue