Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

1649_Data Structure & Algorithms_Assignment 1, Assignments of Data Structures and Algorithms

This unit introduces students to data structures and how they are used in algorithms, enabling them to design and implement data structures. The unit introduces the specification of abstract data types and explores their use in concrete data structures. Based on this knowledge, students should be able to develop solutions by specifying, designing and implementing data structures and algorithms in a variety of programming paradigms for an identified need.

Typology: Assignments

2021/2022

Available from 06/01/2023

TuanAnhMai
TuanAnhMai 🇬🇧

5

(3)

24 documents

1 / 22

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 Mai Tran Tuan Anh Student ID GCD201452
Class GCD0904 Assessor name Pham Thanh Son
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
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download 1649_Data Structure & Algorithms_Assignment 1 and more Assignments Data Structures and Algorithms 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 Mai Tran Tuan Anh Student ID GCD Class GCD0904 Assessor name Pham Thanh Son 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:

Figure 1: ADT model.................................................................................................................................................... 5 Figure 2: How queue works......................................................................................................................................... 6 Figure 3: Queue properties.......................................................................................................................................... 8 Figure 4: isFull function............................................................................................................................................... 8 Figure 5: isEmpty function........................................................................................................................................... 8 Figure 6: How enqueue works....................................................................................... Error! Bookmark not defined. Figure 7: Enqueue code................................................................................................. Error! Bookmark not defined. Figure 8: How dequeue work......................................................................................... Error! Bookmark not defined. Figure 9: Dequeue code................................................................................................. Error! Bookmark not defined. Figure 10: Front and rear pointer.................................................................................. Error! Bookmark not defined. Figure 11: Test queue.................................................................................................... Error! Bookmark not defined. Figure 12: Result............................................................................................................ Error! Bookmark not defined. Figure 13: How Stack work........................................................................................................................................ 13 Figure 14: Stack properties............................................................................................ Error! Bookmark not defined. Figure 15: Function check index and size....................................................................... Error! Bookmark not defined. Figure 16: Function push............................................................................................... Error! Bookmark not defined. Figure 17: Function pop................................................................................................. Error! Bookmark not defined. Figure 18: Function peek............................................................................................... Error! Bookmark not defined. Figure 19: Test Stack...................................................................................................... Error! Bookmark not defined. Figure 20: Result............................................................................................................ Error! Bookmark not defined. Figure 21: Define properties.......................................................................................... Error! Bookmark not defined. Figure 22: Function isEmpty.......................................................................................... Error! Bookmark not defined. Figure 23: Function offer............................................................................................... Error! Bookmark not defined. Figure 24: Function poll................................................................................................. Error! Bookmark not defined. Figure 25: Function peek............................................................................................... Error! Bookmark not defined. Figure 26: Test Queue.................................................................................................... Error! Bookmark not defined. Figure 27: Result of testing............................................................................................ Error! Bookmark not defined.

I. ABSTRACT DATA TYPES

1. Definition

  • An object's behavior can be described by a set of values and a set of actions, and this behavior is known as an abstract data type (ADT). The definition of ADT merely specifies the actions that must be taken, not how they must be carried out. It is unclear what algorithms will be utilized to carry out the operations and how the data will be structured in memory. Because it provides an implementation-independent view, it is called abstract
  • In other words, abstract data types are the definitions of data and operations that lack implementation- specific information. In this instance, we are aware of the information we are storing and the operations that can be applied to it, but we are unaware of the implementation specifics. Because each programming language has a unique implementation technique, there are no implementation details available.
  • Inbuilt data types include those like int, float, double, long, etc. with which we may do operations like addition, subtraction, division, multiplication, etc. There may now come a time when we need to define operations for our user-defined data type. Only when we need them may we define these operations. Therefore, we can design data structures together with their operations to simplify the process of addressing problems, and such data structures that are not built-in are known as ADT. Figure 1 : ADT model
  • The ADT model is depicted in the above figure. The ADT model has two different sorts of models: public function and private function. The data structures that we use in a program are also included in the ADT model. This paradigm first does encapsulation, or the wrapping of all the data into a single unit, or ADT. Then, the abstraction is carried out, i.e., it is shown what data structures are being used in a program

2.2 Operations

  • Array list operations:  Traverse(): Print each array list element  Insertion(): Inserts a new element  Deletion(): Deletes the element  Search(): Use the provided index or value to search for an element.  Update(): Updates the element at the specified index. 2.3 Applications of array list
  • A linear data structure called an array list is a grouping of related data types. Array lists are kept in adjacent memory regions. Some application of array list can be listed are  Stacks, queues, and other data structures are implemented using array lists.  Matrix computations and other mathematical implementations employ array lists.  Computer lookup tables employ array lists.  The CPU can be scheduled using array lists. 2.4 Types of indexing in an array list  0 (zero-based indexing): The first member of the array list is indexed by the subscript 0  1 (one-based indexing): The first element of the array list is indexed by the subscript of 1.  n (N-based indexing): An array list's base index can be chosen at will. Negative index values are often permitted in programming languages that support n-based indexing, and other scalar data types, such as enumerations or characters, may also be used as array list indexes. 2.5 Code snippet:
  • At first, I need to declare all necessary properties of an array list

Figure 3 : Queue properties

  • Function grow and shrink is used to open up the space of the array list or reduce it Figure 4 : Grow function Figure 5 : Shrink function
  • Function check index to check before adding new elements into the array list. When the user inserts new index, the system will check it with the array list. If the index is outside the length of the array list, the system will throw an exception

Figure 8 : Add function

- The remove function has the similar flow to the add. It will check the index, then it will get the element out of the array list. New index will be assigned for all elements and then the function is ended Figure 9 : Function remove

  • In order to check the index of the elements, I use the function indexOf. Using a for loop, the system will check the elements inserted with all elements inside to array lists. If it is matched, the system will return the index, otherwise it will return false. Figure 10 : Function indexOf
  • After creating the array list, I will test it. Below is the code I use to test the array list Figure 11 : Test array list
  • This is the output

Figure 13 : How Stack work

  1. Operations
  • Certain actions are made available to us so that we can manipulate a stack. The operation is known as a push operation when we want to add an element to the stack and as a pop operation when we want to remove an element from the stack. Underflow occurs when we attempt to pop an element from an empty stack, and overflow occurs when we attempt to push an element into an already-full stack. To use a stack efficiently, we need to check the status of stack as well. These functions below are the main functions of a stack:  push() − Pushing (storing) an element on the stack.  pop() − Removing (accessing) an element from the stack.  peek() − get the top data element of the stack, without removing it.  isFull() − check if stack is full.  isEmpty() − check if stack is empty.
  • For stack pointer, this pointer will always remain at the top of the stack. This pointer will not be removed, it will only be removed from this element to another element
  1. Features of stack memory  The data members are allocated in temporary memory, and they can only be accessed while the method() that included them is active.  Once the associated method has finished running, it automatically allocates or releases memory.

 Experience the corresponding Java. lang. error. JVM's StackOverFlowError if all available memory on the stack is used.  Because the data saved can only be accessed by the owner thread, stack memory allocation is regarded as being safer than heap memory allocation.  Heap-memory allocation is slower than memory allocation and de-allocation.  Stack memory is less capacious than heap memory in terms of storage.

  1. Stack Memory Architecture Figure 14 : Stack Memory Architecture
  • The "stack pointer" is the register "ESP," which is used to point to the subsequent item on the stack. EBP, often known as the "frame pointer," acts as a constant reference point for information on the stack. As a result, the program is able to determine how far away each item in the stack is from this location.

Figure 15 : How function is called in stack

  • Memory I/O operations are followed by a LIFO sequence, as was previously shown. The data added to or stored after any other element on that stack must be cleared or released first on that stack, according to the LIFO approach. The initial variable or object reference of each new method is stored in a new data block that is placed to the top of the stack when the method is called. The stack frame containing a method's data is released when the method has finished running.
    1. Example of Stack Memory in Java
  • Below is an example of stack memory in Java. This function including the print, printChar and the main method.

Figure 16 : Code snippet

  • In the main() method of the code below, we are producing primitive integer and char data. The top of the stack will be moved up to include this main() method. The print() method is then invoked, adding itself once more to the top of the stack. A block is created on the stack for the printChar() method, which is called inside the print() method. All of these method blocks in the stack will have access to the necessary data.
  • The final stack memory is shown in the image below.

III. APPLICATION OF AN ADT

  • There are many different types of ADT and each of them has their own usage in specific scenario. In this part I will focus on ADT usage, mostly double linked list. Doubly linked list is a category of linked list, along with singly linked list and circular linked list.
    1. Linked List
  • A series of data structures that are linked together make up a linked list. A linked list is a group of links that includes stuff. There is a relationship between each link and another link. The second most used data structure is the linked list, behind the array. The key words for understanding the concept of a linked list are listed below.  Link − Each link of a linked list can store a data called an element.  Next − Each link of a linked list contains a link to the next link called Next.  LinkedList − A Linked List contains the connection link to the first link called First Figure 18 : Linked List
    1. Double Linked List
  • A variant of a linked list called a doubly linked list makes it easier to navigate both ahead and backward than a single linked list does. The key terms for comprehending the idea of a doubly linked list are listed below.  Link − Each link of a linked list can store a data called an element.  Next − Each link of a linked list contains a link to the next link called Next.  Prev − Each link of a linked list contains a link to the previous link called Prev.  LinkedList − A Linked List contains the connection link to the first link called First and to the last link called Last.

Figure 19 : Double Linked List

  • I will use doubly linked list to demonstrate the movement in website, including visit the website, going backward and forward First, I will declare class Browser. This class will have object Node, which will contain the information of the website URL Figure 20 : Class Node