Open vvgigg opened 1 year ago
Chapter : 5 - item : 2 - รู้จักกับ Doubly Linked List ให้เขียนคลาสของ Doubly Linked List ซึ่งมีเมท็อดดังนี้
ถ้าน้องยังไม่ค่อยเข้าใจการทำงานของ insert ให้น้องลองกับ List บน Python ได้ เช่น
โดยรูปแบบ Input มีดังนี้
โดยให้เพิ่มเติมจากส่วน #Code Here ของโปรแกรมต่อไปนี้ เพื่อให้สามารถแสดงผลได้ตามที่โจทย์กำหนด **** ห้ามใช้ List ในการทำ Linked List เด็ดขาดถ้าหากพบจะถูกลดเป็น 0 คะแนน ****
class Node: def init(self, value): self.value = value self.next = None self.previous = None
class LinkedList: def init(self): self.head = None self.tail = None
def __str__(self):
if self.isEmpty():
return "Empty"
cur, s = self.head, str(self.head.value) + " "
while cur.next != None:
s += str(cur.next.value) + " "
cur = cur.next
return s
def reverse(self):
if self.isEmpty():
return "Empty"
cur, s = self.tail, str(self.tail.value) + " "
while cur.previous != None:
s += str(cur.previous.value) + " "
cur = cur.previous
return s
def isEmpty(self):
return self.head == None
def append(self, item):
#Code Here
def addHead(self, item):
#Code Here
def insert(self, pos, item):
#Code Here
def search(self, item):
#Code Here
def index(self, item):
#Code Here
def size(self):
#Code Here
def pop(self, pos):
#Code Here
L = LinkedList() inp = input('Enter Input : ').split(',') for i in inp: if i[:2] == "AP": L.append(i[3:]) elif i[:2] == "AH": L.addHead(i[3:]) elif i[:2] == "SE": print("{0} {1} in {2}".format(L.search(i[3:]), i[3:], L)) elif i[:2] == "SI": print("Linked List size = {0} : {1}".format(L.size(), L)) elif i[:2] == "ID": print("Index ({0}) = {1} : {2}".format(i[3:], L.index(i[3:]), L)) elif i[:2] == "PO": before = "{}".format(L) k = L.pop(int(i[3:])) print(("{0} | {1}-> {2}".format(k, before, L)) if k == "Success" else ("{0} | {1}".format(k, L))) elif i[:2] == "IS": data = i[3:].split() L.insert(int(data[0]), data[1]) print("Linked List :", L) print("Linked List Reverse :", L.reverse())
Chapter : 5 - item : 3 - MergeOrderList จงเขียนฟังก์ชั่นสำหรับการ Merge LinkList 2 ตัวเข้าด้วยกันโดยห้ามสร้าง Class LinkList จะมีแต่ Class Node ซึ่งเก็บค่า value ของตัวเองและ Node ถัดไป โดยมีฟังก์ชั่นดังนี้
createList() สำหรับการสร้าง LinkList ที่รับ List เข้ามาโดยจะ return Head ของ Linklist
printList() สำหรับการ print LinkList โดยจะรับค่าเป็น head ของ Linklist และจะทำการ print ทุกตัวที่อยู่ใน Linklist ต่อจาก head จนครบทุกตัว
mergeOrderList() สำหรับการ merge linklist 2 ตัวเข้าด้วยกันโดยให้นำมาต่อกันโดยเรียงตามค่า value โดยที่ให้รับ parameter 2 ตัว และจะ return Head ของ Linklist ที่ทำการ merge แล้ว
ห้ามใช้ sort() หากพบข้อนี้จะไม่ได้คะแนน
ห้ามสร้าง Class LinkList
class node: def init(self,data,next = None ):
def __str__(self):
### Code Here ###
def createList(l=[]):
def printList(H):
def mergeOrderesList(p,q):
#################### FIX comand ####################
########### input only a number save in L1,L2 #############
LL1 = createList(L1)
LL2 = createList(L2)
print('LL1 : ',end='')
printList(LL1)
print('LL2 : ',end='')
printList(LL2)
m = mergeOrderesList(LL1,LL2)
print('Merge Result : ',end='')
printList(m)
Chapter : 5 - item : 4 - VIM Text Editor
Chapter : 5 - item : 5 - Radix Sort (มากไปน้อย)
Chapter : 5 - item : 1 - Locomotive-(101)