Docsity

ASM 1_1649 Data structure and Algorithms (PASS), Slides of Data Structures and Algorithms

Partial preview of the text

Download ASM 1_1649 Data structure and Algorithms (PASS) and more Slides Data Structures and Algorithms in PDF only on Docsity!

Higher National in Computing

ASSIGNMENT 1

Name: Dong Sy Nhat Thanh

ID: GCS

Class: GCS1003A

Subject code: 164 9

Assignment due: Assignment submitted:

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 Student ID Class 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

Assignment Brief 1 (RQF)

Higher National Certificate/Diploma in Business

Student Name/ID Number: Unit Number and Title: Unit 19: Data Structures and Algorithms Academic Year: 2021 Unit Assessor: Assignment Title: Examine and specify ADT and DSA Issue Date: Submission Date: Internal Verifier Name: Date: Submission Format: Format: ● The submission is in the form of an individual written report and a presentation. This should be written in a concise, formal business style using single spacing and font size 12. You are required to make use of headings, paragraphs and subsections as appropriate, and all work must be supported with research and referenced using the Harvard referencing system. Please also provide a bibliography using the Harvard referencing system. Submission ● Students are compulsory to submit the assignment in due date and in a way requested by the Tutor. ● The form of submission will be a soft copy posted on http://cms.greenwich.edu.vn/. ● Remember to convert the word file into PDF file before the submission on CMS. Note: ● The individual Assignment must be your own work, and not copied by or from another student. ● If you use ideas, quotes or data (such as diagrams) from books, journals or other sources, you must reference your sources, using the Harvard style. ● Make sure that you understand and follow the guidelines to avoid plagiarism. Failure to comply this requirement will result in a failed assignment. Unit Learning Outcomes:

Learning Outcomes and Assessment Criteria (Assignment 1) Pass Merit Distinction LO1 Examine abstract data types, concrete data structures and algorithms D1 Analyse the operation, using illustrations, of two network shortest path algorithms, providing an example of each. P1 Create a design specification for data structures explaining the valid operations that can be carried out on the structures. P2 Determine the operations of a memory stack and how it is used to implement function calls in a computer. M1 Illustrate, with an example, a concrete data structure for a First In First out (FIFO) queue. M2 Compare the performance of two sorting algorithms. LO2 Specify abstract data types and algorithms in a formal notation D2 Discuss the view that imperative ADTs are a basis for object orientation and, with justification, state whether you agree. P3 Using an imperative definition, specify the abstract data type for a software stack. M3 Examine the advantages of encapsulation and information hiding when using an ADT.

Table of Contents

P1. Create a design specification for data structures explaining the valid

operations that can be carried out on the structures.

1. Data Structures

An ADT is a type of data structure defined by its behavior rather than its implementation in computer science. It is a means of encapsulating both the data and the actions that can be performed on the data.

2. Type of ADT

List, stack, and queue are a few typical ADTs that have proven helpful in a wide range of applications. 2.1. List The list of Abstract Data Types is made up of many elements that are linearly related to one another. A linear connection implies that, with the exception of the first variable, each variable on the list has a distinct successor. Moreover, lists feature a naturally occurring characteristic called size, which is essentially the number of elements in the list. This list can be modified. List is another abstraction in which other classes support the real details of the data form. These classes include LinkedList, which is inside carried out using the data construction of LinkedList, and Array List, which is inside carried out using Arrays. The List ADT Functions is given below:

  • 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. 2. 1 .1. Example: Linked List: A linked list is a common data structure that consists of a chain of nodes, each of which has a value and a pointer to the node after it in the chain.

The last component of the list points to null, and the head pointer is focused on the main node. The head pointer zeroes out at null when the list is empty. Figure 1 : Linked List (Linked List Data Structure - GeeksforGeeks) There are four types of linked lists:

  • Singly linked lists.
  • Doubly linked lists.
  • Circular linked lists.
  • Circular doubly linked lists Figure 2 : Singly linked lists (Singly Linked List definition & meaning DSA - GeeksforGeeks) Figure 3 : Doubly linked lists (Introduction and Insertion in a Doubly Linked List - GeeksforGeeks)
  1. Other applications: Linked lists are also used in various other applications such as managing the undo/redo stack in text editors and web browsers, tracking changes in version control systems, and representing polynomials in computer algebra systems. Applications of linked lists in the real world:
  2. File systems: Many operating systems use linked lists to manage files and folders.
  3. Memory management: Linked lists are used to manage memory in computers, allowing the computer to allocate and deallocate memory as needed.
  4. Network routing: Network routers use linked lists to store information about which paths data should take to reach its destination.
  5. Operating system processes: Linked lists can be used to store information about processes running in an operating system and to manage their execution.
  6. Databases: Databases often use linked lists to store records and to keep track of which records are related to each other.
  7. Web browsers: Web browsers use linked lists to store information about the webpages that the user has visited. 2.2. Stack An abstract data type with a bounded (predetermined) limit is a stack. It is a fundamental data structure that enables the addition and removal of items within a particular request. When an element is added, it always rises to the top of the stack, and the element that is at the top of the stack, as in a pile of objects, is the major element that can be deleted. Elements of a similar type are arranged in succeeding requests in a stack. All actions take place at a single end that is at the top of the stack, and the following operations are possible:
    • 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.

2.2.1. Example Figure 6 : Illustration for push a element onto stack and after push (Data Structure and Algorithms - Stack (tutorialspoint.com)) Figure 7 : Illustration for pop operation the topmost element and stack after pop (Data Structure and Algorithms - Stack (tutorialspoint.com)) 2.2.2. Applications Function Call: Function Call: Information about dynamic functions or subroutines is stored on the stack. String Reversal: Switching a string is nothing new to Stack. Individual characters from the string are pushed into the stack, and then someone is pulled out of the stack.

Dequeue: Removes an element from the queue's beginning and returns it Figure 9 : Dequeue (p5.js | Dequeue Operation in Queue - GeeksforGeeks) 2.3.2. Applications

  • Managing requests on a single shared resource such as CPU scheduling and disk scheduling
  • Handling hardware or real-time systems interrupts
  • Handling website traffic
  • Routers and switches in networking
  • Maintaining the playlist in media players

P2. Determine the operations of a memory stack and how it is used to

implement function calls in a computer.

1. Memory Stack

A data structure called a memory stack is used to store and control how much memory a program is allotted. Due to the fact that it is a Last In First Out (LIFO) data structure, the last item added to the stack is also the first one to be taken away from it. The system creates a section of memory known as a stack when a program is run. This stack is used to store the program's data, function parameters, and return addresses. The system maintains the return address and the function's parameters on the stack each time a function is called, and the size of the stack is fixed and predetermined. Data is removed from the stack and control is returned to the original application after the function completes. Figure 10 : Memory Stack (Stack Memory - an overview | ScienceDirect Topics) 1.1. How the memory is organized Data is arranged in a Last-In-First-Out (LIFO) pattern using a sort of memory management technique called stack. As a result, the item that was most recently placed to the stack is the one that is taken out first. The stack is a tool used in memory management to control memory allocation. The memory management will allot memory for a program to use when it is executing. Each block of this RAM that is allocated is placed in the stack. The memory manager will maintain track of the stack's blocks and always move the following block to the top. The memory management will take the top block off the stack and give it to the program when it requires extra memory. The memory management will release the memory and add it back to the stack after the program is done.

items) (stack). Following the initial plate, which is pushed to the bottom of the stack, all subsequent plates are placed in order. The most inaccessible data item is inserted first and placed at the bottom of the stack. The Pop Operation: Data elements are removed from a loaded stack using the pop operation. In our plate dispenser picture, the most recently inserted plate (data item) is at the top of the stack. This data item is the first to be erased, and it is popped off the stack. Examine the spring-loaded mechanism at the base of the plate dispenser. When a plate is removed, it rises the stack of plates. Things in that order continue to appear in memory. The Peek Operation: During this procedure, no data items are added or removed from the stack. The peek action only requests the data item at the top of the stack (the most recently pushed item). The Search Operation: During this procedure, no data items are added to or removed from the stack. Any data item in the stack whose address the search operation requests. It establishes the item's placement in relation to the item at the top of the stack.

2. Function call in memory stack

In memory management, a method (function) call is stored in a stack. Each time a method is called, a new entry is added to the stack, indicating the location of the instruction that called the method, the arguments it received, and the address to which it should return. When a method returns, the top of the stack is removed, and the instruction that called the method is then reached using the return address. After the program reaches the main function, the stack is completely popped off and the program terminates, which is repeated numerous times. Figure 12 : A stack that stores activation records. (Function Calls and the Call Stack — Programming and Data Structures 0.2 documentation (eecs280staff.github.io))

Figure 13 : A stack that grows downward rather than upward. (Function Calls and the Call Stack — Programming and Data Structures 0.2 documentation (eecs280staff.github.io)) The main() function is invoked when the program is run, which causes an activation record to be created and added to the top of the stack. The activation record for foo() is thus placed at the top of the stack when main() invokes foo(). The activation record for bar() is then placed on the stack after bar() is called. Bar(activation )'s record is taken off the stack when it returns. When foo() is finished, its activation record is deleted. Lastly, when the method returns, the activation record for main() is deleted.