Điểm của bài asm còn tùy thuộc vào người chấm. Chỉ cần paraphase bài này là có thể đạt merit hoặc có thể đạt distinction tùy vào thầy dạy. 1 trong nhưng tool paraphase mình recommend là quillbot.ASSIGNMENT 1 FRONT SHEET Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 20 Advanced Programming Submission date Date Received 1st submission Re submission Date Date Rec.
Trang 1ASSIGNMENT 2 FRONT SHEET
Unit number and title Unit 19: Data Structures and Algorithms
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism I understand that making a false declaration is a form of malpractice
Student’s signature Grading grid
Trang 2 Summative Feedback: Resubmission Feedback:
Internal Verifier’s Comments:
IV Signature:
Trang 3Table of Contents
A INTRODUCTION 5
B IMPLEMENT COMPLEX DATA STRUCTURES AND ALGORITHMS 6
1 Singly vs Doubly Linked-List 6
1.1 Highlight some differences between Singly and Doubly Linked-List 6
1.2 Describe Singly and Doubly Linked-List’s operations 7
1.3 Implement Singly and Doubly Linked-List 8
2 Insert An Element In The Middle Of A Linked-List 16
2.1 Implementation 16
2.2 Explanation of the implementation 16
3 Sorting in Linked-List 18
3.1 Describe your selected sorting algorithm 18
3.2 Implementation 19
3.3 Explanation Of The Implementation 19
C IMPLEMENT ERROR HANDLING AND REPORT TEST RESULTS 22
1 Testing Plan 22
2 Evaluation 25
2.1 Overall: 25
2.2 Explain and Solution 25
D DISCUSS HOW ASYMPTOTIC ANALYSIS CAN BE USED TO ASSESS THE EFFECTIVENESS OF AN ALGORITHM 26
1 Asymptotic Analysis: 26
2 Asymptotic Notations And How They Relate To Ideas Of Best, Average And Worst Case: 26
3 Some Examples to Clarify O(1), O(n), O(N log N) 28
E DETERMINE TWO WAYS IN WHICH THE EFFICIENCY OF AN ALGORITHM CAN BE MEASURED, ILLUSTRATING YOUR ANSWER WITH AN EXAMPLE 31
F TRADE-OFF WHEN SPECIFYING AN ADT 33
G CONCLUSION 34
References 34
Trang 4List of Figures
Figure 1: Node structure of singly linked list 8
Figure 2: instance variables and constructor of linked list 9
Figure 3: addLast Operation and addFirst Operation of singly 9
Figure 4: singly Insert operation 10
Figure 5: Singly Search Operation: 10
Figure 6:getFirst and getLast operation 10
Figure 7: Size and isEmpty Operation of singly list 11
Figure 8: Singly RemoveFirst and RemoveLast Operation: 11
Figure 9: Node class of doubly linked list 12
Figure 10: Doubly linked list class 12
Figure 11: size and isEmpty operation of doubly 13
Figure 12: addFirst and addLast operations of doubly 13
Figure 13: search operation of doubly 14
Figure 14: Insert Doubly 14
Figure 15: GetFirst and Last doubly 15
Figure 16: Remove First and Last Operation doubly 15
Figure 17: InsertMiddle Doubly Linked List 16
Figure 18: Searching the previous node 17
Figure 19: adjust pointer of new node 18
Figure 20: Complete Inserting process 18
Figure 21: Sorting algorithm Implement 19
Trang 5A INTRODUCTION
In this assignment, there are some tasks I need to complete such as implementing a complex algorithm and data structure(singly and doubly linked list) that consists the valid operations Following that, I will implement error handling and report test results Additionally, I will discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm To more specific, taking an example to clarify some concepts of big-O notation Determining two ways in which the efficiency of an algorithm can be measured will be discussed and illustrating my answer with an example In the end, discussing about space-time trade-off when specifying an adt
Trang 6B IMPLEMENT COMPLEX DATA STRUCTURES AND ALGORITHMS
1 Singly vs Doubly Linked-List
1.1 Highlight some differences between Singly and Doubly Linked-List
Basically, both singly and doubly linked list could be understood as the characteristic variation of linked list Using Singly or Doubly linked list based on the purpose to achieve and system limitations Singly linked list is commonly viewed as the simple implementation of linked list ADT and doubly linked list is the more complex one Moreover, most of the strengths of this one also are the weakness of the other and vice versa In other words, they are put in the opposite assessment For more specific, here are the comparison table of these two:
which stores each data in a node and each node of singly linked list only has one pointer point to the next node
Doubly linked list also stores data in a node but each node of the doubly linked list has two pointers One point
to the previous node and one point to the next node
one pointer
Structure:
Node data
Next pointer(next node)
A node of this one has two pointers Structure:
Node data
Next pointer(next node)
Previous pointer(previous node)
node of a singly linked list only stores one pointer
It uses memory as twice as singly because its node store both the next pointer as well as the previous pointer
Searching the known position: Best caseO(1), average case: O(n), worst case O(n)
Easy at insertion Cannot access the previous node
RemoveLast operation needs O(1) Searching the known position: Best case (1), average case: O(1), worst case O(n)
More actions in insertion Easy to access the previous node
searching or insertion is not necessary, the singly linked list is more suitable
If we require greater efficiency when searching or inserting and memory is not an issue, we should use a doubly linked list
Trang 71.2 Describe Singly and Doubly Linked-List’s operations
a) Singly Linked List operations
addFirst(): insert an element saved to the new node before the current head of the linked list The next pointer of the new node will point to the current head The new head now is set to the new node
addLast(): insert an element saved to the new node after the current tail of the linked list The next pointer of the current tail will point to the new node The new tail now is set to the new node
Insert(): traversal and find the previous index node and set the next pointer of this node to the new node The next pointer of the new node is set to the next pointer of the previous node
removeFirst(): set the head pointer to the next node of the current head and set the next pointer of this one to null
removeLast(): traversal sequentially to the previous node of the tail pointer node Set the next pointer of the previous node to null
size(): return the size instance variable which is updated constantly whenever one change of data in the linked list happens
isEmpty(): if size is equal to 0, return true and vice versa
getFirst(): return the first pointer if the linked list Is not null
getLast(): return the last pointer if the linked list Is not null
Search(): traversal by looping via each element to find the element at the correct index and return this one
Sort(): apply the merge sort algorithm to sort the list ascendingly
b) Doubly Linked List operation
addFirst(): insert an element saved to the new node before the current head of the linked list The next pointer of the new node will point to the current head and the previous pointer of the current head to the new node The new head now is set to the new node
addLast(): insert an element saved to the new node after the current tail of the linked list The next pointer of the current tail will point to the new node and the previous pointer of the new node point to the current tail The new tail now is set to the new node
Insert(): traversal and find the previous index node and set the next pointer of this node to the new node as well as set the previous of the new node point to the previous index node The next pointer of the new node is set to the next pointer of the previous node and set the previous pointer of the node at the next pointer of the new node to the new node
Trang 8 removeFirst(): set the head pointer to the next node of the current head and set the next pointer of this one to null The head node now also is set its previous pointer to null
removeLast(): from the last node, back to the previous node via the previous pointer Set the next pointer of the previous node to null and set the previous pointer of the last node
as null The tail pointer now points to the previous node
size(): return the size instance variable which is updated constantly whenever one change
of data in the linked list happens
isEmpty(): if size is equal to 0, return true and vice versa
getFirst(): return the first pointer if the linked list Is not null
getLast(): return the last pointer if the linked list Is not null
Search(): traversal by looping via each element to find the element at the correct index and return this one
Sort(): apply the merge sort algorithm to sort the list ascendingly
1.3 Implement Singly and Doubly Linked-List
To demonstrate the implementation of the Singly-Doubly linked list, here are some code screenshots with the brief explanation of each function which have been written clearly in the code comment
a) Implement Singly Linked-List:
Node class:
Singly linked list instance variables and constructor:
Figure 1: Node structure of singly linked list
Trang 9 addLast Operation and addFirst Operation:
Insert operation:
Figure 2: instance variables and constructor of linked list
Figure 3: addLast Operation and addFirst Operation of singly
Trang 10 Search Operation:
getFirst and getLast operation:
Figure 4: singly Insert operation
Figure 5: Singly Search Operation:
Figure 6:getFirst and getLast operation
Trang 11 RemoveFirst and RemoveLast Operation:
Size and isEmpty operation:
Figure 8: Singly RemoveFirst and RemoveLast Operation:
Figure 7: Size and isEmpty Operation of singly list
Trang 12b) Implement Doubly Linked List:
Node class
Instance variables and constructor
Figure 9: Node class of doubly linked list
Figure 10: Doubly linked list class
Trang 13 addFirst and addLast operations:
size and isEmpty operation of doubly
Figure 12: addFirst and addLast operations of doubly
Figure 11: size and isEmpty operation of doubly
Trang 14 Search operation:
Insert Operation:
Figure 13: search operation of doubly
Figure 14: Insert Doubly
Trang 15 RemoveFirst and RemoveLast operation:
GetFisrt and GetLast operation:
Figure 16: Remove First and Last Operation doubly
Figure 15: GetFirst and Last doubly
Trang 162 Insert An Element In The Middle Of A Linked-List
Although there are two terminologies of linked-list(singly and doubly), it could be said that the doubly linked list is the upgraded version or extended version of the singly linked list In other words, when I implement one operation in the doubly linked list, this also means that I could implement it on the singly linked list at an easier level So, in this section, I will only show the implementation of inserting an element in the middle for the double link list
2.1 Implementation
2.2 Explanation of the implementation
To insert an element into the middle of doubly linked list, one of the crucial things is checking whether the node data is null or not If it’s null, throw error immediately If the list is empty or the size is equal to 1, it means this element inserted will be new head or new tail One of the essential steps is to find the previous element of the middle index(middle position of linked) This is because the storing mechanism of a linked list is very different from the other data structure such as array-list as well as array It stores data in each node and each node has the pointers to join the next or
Figure 17: InsertMiddle Doubly Linked List
Trang 17the previous node It means, it does not mark any index to the elements in this data structure except two pointers to identify this linked list(head pointer and tail pointer)
To find the correct middle index to insert the element, this is necessary to pass via each element and find the previous node(middle index - 1) and perform adjusting the pointer of this previous node and also the next node of this one To find the middle index, I need to check whether it is an even or odd number If it’s odd, increase the size to one digit and divide it by two(because the odd integer number when separated will provide the not really exact number)
In my source code, the finding process is hidden because I use the search operation of this doubly linked list adt However, the real process of this could be described basically as creating the prevNode variable to store and the count variable(called i) and a while loop to loop via the list until
it reaches the middle index – 1
For more illustration, here is the visualisation of the searching previous node process Assuming the index that needs to be inserted is the middle index(size = 7, middleindex = 3(one other way will take 4 is middle index but I prefer the way that calculate index is 3)) and the previous index is 2:
The loop passes via each element Each element passed will be stored temporarily to the prevNode and the I variable is increased via each time loop until the condition of the while loop does not meet, and the loop stop looping In the real implementation of my code, the search operation also could traversal elements by two way(from tail or from head) based on the inserted index
After finding the previous node, the next step is quite simple It just needs to adjust the pointer of the previous node and the next node of the previous node to the new node In the code
Figure 18: Searching the previous node
Trang 18implementation screenshot, this step is written quite clearly; however, here is the visual illustration
of this process:
Continues with the illustration above, the index to insert is 3 and the data now is 43:
Apply newNode.setNext(prev.getNext()) and newNode.setPrev(prevNode):
Adjust the pointer of these beside nodes:
When all the pointer next and prev of each node are pointing to the neccesary node as the requirement of this operation The operation is terminated with the node has new node inserted
3 Sorting in Linked-List
3.1 Describe your selected sorting algorithm
To apply a sorting algorithm to sort the linked list, here are some suitable sorting algorithms such as insertion sort and merge sort However, insertion sort is considered and chosen to be the sorting algorithm for my linked-lists sorting operation because of its easy implementation of this one
Figure 19: adjust pointer of new node
Figure 20: Complete Inserting process
Trang 19Insertion sort is viewed as a type of in-place comparison-based sorting algorithm It uses the loop to pass via elements of the linked list Each iteration of this method removes an element from the input data and puts it into the right location in the sorted linked list (Palaniappan, 2021) The element is deleted from the input and is chosen at random, and the procedure is continued until all input items have been processed
3.2 Implementation
3.3 Explanation Of The Implementation
Besides explaining the particular code implementation, I would briefly describe the mechanism of insertion sort and provide an illustrated visual for the sorting process Generally, the basic working
Figure 21: Sorting algorithm Implement