Docsity

Assignment 2 1649 (merit), Assignments of Information Systems

Toggle sidebar
bg1
ASSIGNMENT 2 FRONT SHEET
Qualification
BTEC Level 5 HND Diploma in Computing
Unit number and title
Unit 19: Data Structures and Algorithms
Submission date
03-11-2023
Date Received 1st
submission
03-11-2023
Re-submission Date
Date Received 2nd
submission
Student Name
Nguyį»…n Văn Quang
Student ID
Gch211372
Class
Gch1107
Assessor name
Đỗ Hồng QuĆ¢n
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
Quang
Grading grid
P4
P5
P6
P7
M4
M5
D3
D4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f

Partial preview of the text

Download Assignment 2 1649 (merit) and more Assignments Information Systems in PDF only on Docsity!

ASSIGNMENT 2 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 19: Data Structures and Algorithms Submission date 03 - 11 - 2023 Date Received 1st submission 03 - 11 - 2023 Re-submission Date Date Received 2nd submission Student Name Nguyį»…n Văn Quang Student ID Gch Class Gch1107 Assessor name Đỗ Hồng QuĆ¢n 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 Quang Grading grid P 4 P 5 P 6 P 7 M 4 M 5 D3 D

 Summative Feedback:  Resubmission Feedback: Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:

  • Design and implementation of Stack ADT and Doubly Linked List ADT, Undo-Redo feature (P4 + M4)
    • I. Stack ADT
      • 1.1 Introduction.....................................................................................................................................................
      • 1.2 Implement size, isEmpty method....................................................................................................................
    • III. Implementation
      • 3.2 Implement pop method
      • 3.3 Implement peek() method
      • 3.4 Implement search() method
    • II. Doubly linkedList
      • 2.1 Introduction...................................................................................................................................................
      • 2.3 implement addLastNode()
      • 2.4 implement reMoveFirst() method
      • 2.5 implement reMoveLast() method
      • 2.5 implement addByIndex() method
      • 2.6 Implement removeIndex() method
      • 2.7 Implement search() of last doubly linkedList
  • Implement error handling and report test results (P5)
    • I. Error handling
  • Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm (P6)
  • an example (P7) Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with
  • Figure 1: stack (GeeksforGeeks, 2023).
  • Figure 2:Sourse code of class Node
  • Figure 3: Source code of variables and constructor of MyStack
  • Figure 4: Source code of size() and isEmpty()
  • Figure 5:Implement Push method
  • Figure 6:image of push method
  • Figure 7:Image of push method
  • Figure 8:image of push method
  • Figure 9: Implement pop method................................................................................................................................
  • Figure 10:Image pop method
  • Figure 11: Image pop method
  • Figure 12:Figure 11: Image pop method
  • Figure 13:Implement peek method
  • Figure 14:Image of peek method
  • Figure 15: Implement of search method
  • Figure 16:Image of search method
  • Figure 17:Source code of class Node..........................................................................................................................
  • Figure 18:Source code of constructor
  • Figure 19:Source code of size()
  • Figure 20:Source code of addFirstNode()...................................................................................................................
  • Figure 21:Image of addFirst()
  • Figure 22:Image of addFirst()
  • Figure 23:Source code of addLastNode()
  • Figure 24: Image of addLast() Node
  • Figure 25:Image of addLast() Node 2.........................................................................................................................
  • Figure 26:Source code of removeFirst()
  • Figure 27:Image of removeFirst() 1............................................................................................................................
  • Figure 28: Image of removeFirst() 2...........................................................................................................................
  • Figure 29:Image of removeFirst() 3............................................................................................................................
  • Figure 30:Image of removeFirst() 3............................................................................................................................
  • Figure 31:Source code of removeLast
  • Figure 32:Image of removeLast()
  • Figure 33:Image of removeLast()
  • Figure 34:Image of removeLast()
  • Figure 35:Source code of addByIndex()
  • Figure 36: Image of addByIndex()
  • Figure 37:Image of addByIndex()
  • Figure 38:Image of addByIndex()
  • Figure 39:Source code of removeIndex()
  • Figure 40:Image of removeIndex()
  • Figure 41:Source code search() of last doubly linkedList
  • Figure 42:Image of search()
  • Figure 43:Source code of Constructor
  • Figure 44: Source code of writeWord()
  • Figure 45:Image of writeWord() and how it work......................................................................................................

Figure 46:The result when call writeWord method .................................................................................................... 33 Figure 47:Souce code of undo().................................................................................................................................. 34 Figure 48: image of undo() and how it work .............................................................................................................. 34 Figure 49:The result of undo() .................................................................................................................................... 35 Figure 50:Source codo of redo() stack ........................................................................................................................ 35 Figure 51:Image of redo() stack and how it work....................................................................................................... 36 Figure 52:The result of redo() stack............................................................................................................................ 36 Figure 53:Image of undo and redo , how it work........................................................................................................ 37 Figure 54:new current ................................................................................................................................................. 38 Figure 55:Source code of display() ............................................................................................................................. 38 Figure 56:Source code of undo() ................................................................................................................................ 38 Figure 57:Image of undo() doubly .............................................................................................................................. 39 Figure 58:The result of undo() douly .......................................................................................................................... 39 Figure 59:Source code of redo() doubly ..................................................................................................................... 40 Figure 60:Image of redo() doubly ............................................................................................................................... 40 Figure 61: The result of redo() .................................................................................................................................... 40 Figure 62:Image of undo and redo base douly linkedList .......................................................................................... 41 Figure 63:Error handling with if else .......................................................................................................................... 42 Figure 64: Error handling with throws........................................................................................................................ 43 Figure 65:Big O Complexity Chart (Prado, 2020) ................................................................................................. 50

Design and implementation of Stack ADT and Doubly Linked List ADT,

Undo-Redo feature (P4 + M4)

I. Stack ADT 1.1 Introduction A linear data structure called a stack arranges its actions in a specific order. First In Last Out (FILO) or Last In First Out (LIFO) may be the order. It is implied by LIFO that the element placed last will come out first and by FILO that the element inserted first will come out last (GeeksforGeeks, 2023).

Figure 3 : Source code of variables and constructor of MyStack Explanation of figure 3: "Node top" This is a field of the "MyStack" class, and it represents the top of the stack. This field is a reference to an object of the "Node" class, which has been explained earlier. The variable top points to the topmost node of the stack, where the newest element is inserted and where removal operations are performed. "int count" This is a field used to keep track of the number of elements in the stack. Initially, when a new "MyStack" object is created by calling new MyStack(), this field is set to 0. "public MyStack()" This is the constructor method of the "MyStack" class. When a new "MyStack" object is created by calling new MyStack(), this method is called. This method initializes the top field to null (because the stack is initially empty), and the count field to 0 (because there are no elements initially in the stack). Operation of stack Method Description Parameter Return value push Insert element on top of tack Int data no pop Remove element on top of stack no That element isEmpty Check empty no bool peek Get data from top but not change no That elemetn size Get total accout no count Search Seach index of last element Int data intdex

1.2 Implement size, isEmpty method Figure 4 : Source code of size() and isEmpty() Explanation of figure 4: public int size(): This method returns the size of the stack, meaning the number of elements currently in the stack. It uses the count variable to retrieve the value that keeps track of the number of elements and returns that value. When the stack is created (an instance of MyStack is initialized), the initial value of count is 0. After adding and removing elements, count is updated to reflect the correct number of elements in the stack. Public boolean isEmpty(): This method checks whether the stack is empty or not. If count (the number of elements) is equal to 0, it means the stack doesn't contain any elements, and the method returns true to indicate that the stack is empty. Conversely, if count is not equal to 0, it means the stack contains at least one element, and the method returns false to indicate that the stack is not empty. III. Implementation 3.1 Implement Push method Figure 5 :Implement Push method push() adds a new item to the stack's top. It requires the item but returns nothing.

Figure 8 :image of push method 3 Explanation of Figure 8 : Continuing the process, we now have a list of 3 elements, and it can be seen that the "push" operation has a time complexity of O(1). In a stack, the last-in-first-out (LIFO) principle is applied. 3.2 Implement pop method Figure 9 : Implement pop method Explanation of Figure 9 : pop() removes the item at the top of the stack. It does not require any arguments and just returns the item. The stack has been altered. If the stack is empty, return null to indicate that there are no elements to remove. Otherwise, retrieve the value of the node at the top of the stack (current), update top to point to the next node, and remove the link of current with the stack. Return the value of the removed current node.

Figure 10 :Image pop method 1 Explanation of Figure 10 : At the moment, the list contains 3 elements, so it will satisfy the 'else' condition and create a 'current' variable that is set equal to 'top' (line 62). It then moves 'top' down by one unit (line

  1. and sets 'current.next' to null (line 64). Finally, it returns the 5 value that has been removed (line 65). After that, the size will be reduced to 2. Figure 11 : Image pop method 2 Explanation of Figure 11 : The list currently has three members, thus it will create a 'current' variable with the value 'top' (line 62) and satisfy the 'otherwise' condition. Line 63 then shifts 'top' down by one unit, and line 64 sets 'current.next' to null. Ultimately, the value 20 that was eliminated is returned (line 65). After that, the size will be reduced to 1.

3.4 Implement search() method Figure 15 : Implement of search method Explanation of Figure 15 : Initially, lastIndex and currentIndex are initialized with values - 1 and 0, respectively. In the loop, we iterate through each element in the stack. If the current element has a value equal to data, lastIndex is updated with the index of that element. After each iteration, currentIndex increases by 1, and the code moves on to the next element. In the end, after traversing the entire stack, the method returns the value of lastIndex, which is the index of the last element with a value equal to data. If not found, lastIndex retains its initial value of - 1 to indicate that no matching element was found.

Figure 16 :Image of search method Explanation of Figure 16 : First, you call the search function with an argument of data equal to 7. Then, you create a current node and set it to top (line 70). After declaring and initializing lastIndex and currentIndex (lines 71, 72), because current is initially set to top, you will traverse the entire list and compare it with the argument passed. If current != null (line 73), you start the while loop at the beginning by checking if current.data is not equal to arg (line 74). Then, you move current to the next node (line 77) and increase currentIndex by one. Continuing this process, with currentIndex equal to 1, you have found a match with current.data = arg, and lastIndex is set to 1. You continue by moving current to the next node and incrementing currentIndex by one. You repeat the loop, and this time, current.data still matches, so lastIndex becomes 2. I continue to move current to the next node and increase currentIndex by one, repeatedly checking, and when current becomes null, you return 2. This indicates that the last element with the value equal to arg was found at index 2 in the list. II. Doubly linkedList 2.1 Introduction Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list (tutorialspoint, 2023). Operation of doubly linked List Method Description Parameter Return value addFirst Insert element on head of doubly Int data no addlast Insert element on tail of doubly Int data no removeFirst Remove element on tail of doubly no That element removeLast Remove element on tail of doubly no That element addIndex Insert element by index Int data, index no removeIndex Remove element by index Int index That element search Searh last index of element Int data That index I will implement the doubly linkedList:

Figure 18 :Source code of constructor Explanation of Figure 18 : Node head: This is a reference to the first node in the doubly linked list. Node tail: This is a reference to the last node in the doubly linked list. int count: It keeps track of the number of nodes in the doubly linked list. The constructor MyDoublyLinkedList() initializes the linked list by setting both the head and tail to null, indicating that the list is initially empty. The count is set to 0 to indicate there are no nodes in the list when it is first created. Figure 19 :Source code of size() Explanation of Figure 18 : return total elements in the list. 2.2 implement addFirstNode() Figure 20 :Source code of addFirstNode() Explanation of Figure 20 : Node newNode = new Node(data);: Creates a new node with the given data. if (head == null) { tail = newNode; }: Checks if the head is null, indicating that the list is empty. If it's empty, the tail is set to the new node, as it's the first and only node in the list. else { newNode.next = head; head.previous = newNode; }: If the list is not empty, it updates the links to make the new node the new head. It sets the next of the new node to the current head, and the previous of the current head to the new node. head = newNode;: Updates the head to be the new node, effectively making it the first node in the list.

count++;: Increases the count to maintain the count of nodes in the doubly linked list. Figure 21 :Image of addFirst() 1 Explanation of Figure 21 : Step 1: First, I set arg to 10 and create a "newNode" with a data value of 10 (line 42). Step 2: Currently, the list is empty, so it satisfies the condition head == null, and the tail will shift to the "newNode" (lines 43, 44). Step 3: Skipping the else condition and shifting the position of head to "newNode" and incrementing the count by 1, we end up with the arrangement as shown above (lines 49, 50). Figure 22 :Image of addFirst() 2 Explanation of Figure 22 : Step 1: I set arg to 20 and generate a "newNode" with a data value of 20 (line 42). Step 2: Currently, the list has one element, so it satisfies the else condition. It updates "newNode.next" to point to the head, and "head.previous" is set to point to "newNode" (lines 46, 47). Step 3: Then, it moves the position of head to be "newNode" and increments the count by 1, resulting in a size of 2 (lines 49, 50).

Step 3: Then, skipping the else condition, the tail is moved to "newNode," and the count is incremented by 1 (lines 61, 62). Figure 25 :Image of addLast() Node 2 Explanation of Figure 25: Currently, the list has one element with data 2. Step 1: I continue by setting the argument to 3 and create a "newNode" with a data value of 3 (line 54). Step 2: Since the list has one element, it satisfies the else condition. "tail.next" is updated to point to "newNode," and "newNode.previous" is set to point to "tail" (lines 58, 59). Step 3: Finally, "tail.next" is moved to "newNode," and the count is increased by one, resulting in a size of 2 (lines 61, 62). Continuing, I set the argument to 9, and there is an illustrated list as shown above. 2.4 implement reMoveFirst() method Figure 26 :Source code of removeFirst()

Explanation of Figure 25: The removeFirst method removes and returns the value of the first node in a doubly linked list in steps: Check if the list is empty (if head == null), and if it is, return null. Store the node to be removed in the removedNode variable. If there is only one node in the list (if head == tail), set both head and tail to null, indicating an empty list. If there are multiple nodes in the list, update head to point to the next node, and set head.previous to null. The removed node (removedNode) is no longer linked to the list. Decrease count by 1 to maintain the number of nodes in the list and return the value of removedNode.data. Figure 27 :Image of removeFirst() 1 Explanation of Figure 27: Currently, my list has 2 elements. Step 1: Calling removeFirst() creates a removedNode assigned to the head (line 69) Step 2: Since there are two elements, it satisfies the else condition. It moves the head forward by one step, continues with head.previous pointing to null, and removedNode.next pointing to null (line 71, 72) Step 3: Reducing count by one, the size becomes 1, and it returns removedNode.data, which has a value of 2 (78, 79) Figure 28 : Image of removeFirst() 2