Tải bản đầy đủ (.pdf) (94 trang)

Phan tuan manh bh00210 data structures algorithms asm1 1st Distinction

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.96 MB, 94 trang )

ASSIGNMENT 1 FRONT SHEET
Qualification

BTEC Level 5 HND Diploma in Computing

Unit number and title

Unit 19: Data Structures and Algorithms

Submission date

10/10/2023

Date Received 1st submission

Re-submission Date

Date Received 2nd submission

Student Name

Phan Tuan Manh

Student ID

BH00210

Class

IT0503


Assessor name

Ta Quang Hieu

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

1|P ag e


 Summative Feedback:


Grade:

 Resubmission Feedback:

Assessor Signature:

Date:

Internal Verifier’s Comments:

IV Signature:

2|P ag e


Table of Contents
I.

INTRODUCTION-------------------------------------------------------------------------------------------------------------------------------------- 7

II.

BODY --------------------------------------------------------------------------------------------------------------------------------------------------- 9
P1 CREATE A DESIGN SPECIFICATION FOR DATA STRUCTURES EXPLAINING THE VALID OPERATIONS THAT CAN BE CARRIED OUT ON THE STRUCTURES --- 9
1.
2.
3.

Data Structures ------------------------------------------------------------------------------------------------------------------------------- 9
Abstract data type---------------------------------------------------------------------------------------------------------------------------- 9

Stack ADT------------------------------------------------------------------------------------------------------------------------------------- 11
3.1
3.2
3.3

Stack operations and working mechanism -------------------------------------------------------------------------------------------------- 12
Implementation of a Stack ADT in Code------------------------------------------------------------------------------------------------------ 14
Applications/Examples of Stack---------------------------------------------------------------------------------------------------------------- 17

P2 DETERMINE THE OPERATIONS OF A MEMORY STACK AND HOW IT IS USED TO IMPLEMENT FUNCTION CALLS IN A COMPUTER ---------------------- 18
1. Memory Stack ------------------------------------------------------------------------------------------------------------------------------- 18
2. Operations of a Memory Stack ---------------------------------------------------------------------------------------------------------- 18
3. Using the Memory Stack for Function Calls ------------------------------------------------------------------------------------------- 20
M1 ILLUSTRATE, WITH AN EXAMPLE , A CONCRETE DATA STRUCTURE FOR A FIRST IN FIRST OUT (FIFO) QUEUE. -------------------------------------- 22
1. Queue Data Structure --------------------------------------------------------------------------------------------------------------------- 22
2. FIFO Principle of Queue ------------------------------------------------------------------------------------------------------------------- 22
3. Implementation of FIFO Queue ---------------------------------------------------------------------------------------------------------- 23
M2 COMPARE THE PERFORMANCE OF TWO SORTING ALGORITHMS. ------------------------------------------------------------------------------------ 26
1.

Sorting Algorithms ------------------------------------------------------------------------------------------------------------------------- 26
1.1
1.2
1.3

2.

Bubble Sort Algorithm: -------------------------------------------------------------------------------------------------------------------- 32
2.1
2.2

2.3

3.

The Purpose of Sorting -------------------------------------------------------------------------------------------------------------------------- 26
Different Sorting Algorithms-------------------------------------------------------------------------------------------------------------------- 27
Comparing Tow Sorting Algorithms ----------------------------------------------------------------------------------------------------------- 29
How does Bubble Sort Work? ------------------------------------------------------------------------------------------------------------------ 32
Implementation of Bubble Sort ---------------------------------------------------------------------------------------------------------------- 36
Time Complexity ---------------------------------------------------------------------------------------------------------------------------------- 38

Selection Sort Algorithm: ----------------------------------------------------------------------------------------------------------------- 39
3.1
3.2
3.3

How does Selection Sort Algorithm work?--------------------------------------------------------------------------------------------------- 39
Implementation of Bubble Sort ---------------------------------------------------------------------------------------------------------------- 44
Time complexity ---------------------------------------------------------------------------------------------------------------------------------- 45

D1 ANALYSE THE OPERATION, USING ILLUSTRATIONS, OF TWO NETWORK SHORTEST PATH ALGORITHMS, PROVIDING AN EXAMPLE OF EACH. -------- 46
1.
2.

Explanation ---------------------------------------------------------------------------------------------------------------------------------- 46
Dijkstra Algorithm -------------------------------------------------------------------------------------------------------------------------- 47
2.1
2.2
2.3


3.

How does Dijkstra’s Algorithm works? ------------------------------------------------------------------------------------------------------- 47
Implementation of Dijkstra’s Algorithm ------------------------------------------------------------------------------------------------------ 54
Time Complexity ---------------------------------------------------------------------------------------------------------------------------------- 57

Bellman-Ford Algorithm------------------------------------------------------------------------------------------------------------------- 57
3.1
3.2
3.3

How does Bellman-Ford’s Algorithm works? ------------------------------------------------------------------------------------------------ 58
Implementation of Bellman-Ford’s Algorithm ---------------------------------------------------------------------------------------------- 65
Time Complexity ---------------------------------------------------------------------------------------------------------------------------------- 70

P3 USING AN IMPERATIVE DEFINITION, SPECIFY THE ABSTRACT DATA TYPE FOR A SOFTWARE STACK. -------------------------------------------------- 71

3|P ag e


1.
2.

Introduction to Formal Specification and Types of Languages ------------------------------------------------------------------- 71
Definitions of Pre-condition, Post-condition, and Error-condition --------------------------------------------------------------- 71
2.1
2.2
2.3

3.


Pre-condition: ------------------------------------------------------------------------------------------------------------------------------------- 72
Post-condition: ------------------------------------------------------------------------------------------------------------------------------------ 72
Error-condition: ----------------------------------------------------------------------------------------------------------------------------------- 72

Specification of Stack Operations ------------------------------------------------------------------------------------------------------- 72
3.1
3.2
3.3
3.4
3.5

Operation: push(item) --------------------------------------------------------------------------------------------------------------------------- 72
Operation: pop() ---------------------------------------------------------------------------------------------------------------------------------- 73
Operation: peek() --------------------------------------------------------------------------------------------------------------------------------- 74
Operation: isEmpty() ----------------------------------------------------------------------------------------------------------------------------- 75
Operation: size() ---------------------------------------------------------------------------------------------------------------------------------- 75

4. Implementation example Stack in software ------------------------------------------------------------------------------------------ 76
M3 EXAMINE THE ADVANTAGES OF ENCAPSULATION AND INFORMATION HIDING WHEN USING AN ADT. --------------------------------------------- 79
1.

Understanding Encapsulation and Information Hiding ---------------------------------------------------------------------------- 79
1.1
1.2

Encapsulation-------------------------------------------------------------------------------------------------------------------------------------- 79
Information hiding-------------------------------------------------------------------------------------------------------------------------------- 80

2. Application of Read-only and Write-only Properties -------------------------------------------------------------------------------- 81

3. Advantages of Encapsulation and Information Hiding in an ADT ---------------------------------------------------------------- 81
4. Encapsulation Using Accessors and Mutators---------------------------------------------------------------------------------------- 81
5. Encapsulation Using Properties --------------------------------------------------------------------------------------------------------- 82
D2 DISCUSS THE VIEW THAT IMPERATIVE ADTS ARE A BASIS FOR OBJECT ORIENTATION AND, WITH JUSTIFICATION, STATE WHETHER YOU AGREE.--- 84
1.
2.
3.
4.
5.

Imperative ADTs and Object Orientation ---------------------------------------------------------------------------------------------- 84
Integrating Imperative ADTs into an Object-Oriented Paradigm ---------------------------------------------------------------- 84
Comparing and Contrasting Traditional Imperative ADTs with OOP-based ADTs -------------------------------------------- 85
Discussion on Imperative ADTs as a Basis for Object Orientation---------------------------------------------------------------- 85
Example to prove the viewpoint --------------------------------------------------------------------------------------------------------- 87
5.1
5.2

Imperative ADT - Stack Functions:------------------------------------------------------------------------------------------------------------- 87
OOP-based ADT - Stack Class: ------------------------------------------------------------------------------------------------------------------ 88

III.

EVALUATION --------------------------------------------------------------------------------------------------------------------------------------- 89

IV.

CONCLUSION ----------------------------------------------------------------------------------------------------------------------------------- 91

V.


REFERENCES ---------------------------------------------------------------------------------------------------------------------------------------- 93

4|P ag e


Table of Figuares
FIGURE 1: ABSTRACT DATA TYPE. --------------------------------------------------------------------------------------------------------------------------- 10
FIGURE 2: STACK ADT --------------------------------------------------------------------------------------------------------------------------------------- 11
FIGURE 3: STACK OPERATIONS ------------------------------------------------------------------------------------------------------------------------------- 13
FIGURE 4: IMPLEMENTATION OF A STACK ADT (1) --------------------------------------------------------------------------------------------------------- 14
FIGURE 5: IMPLEMENTATION OF A STACK ADT (2) --------------------------------------------------------------------------------------------------------- 15
FIGURE 6: IMPLEMENTATION OF A STACK ADT (3) --------------------------------------------------------------------------------------------------------- 16
FIGURE 7: EXAMPLE MEMORY STACK FOR FUNCTION CALLS ----------------------------------------------------------------------------------------------- 21
FIGURE 8: QUEUE DATA STRUCTURE (GEEKSFORGEEKS, 2023) -------------------------------------------------------------------------------------------- 22
FIGURE 9: FIFO PROPERTY IN QUEUE (GEEKSFORGEEKS, 2023) -------------------------------------------------------------------------------------------- 23
FIGURE 10: FIFO QUEUE USING JAVA (1) ------------------------------------------------------------------------------------------------------------------- 24
FIGURE 11: FIFO QUEUE USING JAVA (2) ------------------------------------------------------------------------------------------------------------------- 25
FIGURE 12: BUBBLE SORT FLOW CHART --------------------------------------------------------------------------------------------------------------------- 33
FIGURE 13: BUBBLE SORT ALGORITHM : PLACING THE LARGEST ELEMENT AT CORRECT POSITION (GEEKSFORGEEKS, 2023) ----------------------------- 34
FIGURE 14: BUBBLE SORT ALGORITHM : PLACING THE SECOND LARGEST ELEMENT AT CORRECT POSITION (GEEKSFORGEEKS, 2023) -------------------- 35
FIGURE 15: BUBBLE SORT ALGORITHM : PLACING THE REMAINING ELEMENTS AT THEIR CORRECT POSITIONS (GEEKSFORGEEKS, 2023)----------------- 35
FIGURE 16: IMPLEMENTATION OF BUBBLE SORT (1) -------------------------------------------------------------------------------------------------------- 37
FIGURE 17: IMPLEMENTATION OF BUBBLE SORT (2) -------------------------------------------------------------------------------------------------------- 38
FIGURE 18: SELECTION SORT FLOW CHART ------------------------------------------------------------------------------------------------------------------ 40
FIGURE 19: SELECTION SORT ALGORITHM | SWAPPING 1ST ELEMENT WITH THE MINIMUM IN ARRAY (GEEKSFORGEEKS, 2023) ------------------------ 41
FIGURE 20: SELECTION SORT ALGORITHM | SWAPPING I=1 WITH THE NEXT MINIMUM ELEMENT (GEEKSFORGEEKS, 2023) ----------------------------- 42
FIGURE 21: SELECTION SORT ALGORITHM | SWAPPING I=2 WITH THE NEXT MINIMUM ELEMENT (GEEKSFORGEEKS, 2023) ----------------------------- 42
FIGURE 22: SELECTION SORT ALGORITHM | SWAPPING I=3 WITH THE NEXT MINIMUM ELEMENT (GEEKSFORGEEKS, 2023) ----------------------------- 43
FIGURE 23: SELECTION SORT ALGORITHM | REQUIRED SORTED ARRAY (GEEKSFORGEEKS, 2023) -------------------------------------------------------- 43

FIGURE 24: IMPLEMENTATION OF SELECTION SORT (1)----------------------------------------------------------------------------------------------------- 44
FIGURE 25: IMPLEMENTATION OF SELECTION SORT (2)----------------------------------------------------------------------------------------------------- 45
FIGURE 26: EXAMPLE SHORTEST PATH ALGORITHM --------------------------------------------------------------------------------------------------------- 46
FIGURE 27: DIJKSTRA’S ALGORITHM FLOW CHART --------------------------------------------------------------------------------------------------------- 48
FIGURE 28: DIJKSTRA’S ALGORITHM------------------------------------------------------------------------------------------------------------------------- 49
FIGURE 29: DIJKSTRA’S ALGORITHM STEP 1 ----------------------------------------------------------------------------------------------------------------- 50
FIGURE 30: DIJKSTRA’S ALGORITHM STEP 2 ----------------------------------------------------------------------------------------------------------------- 51
FIGURE 31: DIJKSTRA’S ALGORITHM STEP 3 ----------------------------------------------------------------------------------------------------------------- 52
FIGURE 32: DIJKSTRA’S ALGORITHM STEP 4 ----------------------------------------------------------------------------------------------------------------- 53
FIGURE 33: DIJKSTRA’S ALGORITHM STEP 5 ----------------------------------------------------------------------------------------------------------------- 54
FIGURE 34: IMPLEMENTATION OF DIJKSTRA’S ALGORITHM (1) --------------------------------------------------------------------------------------------- 55
FIGURE 35: IMPLEMENTATION OF DIJKSTRA’S ALGORITHM (2) --------------------------------------------------------------------------------------------- 56
FIGURE 36: IMPLEMENTATION OF DIJKSTRA’S ALGORITHM (3) --------------------------------------------------------------------------------------------- 56
FIGURE 37: INITIAL GRAPH (GEEKSFORGEEKS, 2023). ------------------------------------------------------------------------------------------------------ 58
FIGURE 38: INITIALIZE A DISTANCE ARRAY (GEEKSFORGEEKS, 2023). -------------------------------------------------------------------------------------- 59
FIGURE 39: 1ST RELAXATION (GEEKSFORGEEKS, 2023). --------------------------------------------------------------------------------------------------- 60
FIGURE 40: 2ND RELAXATION (GEEKSFORGEEKS, 2023). --------------------------------------------------------------------------------------------------- 61
FIGURE 41: 3RD RELAXATION (GEEKSFORGEEKS, 2023). --------------------------------------------------------------------------------------------------- 62

5|P ag e


FIGURE 42: 4TH RELAXATION (GEEKSFORGEEKS, 2023). --------------------------------------------------------------------------------------------------- 63
FIGURE 43: 5TH RELAXATION (GEEKSFORGEEKS, 2023). --------------------------------------------------------------------------------------------------- 64
FIGURE 44: 6TH RELAXATION (GEEKSFORGEEKS, 2023). --------------------------------------------------------------------------------------------------- 65
FIGURE 45: IMPLEMENTATION OF BELLMAN-FORD’S ALGORITHM (1) ------------------------------------------------------------------------------------- 66
FIGURE 46: IMPLEMENTATION OF BELLMAN-FORD’S ALGORITHM (2) ------------------------------------------------------------------------------------- 67
FIGURE 47: IMPLEMENTATION OF BELLMAN-FORD’S ALGORITHM (3) ------------------------------------------------------------------------------------- 68
FIGURE 48: IMPLEMENTATION OF BELLMAN-FORD’S ALGORITHM (4) ------------------------------------------------------------------------------------- 69
FIGURE 49: OPERATION: PUSH(ITEM) ----------------------------------------------------------------------------------------------------------------------- 73

FIGURE 50: OPERATION: POP() ------------------------------------------------------------------------------------------------------------------------------ 74
FIGURE 51: OPERATION: PEEK() ----------------------------------------------------------------------------------------------------------------------------- 74
FIGURE 52: OPERATION: ISEMPTY() ------------------------------------------------------------------------------------------------------------------------- 75
FIGURE 53: OPERATION: SIZE() ------------------------------------------------------------------------------------------------------------------------------ 76
FIGURE 54: UNDO/REDO FUNCTIONALITY IN A TEXT EDITOR ------------------------------------------------------------------------------------------------ 77
FIGURE 55: ENCAPSULATION USING ACCESSORS AND MUTATORS ----------------------------------------------------------------------------------------- 82
FIGURE 56: ENCAPSULATION USING PROPERTIES ----------------------------------------------------------------------------------------------------------- 83
FIGURE 57: IMPERATIVE ADT ------------------------------------------------------------------------------------------------------------------------------- 87
FIGURE 58: IMPERATIVE OOP ------------------------------------------------------------------------------------------------------------------------------- 88

6|P ag e


I.

Introduction

In the ever-evolving landscape of software development, innovation and efficiency are key drivers of
success. As an in-house software developer at Softnet Development Ltd, a renowned provider of network
provisioning solutions, I find myself immersed in a compelling scenario. Our company is an integral part of
a collaborative service provisioning development project, and I have proudly secured the contract to
design and develop a middleware solution that will serve as the crucial interface connecting multiple
computer provisioning interfaces, including SOAP, HTTP, JML, and CLI, with the telecom provisioning
network at the back end via CLI.
Within this challenging context, my account manager has entrusted me with a special role: to guide and
inform our team on the intricacies of designing and implementing abstract data types (ADTs). This
assignment calls for the creation of a presentation that will be shared with our esteemed collaborating
partners, shedding light on how ADTs can be harnessed to enhance every aspect of software development,
from design to testing.
Moreover, I have been tasked with crafting an introductory report, which will be distributed to all project

partners. This report aims to provide a comprehensive understanding of how to specify abstract data types
and algorithms using a formal notation. It will delve into the core principles of ADTs and their potential to
elevate software engineering practices.
The primary objectives of this report are as follows:
1. Explanation on how to specify an abstract data type using the example of a software stack:
In this report, I will elucidate the process of defining and specifying an abstract data type, employing the
practical example of a software stack. This will serve as a foundation for comprehending the broader
concepts related to ADTs.
2. Explanation of the advantages of encapsulation and information hiding when using an ADT:
One of the core principles of ADTs is encapsulation, which promotes the concealment of implementation
details. I will explore the significance of encapsulation and information hiding in the context of ADTs,
highlighting the benefits they bring to software development.
7|P ag e


3. Discussion of imperative ADTs with regard to object orientation:
As software development embraces object-oriented paradigms, it is crucial to assess how imperative
ADTs fit into this landscape. This report will engage in a thoughtful discussion, offering insights into the
relationship between imperative ADTs and object orientation.
Through this report, I aim to provide a comprehensive foundation for understanding and harnessing the
power of abstract data types in the realm of software development. Together, I will embark on a journey
to explore the intricacies of ADTs and their transformative potential in shaping the future of software
engineering.

8|P ag e


II.

Body


LO1 Examine abstract data types, concrete data structures and algorithms
P1 Create a design specification for data structures explaining the valid

operations that can be carried out on the structures
1. Data Structures
A data structure is a technique of organizing the data so that the data can be utilized efficiently. There are
two ways of viewing the data structure:
Mathematical/ Logical/ Abstract models/ Views: The data structure is the way of organizing the
data that requires some protocols or rules. These rules need to be modeled that come under the
logical/abstract model.
Implementation: The second part is the implementation part. The rules must be implemented
using some programming language.

2. Abstract data type
An abstract data type is an abstraction of a data structure that provides only the interface to which the
data structure must adhere. The interface does not give any specific details about something should be
implemented or in what programming language.
In other words, we can say that abstract data types are the entities that are definitions of data and
operations but do not have implementation details. In this case, we know the data that we are storing and
the operations that can be performed on the data, but we don't know about the implementation details.
The reason for not having implementation details is that every programming language has a different
implementation strategy for example; a C data structure is implemented using structures while a C++ data
structure is implemented using objects and classes.

9|P ag e


For example, a List is an abstract data type that is implemented using a dynamic array and linked list. A
queue is implemented using linked list-based queue, array-based queue, and stack-based queue. A Map is

implemented using Tree map, hash map, or hash table (Abstract data type in data structure - javatpoint).
The process of providing only the essentials and hiding the details is known as abstraction.

Figure 1: Abstract Data Type.

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 (geeksforgeeks, 2023).
To further elucidate the concept of an Abstract Data Type (ADT), we will employ the example of a software
stack. A stack is a prime illustration of an ADT, showcasing its essential characteristics.

10 | P a g e


3. Stack ADT
To underscore the definition of an Abstract Data Type (ADT), we'll delve into the example of a software
stack. A stack serves as an exemplary manifestation of the ADT concept.
As previously outlined, an ADT is an abstract programming notion. It describes a set of data and operations
on that data without specifically revealing the underlying structure or algorithms. In the context of a
software stack, this ADT adheres to the Last-In-First-Out (LIFO) principle.
LIFO( Last In First Out ):
This strategy states that the element that is inserted last will come out first. You can take a pile of plates
kept on top of each other as a real-life example. The plate which we put last is on the top and since we
remove the plate that is at the top, we can say that the plate that was put last comes out first (Introduction
to stack - data structure and algorithm tutorials, 2023).

Figure 2: Stack ADT


By examining the stack ADT, we can emphasize the core concept of ADTs. It demonstrates how ADTs
provide a clear interface and define the permissible operations on the data structure, abstracting away
the underlying complexities of implementation.

11 | P a g e


This example of a software stack serves as a concrete manifestation of the ADT concept, highlighting the
power of abstraction in software development.

3.1

Stack operations and working mechanism

In order to make manipulations in a stack, there are certain operations provided to us.


push() to insert an element into the stack



pop() to remove an element from the stack



top() Returns the top element of the stack.



isEmpty() returns true if stack is empty else false.




size() returns the size of stack.

12 | P a g e


Figure 3: Stack operations

13 | P a g e


3.2

Implementation of a Stack ADT in Code

To demonstrate the stack's nature as an ADT, let’s consider a basic implementation in Java:

Figure 4: Implementation of a Stack ADT (1)

14 | P a g e


Figure 5: Implementation of a Stack ADT (2)

15 | P a g e


Figure 6: Implementation of a Stack ADT (3)


This implementation defines a generic Stack class that can work with any data type. It includes methods
for pushing, popping, peeking at the top element, checking if the stack is empty or full, and displaying
the contents of the stack. The example usage at the end demonstrates how to use this Stack ADT in
Java.

16 | P a g e


3.3

Applications/Examples of Stack

Stacks are fundamental data structures that find application in various real-world scenarios. Here are
some examples of how stacks are used in practical situations:
Function Call Management: Stacks are widely used in programming languages to manage function
calls. When a function is called, its local variables and parameters are stored in a stack frame. As
functions return, their stack frames are popped from the stack.
Undo Functionality in Software: In many software applications, such as text editors or graphic design
tools, stacks are used to implement undo and redo functionality. Each state of the application is pushed
onto a stack, allowing users to go back to previous states.
Expression Evaluation: Stacks are utilized in parsing and evaluating mathematical expressions, such as
arithmetic expressions or expressions in programming languages. They help maintain the correct order
of operations.
Browser History: Web browsers use stacks to keep track of the user's navigation history. Each visited
page is pushed onto the stack, and the back and forward buttons pop pages from the stack.
Backtracking Algorithms: Algorithms that involve backtracking, like depth-first search (DFS) and
recursive algorithms, use stacks to keep track of the state of the search and return to previous states.
Call Stack in Multithreading: In multithreaded applications, each thread has its own call stack. Stacks
are used to manage the execution context of each thread, including function calls and local variables.

Balancing Parentheses: Stacks are employed to check for balanced parentheses in expressions. As
open and close brackets are encountered, they are pushed and popped from the stack. If the stack is
empty at the end, the expression is balanced.
Task Scheduling: Stacks can be used in task scheduling algorithms to keep track of tasks that need to
be executed. The last task added is the first to be executed (Last-In-First-Out, or LIFO).

17 | P a g e


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

implement function calls in a computer
In Part P2, I will delve into the operations of a memory stack and explore how it plays a vital role in the
execution of function calls within a computer system.

1. Memory Stack
A stack is a special area of computer’s memory which stores temporary variables created by a function. In
stack, variables are declared, stored and initialized during runtime.
It is a temporary storage memory. When the computing task is complete, the memory of the variable will
be automatically erased. The stack section mostly contains methods, local variable, and reference
variables (Matthew Martin & Martin, 2023).

2. Operations of a Memory Stack


Push Operation:

This operation is used to insert a new data item into the top of the Stack. The new item can be inserted
as follows:SP ←SP-1
M[SP]← DR


In the first step, the Stack Pointer is decremented to point at the address where the data item will be
stored.
Then, by using the memory write operation, the data item from Data Register gets inserted into the
top of the stack ( at the address where the Stack Pointer is pointing) (Memory Stack Organization in
computer architecture, 2023).

18 | P a g e




Pop Operation:

This operation is used to delete a data item from the top of the Stack. Data item can be deleted as
follows:DR←M[SP]
SP←SP+1

In the first step, the top data item is read from the Stack into the Data Register. The Stack Pointer is
then incremented to point at the next data item in the stack. Push or Pop operations can be performed
with the help of the following microoperations:
Access to memory with the help of Stack Pointer (SP), and
Updating the stack.
It totally depends upon the organization of the stack whether the Stack Pointer (SP) is updated by
incrementing or decrementing the address values.
In this case, the Stack Pointer grows by decreasing the memory address. The Stack may be made in a
way that the Stack Pointer grows by increasing the memory also (Memory Stack Organization in
computer architecture, 2023).




Peek (Top) Operation

Peek, also known as the top operation, allows us to examine the value or data stored at the top of the
stack without removing it. This operation is essential for inspecting the current state of the stack.

19 | P a g e


3. Using the Memory Stack for Function Calls
The memory stack plays a pivotal role in managing function calls in a computer's execution environment.
Here is a step-by-step explanation of how it is used for this purpose:


Function Call: When a function is called, a new activation record (often referred to as a stack frame)
is created and pushed onto the stack. This record contains information about the function's
parameters, local variables, and the return address.



Execution: The function begins its execution with access to its own activation record. It can access
and modify its local variables within this frame.



Nested Calls: If a function makes further nested function calls, new activation records are created
and pushed onto the stack, forming a stack of stack frames. This allows for recursive and nested
function calls.




Return: When a function completes its execution or encounters a return statement, it pops its
activation record from the stack, transferring control back to the calling function. The return value
may also be passed back.



Stack Maintenance: The stack is continuously maintained as functions are called and return. The
top-of-stack pointer is updated accordingly.

The memory stack ensures that the execution context of each function is preserved and that functions can
return to their respective callers seamlessly.
Illustrative Example:

20 | P a g e


Figure 7: Example Memory Stack for Function Calls

In this Java example:
The main function is the entry point of the program.
When the main function is called, it pushes its activation record onto the memory stack, including the
return address for when it completes execution.
The add function is called from within the main function.
When the add function is called, its activation record is pushed onto the stack, and it calculates the sum of
a and b .

After completing its execution, the add function pops its activation record from the stack, and control
returns to the main function.
Finally, the main function completes its execution, and its activation record is popped from the stack.

This example demonstrates how the memory stack is used to manage the execution context of functions,
allowing them to be called and return control to their respective callers.

21 | P a g e


M1 Illustrate, with an example, a concrete data structure for a First In First

out (FIFO) queue.
1. Queue Data Structure
A Queue is defined as a linear data structure that is open at both ends and the operations are performed
in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one end, and all deletions from
the list are made at the other end. The element which is first pushed into the order, the operation is first
performed on that (geeksforgeeks, 2023).

Figure 8: Queue Data Structure (geeksforgeeks, 2023)

2. FIFO Principle of Queue
A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served.
(i.e. First come first serve).
Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the
queue, is called the front of the queue(sometimes, head of the queue), similarly, the position of the last
22 | P a g e


entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the queue. See
the below figure (geeksforgeeks, 2023).

Figure 9: Fifo Property in Queue (geeksforgeeks, 2023)


3. Implementation of FIFO Queue
Provide a concrete example to demonstrate how the FIFO queue works and how it is used. This example
should include adding elements to the queue, removing elements from the queue, and accessing the first
element.
Here is an example of how you can implement and illustrate a FIFO queue using Java:

23 | P a g e


Figure 10: FIFO queue using Java (1)

24 | P a g e


Figure 11: FIFO queue using Java (2)

In this Java example, we've implemented a FIFOQueue class using a linked list (LinkedList), which is a
common choice for implementing a queue in Java. The methods enqueue, dequeue, isEmpty, size, and
peek are used to interact with the FIFO queue.

25 | P a g e


×