



















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Assignment 1 - Data Structure & Algorithms - PASS
Typology: Assignments
1 / 59
This page cannot be seen from the preview
Don't miss anything!




















































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.
Grading grid P1 P2 P3 M1 M2 M3 D1 D
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.
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:
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