1649-Data structure and algorithms-Assignment 1, Assignments of Information Technology

1649-Data structure and algorithms-Assignment 1

Typology: Assignments

2022/2023

Uploaded on 07/31/2023

truong-duy-minh-fgw-dn
truong-duy-minh-fgw-dn 🇻🇳

5

(7)

12 documents

1 / 24

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
Date Received 1st
submission
Re-submission Date Date Received 2nd
submission
Student Name Truong Duy Minh Student ID GCD210505
Class GCD1102 Assessor name
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
P1 P2 P3 M1 M2 M3 D1 D2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download 1649-Data structure and algorithms-Assignment 1 and more Assignments Information Technology 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 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Truong Duy Minh Student ID GCD Class GCD1102 Assessor name 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 P1 P2 P3 M1 M2 M3 D1 D

 Summative Feedback:  Resubmission Feedback:

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

I. DATA STRUCTURE

1. Abstract data type

1.1. Definition

Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of values and a set of operations. The definition of ADT only mentions what operations are to be performed but not how these operations will be implemented. It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations. It is called “abstract” because it gives an implementation-independent view. (htt89) Figure 1 : ADT The user of data type does not need to know how that data type is implemented, for example, we have been using Primitive values like int, float, char data types only with the knowledge that these data type can operate and be performed on without any idea of how they are implemented. So user only needs to know what a data type can do, but not how it will be implemented. There can be different ways to implement an ADT, for example:  Array: An array is a collection of elements of the same type, where each element can be accessed using its index. Arrays are widely used to store and retrieve data efficiently.  Stack: A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. Elements can only be inserted or removed from the top of the stack. It is commonly used for managing function calls, recursive algorithms, and undo/redo operations.

Queue: A queue is a linear data structure that follows the First In, First Out (FIFO) principle. Elements are inserted from the rear and removed from the front. Queues are often used to manage tasks, invoke services, and handle requests.  Linked List: A linked list is a dynamic data structure where each element, known as a node, contains a value and a reference to the next node in the list. Linked lists can be singly-linked (with a reference to the next node only) or doubly-linked (with references to both the next and previous nodes). Linked lists are efficient for insertion and deletion but have slower access times compared to arrays.  Tree: A tree is a hierarchical data structure consisting of nodes connected by edges. It has a root node at the top and child nodes branching downwards. Trees are commonly used for organizing hierarchical data, like file systems, organization structures, and decision-making algorithms.  Graph: A graph is a collection of nodes (also known as vertices) connected by edges. Graphs are used to represent relationships between different elements, such as social networks, web page links, and routing algorithms. These are some of the most commonly used ADTs, each with its own unique characteristics and applications. The choice of ADT depends on the specific requirements and constraints of the problem at hand. 1.2. Examples In this report, I will use Array as an example of general ADT: An Array Abstract Data Type (ADT) is a data structure that stores a fixed-size sequence of elements of the same type. It provides a way to organize and access data in a compact and contiguous manner. (htt90) The Array ADT works by allocating a block of contiguous memory and associating it with an index-based system. Each element in the array is assigned a unique index starting from 0 (for the first element) up to the size of the array minus 1 (for the last element). This index is used to access and manipulate individual elements in the array. Figure 2 : Array The above image can be looked as a top-level view of a staircase where you are at the base of the staircase. Each element can be uniquely identified by their index in the array

Insert element Suppose we want to insert the element 45 at index 4 in the array. To make space for the new element, we shift all elements to the right of the insertion point by one position.  Starting from the rightmost element (here, 50), we move it to the right by one position. We now have an empty space at index 4.  After that, we can insert the new element 45 at the desired index

Delete element Suppose we want to delete the element at index 3 (40) in the array. To remove the element, we shift all elements to the left of the deletion point by one position.  Starting from the deletion point (index 3), we move the element at index 4 (50) to the left by one position, overwriting the element at index 3.  Finally, we set the value at the last position (index 4) to 0 or the default value to close the gap. Sort Suppose we want to sort the elements in ascending order using an Array ADT. We'll use the built-in Arrays.sort() method for simplicity. The sort operation compares adjacent elements and swaps them if they are in the wrong

Figure 3 : Codes for the operation of Array

Figure 4 : Sort operation Figure 5 : Search operation Figure 6 : Update operation Figure 7 : Delete operation This code implements an Array ADT (Abstract Data Type) in Java. The ArrayADTExample class represents a resizable array with the following operations:  getSize(): Returns the current size (number of elements) of the array.  getElement(index): Returns the element at the specified index in the array.  setElement(index, value): Sets the element at the specified index in the array to the given value.  insertElement(index, value): Inserts an element at the specified index in the array. If the array is full, it automatically resizes to accommodate more elements.  deleteElement(index): Deletes the element at the specified index in the array and shifts the remaining elements to close the gap.  sortArray(): Sorts the elements in the array in ascending order using the built-in Arrays.sort() method.  searchElement(value): Searches for the specified value in the array and returns the index at which it is found. If the value is not found, it returns -1. The main method demonstrates the usage of these operations by creating an instance of the ArrayADTExample class, inserting elements (10, 30, 20, and 50), displaying the array before and after sorting, searching for an element (30), updating an element (at index 0), and deleting an element (at index 2). Overall, this code provides a basic implementation of an Array ADT, allowing you to perform common operations on an array such as search, update, insert, delete, and sort.

2. ADT usages 2.1. Application of Stack in memory

Overall, the stack data structure plays a crucial role in memory management for method calls, enabling efficient and organized execution of functions within a program. 2.2. Application of an ADT Scenario: Imagine you are the manager of a prestigious library and you need to develop a program to efficiently manage the library's vast collection of books. As the collection continues to grow, you realize the need for a sophisticated system that allows you to add, remove, and search for books seamlessly. To address this challenge, you decide to develop a Java program that utilizes a linked list data structure to implement the necessary functionalities. The program will offer the following operations:  Adding a Book: With a simple command, you can add a new book to the library's collection. The program will prompt you to provide the title and author of the book, ensuring complete and accurate record-keeping.  Removing a Book: Removing a book is just as effortless. By specifying the title and author of the book, you can swiftly remove it from the library's collection, keeping the catalog up-to-date.  Searching for a Book: The program provides a powerful search functionality that allows you to find books based on keywords. Whether you remember the title or are familiar with the author's name, the search feature will locate the desired book in no time. The program displays the relevant details of any matching books, ensuring quick and efficient access to the library's vast collection. In addition to these primary functionalities, the program also supports other standard linked list operations, such as inserting a book at a specific position, determining the library's total book count, retrieving a book by its index, and more. This comprehensive suite of features ensures that you can manage the library's collection effectively and seamlessly. By leveraging the flexibility and efficiency offered by the linked list data structure, your program empowers you to efficiently handle the library's collection of books, allowing you to focus on providing exceptional service and ensuring a remarkable experience for all library patrons. Illustrate the linked list data structure used for this program: Operations Illustrate Adding a Book  Initially, the LinkedList is empty.  When the first book is added, a new box is created, and the book object is placed inside this box.  The box is linked to the LinkedList by setting the head and tail references to point to this box. Since it's the only book in the library, the head and tail both point to the same box.

 If more books are added, a new box is created for each book, and it contains both the book object and a reference to the next box.  The new box is linked to the existing boxes in the LinkedList. The previous tail box's reference to the next box is updated to point to the new box, and the tail reference is updated to point to the new box.  This process continues as more books are added, with each new box being linked to the previous box, and the tail reference being updated to point to the new box. Removing a Book  Identify the node (box) in the LinkedList that needs to be deleted. This can be done by searching for the desired element, such as the book title or a specific identifier.  Once the node to be deleted is identified (let's call it nodeToDelete), locate its previous node (let's call it previousNode). This is necessary to update the references and ensure the LinkedList remains linked correctly after deletion.  Update the references:  If previousNode is null, it means nodeToDelete is the head of the LinkedList. In this case, update the head reference to the next node (nodeToDelete.next).  If previousNode is not null, set previousNode.next to nodeToDelete.next. This skips over nodeToDelete in the LinkedList and links previousNode directly to