














































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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 Date Received 1st submission
Re-submission Date Date Received 2nd submission
Student Name Phan Minh Tiến Student ID GCD
Class GCD1001^ Assessor name Hồ^ Văn 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.
Student’s signature Tiến
Grading grid
P1 P2 P3 M1 M2 M3 D1 D
Grade: Assessor Signature: Date:
Internal Verifier’s Comments:
IV Signature:
I. Abstract Data Type (ADT)
Abstract data type Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of value 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. The process of providing only the essentials and hiding the details is known as abstraction.
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, a user only needs to know what a data type can do, but not how it will be implemented. Think of ADT as a black box which hides the inner structure and design of the data type. Now we’ll define three ADTs namely List ADT, Stack ADT, Queue ADT. Example of ADT Array of ADT:
Figure 2: Class Array
Main method:
Figure 3: Main method
Figure 4: Class LinkedList
I. What is memory stack A stack can be implemented in a random access memory (RAM) attached to a CPU. The implementation of a stack in the CPU is done by assigning a portion of memory to a stack operation and using a processor register as a stack pointer. The starting memory location of the stack is specified by the processor register as stack pointer.
II. Operations A stack contains elements of the same type arranged in sequential order. All operations take place at a single end that is top of the stack. It works on principle LIFO (Last In First Out). Moreover, the stack following operations can be performed:
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 the 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.
Figure 6: Principle of Stack
III. Exception Operations pop and top cannot be performed if the stack is empty
Attempting the execution of pop or top on an empty stack should throws a StackEmtptyExceptiom. Operations push sometimes cannot be performed if it’s not enough memory. Attempting the execution of push when there is not enough memory should throws a OutOfMemoryError.
IV. Application
Any sort of nesting (such as parentheses) Evaluating arithmetic expressions (and other sorts of expression) Implementing function or method calls Keeping track of previous choices (as in backtracking) Keeping track of choices yet to be made (as in creating a maze) Undo sequence in a text editor Auxiliary data structure for algorithms Component of other data structure
V. Method calls and the implementation by using stack Each time a method is called, an activation record (AR) is allocated for it. This record contains the following information: Parameters and local variables used in the called method Dynamic link: a pointer to the caller’s activation record Return address to resume control by the caller (address of instruction immediately following the call) Return value for a method not declared as void Since the size of AR may vary from one call to another, returned value is placed right above the AR of the caller. Each new AR is placed on top of the run-time stack When a method terminates, its AR is removed from the top of the run-time stack. Thus, the first AR placed on the stack is the last one removed.
VI. Implement Stack by Array in Java.
Class Stack:
I. Definition of Queue
II. Queue Representation
Figure 8: Queue
III. Basic Operations Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues. enqueue() – add (store) an item to the queue. dequeue() – remove (access) an item from the queue. Few more function are required to make the above-mentioned queue operation efficient. These are peek() – Gets the element at the front of the queue without removing it. isfull() – Checks if the queue is full.
isempty() – Checks if the queue is empty. In queue, we always dequeuer(or access) data, pointed by front pointer and while enquiring (or storing) data in the queue we take help of rear pointer.
IV. Applications of Queue Queue is used when things don’t have to be processed immediately, but have to be processed in First In First Out order like Breadth First Search. This property of Queue makes it also useful in following kind of scenarios. When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling. When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.
V. Array implementation of Queue in Java
Class Queue
Figure 10: Class Car
Class QueueForCars: