You are on page 1of 27

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing


Unit number and title Unit 19: Data Structures and Algorithms
Submission date 17/04/2021 Date Received 1st submission
Re-submission Date Date Received 2nd submission
Student Name HUYNH NHAT NAM Student ID GCS190293
Class GCS0805_NX Assessor name NGUYEN NGOC TU
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 NAM
Grading grid
P1 P2 P3 M1 M2 M3 D1 D2
Summative Feedback: Resubmission Feedback:

Grade: Assessor Signature: Date:

Internal Verifier’s Comments:

IV Signature:
ASSIGNMENT 1 BRIEF
Qualification BTEC Level 5 HND Diploma in Business

Unit number Unit 19: Data Structures and Algorithms

Assignment title Examine and specify ADT and DSA

Academic Year

Unit Tutor

Submission
Issue date
date

IV name and 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 Tutors. The form of submission will be a soft copy in PDF posted
on corresponding course of http://cms.greenwich.edu.vn/
Note: The Assignment must be your own work, and not copied by or from another
student or from
books etc. 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
know how to reference properly, and that understand the guidelines on plagiarism. If you
do not, you definitely get fail

Assignment Brief and Guidance:

Scenario: You work as in-house software developer for Softnet Development Ltd, a
software body-shop providing network provisioning solutions. Your company is part of a
collaborative service provisioning development project and your company has won the
contract to design and develop a middleware solution that will interface at the front-end
to multiple computer provisioning interfaces including SOAP, HTTP, JML and CLI, and the
back-end telecom provisioning network via CLI .
Your account manager has assigned you a special role that is to inform your team about
designing and implementing abstract data types. You have been asked to create a
presentation for all collaborating partners on how ADTs can be utilised to improve software
design, development and testing. Further, you have been asked to write an introductory
report for distribution to all partners on how to specify abstract data types and algorithms
in a formal notation.

Tasks
Part 1
You will need to prepare a presentation on how to create a design specification for data
structures, explaining the valid operations that can be carried out on the structures using
the example of:
1. A stack ADT, a concrete data structure for a First In First out (FIFO) queue.
2. Two sorting algorithms.
3. Two network shortest path algorithms.
Part 2
You will need to provide a formal written report that includes the following:
1. Explanation on how to specify an abstract data type using the example of software
stack.
2. Explanation of the advantages of encapsulation and information hiding when using
an ADT.
3. Discussion of imperative ADTs with regard to object orientation.
Learning Outcomes and Assessment Criteria

Pass Merit Distinction

LO1 Examine abstract data types, concrete data structures


and algorithms
D1 Analyse the operation,
P1 Create a design M1 Illustrate, with an using illustrations, of two
specification for data example, a concrete data network shortest path
structures explaining the structure for a First In First algorithms, providing an
valid operations that can out (FIFO) queue. example of each.
be carried out on the M2 Compare the
structures. performance of two sorting
algorithms.
P2 Determine the
operations of a memory
stack and how it is used to
implement function calls in
a computer.

LO2 Specify abstract data types and algorithms in a formal


notation
D2 Discuss the view that
P3 Using an imperative M3 Examine the advantages imperative ADTs are a basis
definition, specify the of encapsulation and for object orientation and,
abstract data type for a information hiding when with justification, state
software stack. using an ADT. whether you agree.
LO1 Examine abstract data types, concrete data structures and algorithms
What are data structure?

A data structure is a specialized format for organizing, processing, retrieving and storing data.
There are several basic and advanced types of data structures, all designed to arrange data to
suit a specific purpose. Data structures make it easy for users to access and work with the data
they need in appropriate ways. Most importantly, data structures frame the organization of
information so that machines and humans can better understand it.

In computer science and computer programming, a data structure may be selected or designed
to store data for the purpose of using it with various algorithms. In some cases, the algorithm's
basic operations are tightly coupled to the data structure's design. Each data structure contains
information about the data values, relationships between the data and -- in some cases --
functions that can be applied to the data.

For instance, in an object-oriented programming language, the data structure and its associated
methods are bound together as part of a class definition. In non-object-oriented languages,
there may be functions defined to work with the data structure, but they are not technically
part of the data structure.

How are data structure used?

In general, data structures are used to implement the physical forms of abstract data types.
Data structures are a crucial part of designing efficient software. They also play a critical role in
algorithm design and how those algorithms are used within computer programs.
For example: JavaScript arrays and objects are common coding structures used for storing and
retrieving information.

Some examples of how data structures are used include the following:
• Storing data: Data structures are used for efficient data persistence, such as specifying
the collection of attributes and corresponding structures used to store records in a
database management system.
• Managing resources and services: Core operating system (OS) resources and services
are enabled through the use of data structures such as linked lists for memory allocation,
file directory management and file structure trees, as well as process scheduling queues.
• Data exchange: Data structures define the organization of information shared between
applications, such as TCP/IP packets.
• Ordering and sorting: Data structures such as binary search trees -- also known as an
ordered or sorted binary tree -- provide efficient methods of sorting objects, such as
character strings used as tags. With data structures such as priority queues,
programmers can manage items organized according to a specific priority.
• Indexing: Even more sophisticated data structures such as B-trees are used to index
objects, such as those stored in a database.
• Searching: Indexes created using binary search trees, B-trees or hash tables speed the
ability to find a specific sought-after item.

Types of data structure:


Figure 1: Types of Data Structure

Primitive Data Structures

These are the structures which are supported at the machine level, they can be used to make
non-primitive data structures. These are integral and are pure in form. They have predefined
behavior and specifications.

Examples: Integer, float, character, pointers.


The pointers, however don’t hold a data value, instead, they hold memory addresses of the
data values. These are also called the reference data types.

Non-primitive Data Structures

The non-primitive data structures cannot be performed without the primitive data structures.
Although, they too are provided by the system itself yet they are derived data structures and
cannot be formed without using the primitive data structures.
The Non-primitive data structures are further divided into the following categories:

Arrays

Arrays are a homogeneous and contiguous collection of same data types. They have a static
memory allocation technique, which means, if memory space is allocated for once, it cannot be
changed during runtime. The arrays are used to implement vectors, matrices and also other
data structures. If we do not know the memory to be allocated in advance then array can lead
to wastage of memory. Also, insertions and deletions are complex in arrays since elements are
stored in consecutive memory allocations.
Files

A file is a collection of records. The file data structure is primarily used for managing large
amounts of data which is not in the primary storage of the system. The files help us to process,
manage, access and retrieve or basically work with such data, easily.

Lists

The lists support dynamic memory allocation. The memory space allocated, can be changed at
run time also. The lists are of two types:

a) Linear Lists

The linear lists are those which have the elements stored in a sequential order. The insertions
and deletions are easier in the lists. They are divided into two types:

1. Stacks: The stack follows a “LIFO” technique for storing and retrieving elements. The
element which is stored at the end will be the first one to be retrieved from the stack.
The stack has the following primary functions:
a. Push(): To insert an element in the stack.
b. Pop(): To remove an element from the stack.
2. Queues: The queues follow “FIFO” mechanism for storing and retrieving elements. The
elements which are stored first into the queue will only be the first elements to be
removed out from the queue. The “ENQUEUE” operation is used to insert an element
into the queue whereas the “DEQUEUE” operation is used to remove an element from
the queue.
b) Non Linear Lists
The non linear lists do not have elements stored in a certain manner. These are:

1. Graphs: The Graph data structure is used to represent a network. It comprises of vertices
and edges (to connect the vertices). The graphs are very useful when it comes to study a
network.
2. Trees: Tree data structure comprises of nodes connected in a particular arrangement and
they (particularly binary trees) make search operations on the data items easy. The tree
data structures consists of a root node which is further divided into various child nodes
and so on. The number of levels of the tree is also called height of the tree.

Figure 2: Example of Tree


P1 Create a design specification for data structures explaining the valid operations that can be
carried out on the structures.

ADT (Abstract data structure)

The abstract data type (ADT) is a type (or class) for objects whose behavior is determined by a
set of values and a set of activities. The definition of ADT only refers to what activities will be
performed, not how these activities will be implemented. It does not specify how data will be
organized in memory and which algorithm will be used to perform the operations. It is called
abstract online because it provides an independent view of implementation. The process of
providing only the essentials and hiding the details is called abstraction. Data type users don't
need to know that the data type is deployed, for example, we used int, float, char data types
only with knowledge of possible values and operations. can be done on them without any idea
how these types are done. Therefore, users only need to know what kind of data can do, not
how it will do. We can think of ADT as a black box hiding the internal structure and design of
the data type. ADTs are dived into three types List ADT, Stack ADT, Queue ADT.

Introduce the Queue

The queue is a linear structure in a particular order in which the operations are performed in
sequence in First In First Out (FIFO).

Figure 3:Illustrating images about queue


The image above depicts the structure of a queue. To make it easier to understand, I will give
an example when you go to the counter at the supermarket, the one who pays you is only one
while the customer waiting for payment is very crowded. Therefore, you have to wait in line
to wait, if you queue first, you will be paid first.
Here are some basic operations associated with queues:
o Enqueue (): add an item to the queue.
o Dequeue (): remove an item from the queue.
o Peek (): Get the element in front of the queue without deleting it. o isfull (): Check if the
queue is full.
o isempty (): Check if the queue is empty.
Example for queue and its operations:
Name: Que[]

Symbol: queue
Data type: integer array [5] Data values: Q=[, ,3,8,9,1]

Operators:

Operators:
Operation Process Results
Enqueue(7 First, add element “7” to the rear position of the queue Q=[7,3,4,9,1]
) Q[]
Dequeue() Then, Remove the front element (“1”) from the queue S=[7,3,4,9, ]
Q[]
Peek() Later, return the value of the front element (“9”) of the S=[7,3,4,9, ]
queue
Front = 9
Q[]
IsFull() Checks whether queue Q [] is full or not and returns False
Boolean
type value
IsEmpty() Checks whether queue Q [] is empty or not and returns False
Boolean
type value

Here are some basic operations when working with Queue as follows:

// Queue implementation in Java


public class Queue {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;

Queue() {
front = -1;
rear = -1;
}

boolean isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}

boolean isEmpty() {
if (front == -1)
return true;
else
return false;
}

void enQueue(int element) {


if (isFull()) {
System.out.println("Queue is full");
} else {
if (front == -1)
front = 0;
rear++;
items[rear] = element;
System.out.println("Inserted " + element);
}
}

int deQueue() {
int element;
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
} else {
element = items[front];
if (front >= rear) {
front = -1;
rear = -1;
} /* Q has only one element, so we reset the queue after deleting it. */
else {
front++;
}
System.out.println("Deleted -> " + element);
return (element);
}
}

void display() {
/* Function to display elements of Queue */
int i;
if (isEmpty()) {
System.out.println("Empty Queue");
} else {
System.out.println("\nFront index-> " + front);
System.out.println("Items -> ");
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");

System.out.println("\nRear index-> " + rear);


}
}

public static void main(String[] args) {


Queue q = new Queue();

// deQueue is not possible on empty queue


q.deQueue();
// enQueue 5 elements
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.enQueue(4);
q.enQueue(5);

// 6th element can't be added to because the queue is full


q.enQueue(6);

q.display();

// deQueue removes element entered first i.e. 1


q.deQueue();

// Now we have just 4 elements


q.display();

}
}

The result

Inserted 4

Inserted 5

Queue is full

Front index-> 0

Items ->

1 2 3 4 5

Rear index-> 4

Deleted -> 1

Front index-> 1
Items ->

2 3 4 5

Rear index-> 4

P2 Determine the operations of a memory stack and how it is used to implement function calls in
a computer.

Determine the operations of a memory stack

The stack operation may involve initializing the stack, using it, and then re-initializing it.
In addition to these basics, a stack is used for the following two main operations -
• Push (): Push (storing) an element on the stack.
• Pop (): Removes (accessing) an element from
the stack. When data is pushed onto the stack.
The most effective way to use the stack is to check the status of the stack. For the same
purpose, the following function is added to the stack -
• Peek (): get the top data element of the stack without deleting it.
• Is full (): check if the stack is full.
• Is empty (): check if the stack is empty.
At all times, we maintain a pointer to the last pushed data on the stack because this
pointer always represents the top of the stack. Hence it is named top. The top pointer
provides the top value of the stack without actually removing it.

Push Operation is a process of putting a new data element on a stack called a Push.
Push operation includes the following steps:
Step 1 - Check if the stack is full.
Step 2 - If the stack is full, create an error and exit.
Step 3 - If the stack is not full, increase from top to bottom to indicate the next
empty space. Step 4 - Add the data element to the position of the stack where
the top is pointing.
Step 5 - Return success.

Figure 4: Push Operations


If the linked list is used to implement the stack, then in step 3, we need dynamic space
allocation
.
Pop Operation means accessing content while removing it from the stack. In an array
implementation of the pop () operation, the data element is not actually deleted but rather is
dropped to a lower position in the stack to point to the next value. But in implementing linked
lists, pop () actually removes data components and frees up memory space.
Here are the steps of a Pop activity:
Step 1 - Check if the stack is empty.
Step 2 - If the stack is empty, generate an error and exit.
Step 3 - If the stack is not empty, access the data element that the vertex is pointing. Step 4 -
Reduce the value of top 1.
Step 5 - Return success.

Figure 5: Pop Operation


Figure 6: The top of the stack in the memory

The image above helps us to realize that the organization of the stack in main memory but in
which the top of the stack is only accessed in memory. Another way is to assume that the top
of the stack and the position just below the top are stored in special processor registers: the
top of the first register and the stack register.
Figure 7:The top of the stack in registers

How it is used to implement function calls in a computer.

Before executing a function, a program pushes all the parameters for the function to the stack
in the reverse order in which they were recorded. The program then issues a call telling which
function it wants to start. Instruct the call to do two things:
• First, it pushes the address of the next instruction, the return address, onto the stack.
• Then, it modifies the command cursor to point to the beginning of the function.
A call stack consists of stack frames (also called activation records or activation frames). These
are machine-dependent data structures and the ABI contains subroutine status information.
Each stack frame corresponds to a call to the subroutine that has not ended with a return. For
example, if a subroutine called Draw Line is currently running has been called by a Draws ape
subroutine, the top of the call stack may be laid out as shown in the next picture.
A diagram like this can be drawn in either direction as long as the position of the vertex. So the
direction of stack development is understood. Moreover, independently of this, the different
architectures about whether call stacks grow to higher addresses or towards lower addresses.
The logic of the diagram is independent of the address choice.
Figure 8: : Implement function calls in a computer
The stack frame at the top of the stack is for the currently executing routine. The stack frame
usually includes at least the following items (in push order):
• The arguments (parameter values) passed to the routine (if any);
• The return address back to the routine's caller (e.g. in the DrawLine stack frame, an address
into DrawSquare's code); and space for the local variables of the routine (if any).

LO2 Specify abstract data types and algorithms in a formal notation


P3 Using an imperative definition, specify the abstract data type for a software stack.

Introduce the Stack

A stack is a linear data structure in a specific order in which operations are performed in the
order of LIFO (Last In First Out) or FILO (First In Last Out), respectively.
Figure 10: Introduce The Stack

There are many practical examples of a stack. Consider an example of stacked panels in a
canteen. The top plate is the first one to be removed, that is, the last placed plate remains in
the stack for the longest time. So it can be seen simply to follow the order LIFO (Last In First
Out) / FILO (First In Last Out). Here are some basic operations associated with queues:
• Push (): Pushing an element on the stack.
• Pop (): Removing 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. Example for stack and its operations: Name: Sta []
Symbol: stack
Data type: integer array [5] Data values: S = [, ,3,8,9,2]
Operators:
Operation Process Results
Push (7) First, add element “7” to the rear position of the stack S[] S=[7,3,8,9,2]
Pop () Then, Remove the front element (“1”) from the stack S[] S=[, ,3,8,9,2]
Peek() Later, return the value of the front element (“9”) of the S=[, .3,8,9,2]
stack S[]
Top = 3
IsFull() Checks whether stack S[] is full or not and returns Boolean False
type
value
IsEmpty() Checks whether stack S[] is empty or not and returns False
Boolean type
Figure 9: Introduce the Stack
value
Here are some basic operations when working with Stack as follows:

import java.util.*; //importing the stack class


class MyClass{
public static void main (String[] args)
{
// Creating a Stack
Stack<Integer> even = new Stack<>();
// pushing values in stack
even.push(0);
even.push(2);
even.push(4);
even.push(6);
even.push(8);
even.push(10);
even.push(12);
even.push(14);
even.push(16);

//printing the stack


System.out.println("Print Stack before pop:");
System.out.println(even);
// returning the number at the top of the stack and removing it
System.out.println("pop => " + even.pop());
System.out.println("pop => " + even.pop());
System.out.println("pop => " + even.pop());
//printing the stack
System.out.println("Print Stack after pop:");
System.out.println(even);
// accessing the number at the top of the stack but not removing it
System.out.println("Number on top of the stack is => " + even.peek());
// checking if the stack is empty or not
System.out.println("Is stack empty? Ans:" + even.empty());
// checking the position of 8 in the stack
System.out.println("Position of 8 from the top is " + even.search(8));
// check if 20 exists in the stack
System.out.println("Position of 20 from the top is " + even.search(20));
}
}

The result:
Print Stack before pop:
[0, 2, 4, 6, 8, 10, 12, 14, 16]
pop => 16
pop => 14
pop => 12
Print Stack after pop:
[0, 2, 4, 6, 8, 10]
Number on top of the stack is => 10
Is stack empty? Ans:false
Position of 8 from the top is 2
Position of 20 from the top is -1

Class diagram for Project:


References
Dba-oracle.com. 2021. Encapsulation and Abstract Data Types (ADT). [online] Available at:
<http://www.dba-oracle.com/t_object_encapsulation_abstract.htm> [Accessed 31 March
2021].

objects?, W., 2021. What is the difference between Abstract Data Types and objects?. [online]
Computer Science Stack Exchange. Available at:
<https://cs.stackexchange.com/questions/51847/what-is-the-difference-between-abstract-
data-types-and-objects> [Accessed 31 March 2021].

WhatIs.com. 2021. What is abstraction? - Definition from WhatIs.com. [online] Available at:
<https://whatis.techtarget.com/definition/abstraction> [Accessed 31 March 2021].

Guru99.com. 2021. Abstraction in Java | Abstract Class & Method with Example. [online]
Available at: <https://www.guru99.com/java-abstract-class-method.html> [Accessed 31
March 2021].

mean?, W., Alam, A. and Kaushik, N., 2021. What does "data abstraction" exactly mean?.
[online] Stack Overflow. Available at: <https://stackoverflow.com/questions/3634915/what-
does-data-abstraction-exactly-mean> [Accessed 31 March 2021].

mean?, W., Alam, A. and Kaushik, N., 2021. What does "data abstraction" exactly mean?.
[online] Stack Overflow. Available at: <https://stackoverflow.com/questions/3634915/what-
does-data-abstraction-exactly-mean> [Accessed 31 March 2021].

programming?, W. and Holterman, H., 2021. What is an abstract data type in object oriented
programming?. [online] Stack Overflow. Available at:
<https://stackoverflow.com/questions/1692933/what-is-an-abstract-data-type-in-object-
oriented-programming> [Accessed 31 March 2021].

The Crazy Programmer. 2021. Types of Data Structures - The Crazy Programmer. [online]
Available at: <https://www.thecrazyprogrammer.com/2018/10/types-of-data-
structures.html> [Accessed 31 March 2021].

Courses.cs.washington.edu. 2021. CSE373: Data Structures and Algorithms. [online] Available


at: <https://courses.cs.washington.edu/courses/cse373/> [Accessed 5 April 2021].

Page 26
Page 27

You might also like