Data Structure - Assignment 1
Data Structure - Assignment 1
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
Assignment Brief 1 (RQF)
Higher National Certificate/Diploma in Business
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.
LO1 Examine abstract data types, concrete data structures and algorithms
LO2 Specify abstract data types and algorithms in a formal notation
Assignment 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.
Page | 4
Learning Outcomes and Assessment Criteria (Assignment 1)
Page | 5
Contents
Assignment Brief 1 (RQF) ...................................................................................................... 3
Higher National Certificate/Diploma in Business ................................................................... 3
Introduction ................................................................................................................... 7
1. Abstract data type (P1) ........................................................................................... 8
1.1 Definition ................................................................................................................. 8
1.2 Examples ................................................................................................................ 8
2. ADT usages .......................................................................................................... 10
2.1 Application of Stack in memory (P2).................................................................. 10
2.2 Application of an ADT (P3) ................................................................................ 26
Page | 6
Introduction
If the data is stored in well organized way on storage media and in computer's
memory, then it can be accessed quickly for processing that further reduces the latency
and the user is provided fast response.
Data structure introduction refers to a scheme for organizing data, or in other words a
data structure is an arrangement of data in computer's memory in such a way that it could
make the data quickly available to the processor for required calculations. A data structure
should be seen as a logical concept that must address two fundamental concerns. First,
how the data will be stored, and second, what operations will be performed on it? As data
structure is a scheme for data organization so the functional definition of a data structure
should be independent of its implementation. The functional definition of a data structure is
known as ADT (Abstract Data Type) which is independent of implementation. The
implementation part is left on developers who decide which technology better suits to their
project needs.
Page | 7
1. Abstract data type (P1)
1.1 Definition
Abstract Data Types
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined
by a set of values 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.
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.
1.2 Examples
List ADT
The data is generally stored in key sequence in a list which has a head structure
consisting of count, pointers and address of compare function needed to compare the data
in the list.
The data node contains the pointer to a data structure and a self-referential pointer which
points to the next node in the list.
Page | 8
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.
Stack ADT
• In Stack ADT Implementation instead of data being stored in each node, the pointer
to data is stored.
• The program allocates memory for the data and address is passed to the stack
ADT.
• The head node and the data nodes are encapsulated in the ADT. The calling
function can only see the pointer to the stack.
• The stack head structure also contains a pointer to top and count of number of
entries currently in stack.
• 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) follows the basic design of the stack abstract
data type.
• Each node contains a void pointer to the data and the link pointer to the next
element in the queue. The program’s responsibility is to allocate memory for storing
the data.
• 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.
• Features of ADT:
• Abstraction: The user does not need to know the implementation of the data
structure.
Page | 9
• Better Conceptualization: ADT gives us a better conceptualization of the real world.
• Robust: The program is robust and has the ability to catch errors.
From these definitions, we can clearly see that the definitions do not specify how these
ADTs will be represented and how the operations will be carried out. There can be
different ways to implement an ADT, for example, the List ADT can be implemented using
arrays, or singly linked list or doubly linked list. Similarly, stack ADT and Queue ADT can
be implemented using arrays or linked lists.
2. ADT usages
2.1 Application of Stack in memory (P2)
2.1.1 How the memory is organized, how a method (function) calls is implemented
with stack
Queue is also known as an abstract data structure, somewhat similar like Stacks. Unlike
stacks, queue is open at both its ends. One end is always used to insert data (enqueue)
and the other is used to remove data (dequeue). Queue follows First-In-First-Out
methodology, example: the data item stored first will be accessed first. (tutorialspoint, n.d.)
A real-life scenario in the form of example for queue will be the queue of people waiting to
accomplish particular task where the first person in the queue is the first person to be
served first.
Page | 10
Figure 2. Example element in Queue
Other examples can also be noted within a computer system where the queue of tasks
arranged in the list to perform for the line printer, for accessing the disk storage, or even in
the time-sharing system for the use of CPU. So basically, queue is used within single
program where there are multiple programs kept in the queue or one task may create other
tasks which must have to be executed in turn by keeping them in the queue.
Basic Operations:
Queue operations may involve initializing or defining the queue, utilizing it and then
completely erasing it from the memory:
• Enqueue() : add ( stored) an item to the queue.
• Dequeue() : remove ( access) an item from the queue.
Few more functions are required to make the above-mentioned queue operation efficient:
• peek() : Gets the element at the front from the queue without removing it.
• isfull() : Checks the queue is full or not.
• isempty() : Checks the queue is empty or not.
• Size(): Show how many elements in queue.
Page | 11
Peek() Peek Array → Int
Queue operation:
• Enqueue():
Queues maintain two data pointers, front and rear. Its operations are comparatively
difficult to implement than that of stacks.
The following steps should be taken to enqueue (insert) data into Queue:
• Step 1: Check if the queue is full.
• Step 2: If the queue is full, produce overflow error and exit.
• Step 3: If the queue is not full, increment rear pointer to point the next empty
space.
• Step 4: Add data element to the queue location, where the rear is pointing.
• Step 5: return success.
Page | 12
public void enQueue(char x)
{
• Dequeue():
Accessing data from the queue is a process of two tasks: access the data where front is
pointing and remove the data after access.
The following steps below are taken to perform dequeue() operation:
• Step 1: Check if the queue is empty.
• Step 2: If the queue is empty, produce underflow error and exit.
• Step 3: If the queue is not empty, access the data where front is pointing.
• Step 4: Increment front pointer to point to the next available data element.
• Step 5: Return success.
Page | 13
Implementation of dequeue() in C# programming language:
Page | 14
IsFull():
As we are using single dimension array to implement queue, we just check for the rear
pointer to reach at max-size to determine that the queue is full or not. In case we maintain
the queue in circular linked-list, the algorithm will differ.
• IsEmpty():
The operation that check for the front pointer if it is less than 0 or larger than rear pointer, it
tells that the queue is not yet initialized or empty.
Page | 15
Stack data structure
A stack is an Abstract Data Type (ADT), commonly used in most programming
languages. It is named stack as it behaves like a real-world stack, for example: a deck of
cards or a pile of plates. (tutorialspoint, n.d.)
Stack is linear data structure which follows a particular order in which the operations are
performed. The order may be LIFO (Last In First Out) or FILO (First In Last Out).
(geeksforgeeks, www.geeksforgeeks.org, n.d.)
Basic Operations:
Page | 16
Stack operations will involve initializing the stack, using it and then DE-initializing it. Apart
from these basic stuffs, a stack is used for the following two primary operations:
push() : Pushing (storing) an element on the stack.
• pop() : Removing (accessing) an element from the stack.
To use stack efficiently, we need to check the status of stack as well. For the same
purpose, the following functionality is added to stacks:
• peek() : get the top data element of the stack, without removing it.
• isFull() : check if stack is full.
• isEmpty() : check if stack is empty.
Page | 17
Stack operation:
• Push():
The process of putting a new data element into stack is known as Push Operation. Push
operation involves a series of steps:
Pop():
Accessing the content while removing it from the stack, is known as Pop Operation. In an
array implementation of pop() operation, the data element is not actually removed, instead
top is decremented to lower position in the stack to point to the next value. But in linked-list
implementation, pop() actually removes data element and deal-locates memory space.
Page | 18
• Step 1: Checks if the stack is empty.
• Step 2: If the stack is empty, produces an error and exit.
• Step 3: If the stack is not empty, accesses the data element at which top is pointing.
• Step 4: Decreases the value of top by 1.
• Step 5: Returns success.
Page | 19
•
Peek():
The Peek() operation returns the top element value from the stack but not removing it.
Page | 20
•
public int Peek()
{
if (IsEmpty())
{
Console.WriteLine("Stack is empty");
}
return stackArray[top]; }
isFull():
IsFull() operation is the operation that check whether a stack is full or not, then returns a
Boolean value.
Page | 21
•
IsEmpty():
Empty() operation is the operation that check whether stack is empty or not. This
operation is slightly different from IsFull() operation, where the condition for it to return
“True” value is only when the top value at “-1” since the index in array starts from 0.
Page | 22
Operation of memory stack
Memory stacks are linear data structures (locations) used to store data in a computer's
memory. They may also be referred to as queues. Data within a stack must always of the
same type. An example of a stack is illustrated.
When a stack is accessed (has items inserted or removed), it is done in an orderly manner
using LIFO. LIFO is an acronym which stands for Last In First Out. The computer uses this
approach to service data requests received by the computer's memory. LIFO dictates that
data inserted or stored last in any given stack, must be the first removed. If that was
applied to our queue for the movies in Figure 1 above, there would be chaos! Operations
on a stack are discussed in the following sections using Figure below:
23
Figure 12. LIFO in memory stack
• The Push Operation:
The push operation involves inserting data items into a stack. Let us examine our
restaurant plate dispenser. The push operation adds plates (data items) to the plate
dispenser (stack). The first plate is pushed to the bottom of the stack with all subsequent
plates following in order after it. The first data item inserted is the most inaccessible and
positioned at the bottom of the stack.
• The Pop Operation:
The pop operation involves removing data items from a loaded stack. In our plate
dispenser illustration, the last plate (data item) added is positioned at the top of the stack.
This data item is popped out of the stack as the first item to be removed. Think of the
spring loading system at the base of the plate dispenser. It pushes the stack of plates
upwards each time a plate is removed. In memory, items continue to be popped out in that
order.
• The Peek Operation:
In this operation, no data item is added to or removed from the stack. The peek operation
simply requests the address location of the data item at the top of the stack (the last item
to be pushed).
• The Search Operation:
In this operation, no data item is added to or removed from the stack either. The search
operation requests the address location of any data item in the stack. It determines the
item location in reference to the item at the top of the stack.
• Duplicate operation:
The duplicate operation pops the top item out of the stack and then pushed again (twice),
so that an additional copy of the former top item is now on top, with the original below it.
24
• Swap or exchange operation:
The swap operation pops 2 topmost items out of the stack and then pushed back buy with
exchanged place
• Rotate operation:
In rotate operation, the n topmost items are moved on the stack in a rotating fashion. For
example, if n=3, items 1, 2, and 3 on the stack are moved to positions 2, 3, and 1 on the
stack, respectively. Many variants of this operation are possible, with the most common
being called left rotate and right rotate.
A call stack is composed of stack frames (also called activation records or activation
frames). These are machine dependent and ABI-dependent data structures containing
subroutine state information. Each stack frame corresponds to a call to a subroutine which
has not yet terminated with a return. (wikipedia, wikipedia, n.d.)
25
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):
26
Maintaining directory of names
Performing arithmetic operations on long integers
Manipulation of polynomials by storing constants in the node of linked list
representing sparse matrices
Applications of linked list in real world-
Image viewer – Previous and next images are linked, hence can be accessed by next and
previous button.
Previous and next page in web browser – We can access previous and next url searched
in web browser by pressing back and next button since, they are linked as linked list.
Music Player – Songs in music player are linked to previous and next song. you can play
songs either from starting or ending of the list.
Applications of Circular Linked Lists:
Useful for implementation of queue. Unlike this implementation, we don’t need to maintain
two pointers for front and rear if we use circular linked list. We can maintain a pointer to
the last inserted node and front can always be obtained as next of last.
Circular lists are useful in applications to repeatedly go around the list. For example, when
multiple applications are running on a PC, it is common for the operating system to put the
running applications on a list and then to cycle through them, giving each of them a slice of
time to execute, and then making them wait while the CPU is given to another application.
It is convenient for the operating system to use a circular list so that when it reaches the
end of the list it can cycle around to the front of the list.
Circular Doubly Linked Lists are used for implementation of advanced data structures
like Fibonacci Heap.
Application of Doubly Linked Lists:
Redo and undo functionality.
Use of Back and forward button in a browser.
Most recently used section is represented by Doubly Linked list.
Other Data structures like Stack, HashTable, BinaryTree can also be applied by Doubly
Linked List.
An example problem: Design a data structure that supports following operations
efficiently.
getMin : Gets minimum
extractMin : Removes minimum
getMax : Gets maximum
extractMax : Removes maximum
insert : Inserts an item. It may be assumed that the inserted item is always greater than
maximum so far. For example, a valid insertion order is 10, 12, 13, 20, 50.
Doubly linked list is the best solution here. We maintain head and tail pointers, since
inserted item is always greatest, we insert at tail. Deleting an item from head or tail can be
done in O(1) time. So all operations take O(1) time.
27