Docsity

Data Structures & Algorithms: BTEC Level 5 HND Computing Guide, Assignments of Data Structures and Algorithms

Toggle sidebar
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
Lý Phước Sang
Student ID
GCS210360
Class
GCS1002
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
P2
P3
M1
M2
M3
D1
D2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Data Structures & Algorithms: BTEC Level 5 HND Computing Guide 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 Lý Phước Sang^ Student ID GCS Class GCS1002 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:

1. Data structures

1.1 Abstract data type (P1)

1.1.1 Definition Abstract data type (ADT) is a type (or class) for objects whose behavior is determined by a set of values and a set of operations. The definition of ADT only describes the actions that must be taken, not how they must be carried out. It is not specified what algorithms will be used to carry out the operations or how the data will be arranged in memory. Because it provides an implementation-independent perspective, it is called "abstract". (Chauhan, 2022) There are three type of ADTs. List ADT, Stack ADT, Queue ADT: List ADT The data is typically stored in key sequence in a list with a head structure that includes count, pointers, and the address of the compare function required to compare the data in the list. The data node contains a pointer to a data structure as well as a self-referential pointer to the next node in the list. (Chauhan, 2022) List ADT functions:  get – Return an element from the list at any given position.  insert – Insert an element at any position of the list.  remove – Remove the first occurrence of any element from a non-empty list.  removeAt – Remove the element at a specified location from a non-empty list.  replace – Replace an element at any position by another element.  size – Return the number of elements in the list.  isEmpty – Return true if the list is empty, otherwise return false.  isFull – Return true if the list is full, otherwise return false. Stack ADT Instead of storing data in each node, the pointer to data is stored in the Stack ADT implementation. The program allocates memory for the data and passes the address to the ADT stack. The ADT contains the head node and the data nodes. The calling function can only see the stack pointer. The stack head structure also includes a top pointer and a count of the number of entries currently in the stack. (Chauhan, 2022) Stack ADT functions:  push – Insert an element at one end of the stack called top.  pop – Remove and return the element at the top of the stack, if it is not empty.  peek – Return the element at the top of the stack without removing it, if the stack is not empty.

 size – Return the number of elements in the stack.  isEmpty – Return true if the stack is empty, otherwise return false.  isFull – Return true if the stack is full, otherwise return false. Queue ADT The queue abstract data type (ADT) is designed similarly to the stack abstract data type. Each node has a void pointer to the data and a link pointer to the next queue element. It is the program's responsibility to allocate memory for data storage. (Chauhan, 2022) Queue ADT functions:  enqueue – Insert an element at the end of the queue.  dequeue – Remove and return the first element of the queue, if the queue is not empty.  peek – Return the element of the queue without removing it, if the queue is not empty.  size – Return the number of elements in the queue.  isEmpty – Return true if the queue is empty, otherwise return false.  isFull – Return true if the queue is full, otherwise return false. There are three main features of ADT:  Abstraction: Only the fundamentals are given, so the user is not required to understand how the data structure is implemented.  Better Conceptualization: The real world is better understood for us thanks to ADT.  Robust: The program is capable of detecting errors and is robust. The three feature mentioned above does not specify how theses ADTs will be represented and how the operations will be carried out. An ADT can be implemented in a variety of ways, such as using arrays, singly linked lists, or doubly linked lists for the List ADT. Linked lists or arrays can be used to implement queue ADT and stack ADT, respectively. (Chauhan, 2022) 1.1.2 Examples Array is one of an examples of general ADT, an array is a collection of items of the same data type that are stored in adjacent memory locations. This makes calculating the position of each element easier by simply adding an offset to a base value, i.e. the memory location of the array's first element (generally denoted by the name of the array). The base value is index 0, and the offset is the difference between the two indexes. Here is the code for access an element in Array, to perform an operation on arrays, we needed to know how to access array elements. Here we want to access array element at index position 2, so we simply need to write: System.out.println(arr[2]); We will create an array with four number, and the result should be 3 because the base value is 0.

Figure 2 Stack illustration (Source: Introduction to Stack - Data Structure and Algorithm Tutorials - GeeksforGeeks) There are five basic function to manipulate Stack:  push() to insert an element into the stack  pop() to remove an element from the stack  peek() method returns an object from the top of the stack  search() To search an element in the stack  Empty() returns true if stack is empty else false. Push function: This function adds a new item to the stack. When the stack is full, this is referred to as an Overflow condition. Pseudo code for push: Figure 3 Push pseudo code (Source: Introduction to Stack - Data Structure and Algorithm Tutorials - GeeksforGeeks)

Code example for the push function: Figure 4 Push example code (Source: Java Stack Class (programiz.com)) Pop function: This method removes an item from the stack. The items are popped in the reversed order in which they are pushed. When the stack is empty, this is referred to as an Underflow condition. Figure 5 Pop pseudo code (Source: Introduction to Stack - Data Structure and Algorithm Tutorials - GeeksforGeeks)

Empty function: The empty function is used to determine whether or not a stack is empty. Code example for the empty function: Figure 8 Empty example code (Source: Java Stack Class (programiz.com)) Peek function: The peek method returns an object from the stack's top. Code example for the peek function: Figure 9 Peek example code (Source: Java Stack Class (programiz.com))

When the memory is organized, we called it stack allocation: The allocation occurs on contiguous memory blocks. Because the allocation occurs in the function call stack, we refer to it as a stack memory allocation. The compiler knows how much memory to allocate, and whenever a function is called, its variables are allocated memory on the stack. When the function call is finished, the memory for the variables is released. This is accomplished through the use of some predefined routines in the compiler. A programmer does not have to worry about stack variable memory allocation and de-allocation. This type of memory allocation is also known as temporary memory allocation because when the method completes its execution, all data belonging to that method is automatically flushed out of the stack. This means that any value stored in the stack memory scheme is accessible as long as the method hasn't finished execution and is still running. 1.2.2 Application of an ADT (P3) When managing a library, by apply the use of abstract data type Linked list, it helped me being able to search all author that have a specific first or last name. There are many others use for the Linked list but for me, it has help me easily search specific author by just enter their first or last name. Although the code is quite complicated to read but it works very well. This is the code to print all book that has author with a specific name the user entered using linked list. It will print out all book with similar name the user has entered. Figure 10 Linked list code

Here is the result. Figure 12 Code result Not only that, we can even have it print all of the book that is 20 years old! This is the code that will take the current year data and calculate to determine whenever the book is 20 years old or not. Figure 13 Linked list code that show all 20 years old book

From the list, the last number of the Library array is the published date of the book. There should be five book that is 20 years old:

  1. Tau biet Vo Vo 8000 2003
  2. Mi biet Ko Tuong 2700 1948
  3. Da re Vo Danh 2000 1992
  4. Idk Vo Danh 2000 1991
  5. Ai biet Vo Danh 2000 1990 Figure 14 Main function code 2

References Bisht, A., 2023. geeksforgeeks. [Online] Available at: https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ [Accessed 28 February 2023]. Chauhan, A., 2022. geeksforgeeks. [Online] Available at: https://www.geeksforgeeks.org/abstract-data-types/ [Accessed 27 February 2023]. geeksforgeeks, 2023. geeksforgeeks. [Online] Available at: https://www.geeksforgeeks.org/what-is-array/?ref=leftbar-rightbar [Accessed 28 February 2023]. geeksforgeeks, 2023. geeksforgeeks. [Online] Available at: https://www.geeksforgeeks.org/introduction-to-stack-data-structure-and-algorithm- tutorials/?ref=lbp [Accessed 28 February 2023]. programiz, 2023. programiz. [Online] Available at: https://www.programiz.com/java-programming/stack [Accessed 28 February 2023]. (Chauhan, 2022) (geeksforgeeks, 2023) (geeksforgeeks, 2023) (programiz, 2023) (Bisht, 2023)