Assignment 1 - Data Structure & Algorithms - PASS, Assignments of Computer Science

Assignment 1 - Data Structure & Algorithms - PASS

Typology: Assignments

2021/2022

Uploaded on 12/22/2022

minh-huy-huynh
minh-huy-huynh 🇻🇳

4.7

(58)

39 documents

1 / 59

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT 1 FRONT SHEET
Qualification
BTEC Level 5 HND Diploma in Computing
Unit number and title
Unit 19: Data Structures and Algorithms
Submission date
11/12/2022
Date Received 1st
submission
Re-submission Date
22/12/2022
Date Received 2nd
submission
Student Name
Huynh Minh Huy
Student ID
GCD210173
Class
GCD1001
Assessor name
Ho Van Phi
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
Huy
Grading grid
P1
P2
P3
M1
M2
M3
D1
D2
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

Partial preview of the text

Download Assignment 1 - Data Structure & Algorithms - PASS and more Assignments Computer Science in PDF only on Docsity!

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 19 : Data Structures and Algorithms Submission date 11/12/2022 Date Received 1st submission Re-submission Date 22 /12/2022 Date Received 2nd submission Student Name Huynh Minh Huy Student ID GCD Class GCD1001 Assessor name Ho Van Phi 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 Huy

Grading grid P1 P2 P3 M1 M2 M3 D1 D

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:

Introduction If data is kept in a well-organized manner on storage media and in computer memory, it may be accessible quickly for processing, reducing latency and providing a prompt response to the user. A data structure is a strategy for organizing data, or in other terms, a data structure is an arrangement of data in a computer's memory that allows the data to be immediately available to the processor for essential computations. A data structure should be viewed as a logical idea that must meet two key issues. First, how will the data be saved, and then, what operations will be done on it? Because a data structure is a system for organizing data, its functional specification should be independent of its implementation. ADT (Abstract Data Type) is the functional specification of a data structure that is independent of implementation. The implementation is left to developers, who pick which technology is best suited to their project's requirements.

Chapter 1: Create a design specification for data structures explaining the valid operations that can be carried out on the structures. (P1) I. Abstract Data Type.

  1. Definition. Abstract Data Type (ADT) is a kind of (or class of) object whose behavior is defined by a collection of values and actions. The definition of ADT just specifies which operations must be completed, not how they will be carried out. It makes no mention of how data will be stored in memory or which algorithms will be used to carry out the actions. The name "abstract" refers to the fact that it provides a view that is independent of implementation. Figure 1 : Abstract Data Type.
  2. Examples. A Doubly Linked List, as opposed to a Single Linked List, is a form of a Linked List in which navigation is accessible in both ways, forward and backward. The following are key words to grasp the notion of a doubly linked list.  Link – A linked list's links can each hold data known as elements.  Next – Each link in a linked list has a Next link to the next link.  Prev − Each link in a linked list has a Prev link to the previous link.  LinkedList − A Linked List has a connection link to the first link named First and a connection link to the last link called Last.

Insertion on a Doubly Linked List. Pushing a node to a doubly-linked list is identical to pushing a node to a linked list, but more work is necessary to handle the prior node's reference. We can introduce entries into a doubly-linked list at three distinct points:

  • Insertion at the beginning.
  • Insertion in-between nodes.
  • Insertion at the End. Assume we have a two-linked list with the items 1, 2, and 3. 1. Insertion at the Beginning. Let's add a node with value 6 at the beginning of the doubly linked list we made above. a) Create a new node.  allocate memory for newNode  assign the data to newNode. Figure 3 : New node. b) Set prev and next pointers of new node.  point next of newNode to the first node of the doubly linked list  point prev to null Figure 4 : Reorganize the pointers (changes are denoted by purple arrows).

c) Make new node as head node.  Point prev of the first node to newNode (now the previous head is the second node)  Point head to newNode Figure 5 : Reorganize the pointers. Code for Insertion at the Beginning in Java Program Language. // insert node at the front public void insertFront(int data ) { // allocate memory for newNode and assign data to newNode Node newNode = new Node( data ); // make newNode as a head newNode.next = head; // assign null to prev of newNode newNode.prev = null; // previous of head (now head is the second node) is newNode if (head != null) head.prev = newNode; // head points to newNode head = newNode; } Figure 6 : Insertion at the Beginning.

The final doubly linked list is after this insertion is: Figure 10 : Final list. Code for Insertion in between two Nodes. // insert a node after a specific node public void insertAfter(Node prev_node , int data ) { // check if previous node is null if ( prev_node == null) { System.out.println("previous node cannot be null"); return; } // allocate memory for newNode and assign data to newNode Node new_node = new Node( data ); // set next of newNode to next of prev node new_node.next = prev_node .next; // set next of prev node to newNode prev_node .next = new_node; // set prev of newNode to the previous node new_node.prev = prev_node ; // set prev of newNode's next to newNode if (new_node.next != null) new_node.next.prev = new_node; } Figure 11 : Insertion in between two Nodes.

3. Insertion at the End. Let's add a node with value 6 at the end of the doubly linked list. a) Create a new node. Figure 12 : New node. b) Set the next pointer of new node and previous node. If the linked list is empty, make the newNode as the head node. Otherwise, traverse to the end of the doubly linked list and Figure 13 : Reorganize the pointers. The final doubly linked list looks like this. Figure 14 : The final list.

Deletion from a Doubly Linked List. Similarly to inserting, we may remove a node from one of three points in a doubly linked list. Assume we have a two-linked list with the items 1, 2, and 3. Figure 16 : Original doubly linked list.

1. Deletion the First Node of Doubly Linked List. If the node to be deleted (i.e. del_node ) is at the beginning Reset value node after the del_node (i.e. node two). Figure 17 : Reorganize the pointers. Finally, free the memory of del_node. And, the linked will look like this Figure 18 : Final list. 2. Deletion of the Inner Node. If del_node is an inner node (second node), we must have to reset the value of next and prev of the nodes before and after the del_node. For the node before the del_node (i.e. first node)

Assign the value of next of del_node to the next of the first node. For the node after the del_node (i.e. third node) Assign the value of prev of del_node to the prev of the third node. Figure 19 : Reorganize the pointers. Finally, we will free the memory of del_node. And, the final doubly linked list looks like this. Figure 20 : Final list.

3. Delete the Last Node Of Doubly Linked List. In this case, we are deleting the last node with value 3 of the doubly linked list. Here, we can simply delete the del_node and make the next of node before del_node point to NULL. Figure 21 : Reorganize the pointers.

Full source code for Doubly Linked List in Java Program Language. public class DoublyLinkedList { // node creation Node head; class Node { int data; Node prev; Node next; Node(int d ) { data = d ; } } // insert node at the front public void insertFront(int data ) { // allocate memory for newNode and assign data to newNode Node newNode = new Node( data ); // make newNode as a head newNode.next = head; // assign null to prev of newNode newNode.prev = null; // previous of head (now head is the second node) is newNode if (head != null) head.prev = newNode; // head points to newNode head = newNode; } // insert a node after a specific node public void insertAfter(Node prev_node , int data ) { // check if previous node is null if ( prev_node == null) {

System.out.println("previous node cannot be null"); return; } // allocate memory for newNode and assign data to newNode Node new_node = new Node( data ); // set next of newNode to next of prev node new_node.next = prev_node .next; // set next of prev node to newNode prev_node .next = new_node; // set prev of newNode to the previous node new_node.prev = prev_node ; // set prev of newNode's next to newNode if (new_node.next != null) new_node.next.prev = new_node; } // insert a newNode at the end of the list void insertEnd(int data ) { // allocate memory for newNode and assign data to newNode Node new_node = new Node( data ); // store the head node temporarily (for later use) Node temp = head; // assign null to next of newNode new_node.next = null; // if the linked list is empty, make the newNode as head node if (head == null) { new_node.prev = null; head = new_node; return; } // if the linked list is not empty, traverse to the end of the linked list while (temp.next != null) temp = temp.next; // assign next of the last node (temp) to newNode