1649 - Assignment 1 - NMT - GCH17392
1649 - Assignment 1 - NMT - GCH17392
Unit number and title Unit 19: Data Structures and Algorithms
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
Summative Feedback: Resubmission Feedback:
IV Signature:
Contents
Introduction .................................................................................................................................................... 4
Part I ............................................................................................................................................................... 4
1. Abstract Data Type (ADT) ..................................................................................................................... 4
2. Stack ADT .............................................................................................................................................. 4
3. Stack Memory......................................................................................................................................... 6
4. Queue ADT ............................................................................................................................................. 9
Part II ............................................................................................................................................................ 12
1. Explanation on how to specify an abstract data type using the example of software stack ................. 12
References .................................................................................................................................................... 13
List of figures
Figure 1: ADT ................................................................................................................................................ 4
Figure 2: Push & Pop in Stack ....................................................................................................................... 5
Figure 3: Stack Operations ............................................................................................................................. 6
Figure 4: Recursion Code ............................................................................................................................... 9
Figure 5: Queue ADT ..................................................................................................................................... 9
Figure 6: Operations of Queue ..................................................................................................................... 11
Introduction
A data structure is a group of data elements that provide an efficient way of storing and organizing data in
a computer so that this data can be best used. Some examples of data structures include arrays, linked lists,
stacks, and queues. This concept is widely used in almost all aspects of computer science, including
operating systems, compiler design, artificial intelligence, and several others. In this report, I will present
stacks, queues, and how to represent the abstract data form using the software stack as an example.
Part I
1. Abstract Data Type (ADT)
Abstract Data type (ADT) is a type (or class) for objects whose behaviour 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. (GeeksforGeeks, 2019)
Figure 1: ADT
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.
2. Stack ADT
Stack is a linear data structure in which the insertion and deletion operations are performed at only one
end. In a stack, adding and removing of elements are performed at a single position which is known as
"top". That means, a new element is added at top of the stack and an element is removed from the top of
the stack. In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out)
principle. (BTech Smart Class, 2019)
push() and Pop() are two basic operations of a stack. Stack initialization, use, and destruction are all
examples of stack operations. In addition to these basic pieces, a stack is used for these two main
processes.
To cater to the proper use of the stack, we must further assert its state. So the following processes are
applied to Stacks.
peek() returns the stack's top data piece without deleting it.
isFull() determines whether the stack is full.
isEmpty() to check if the stack is empty.
Figure 3: Stack Operations
Applications of Stack
Decimal to Binary Conversion: When we convert a decimal number to binary, we divide this
decimal number by 2 and write the remainder in the reverse order in which it was produced. Using
the stack, we save the remainder after each division in turn and after the division operation is
finished, reading the last stack will give the binary representation we are looking for.
Calculate the value of a suffix algebraic expression. Evaluating Expressions: The stack is used to
evaluate prefix, suffix, and prefix expressions.
3. Stack Memory
Stack memory is a memory usage mechanism that allows the system memory to be used as temporary data
storage that behaves as a first-in-last-out buffer. One of the essential elements of stack memory operation
is a register called the Stack Pointer. The stack pointer indicates where the current stack memory location
is, and is adjusted automatically each time a stack operation is carried out. (Yiu, 2015)
Recursion in java is a process where a method calls itself repeatedly. A method in java that calls itself is
called a recursive method. It makes the code compact but complex to understand. When a function is
called, the values as real arguments must be initialized. This leads to the system being able to restart the
program's execution after its execution is complete. To avoid a recursive call, certain requirements must
be met in the system. Otherwise, the procedure will be considered infinite. We use the if ... else
declaration to pause the recursive call in the process.
We can see a rule that n! = (n-1)!*n, which (n-1)! = (n – 2)!*n-1, and so on, we will generalize this
problem by recursion.
The factorial() method is calling itself. Initially, the value of n is 4 inside factorial(). On the next recursive
call, the factorial() method will be called with the value 3. This process continues until n is 0.
Figure 4: Recursion Code
4. Queue ADT
The queue is a linear data structure in which the insertion and deletion operations are performed at two
different ends. In a queue data structure, adding and removing elements are performed at two different
positions. The insertion is performed at one end and deletion is performed at another end. In a queue data
structure, the insertion operation is performed at a position which is known as 'rear' and the deletion
operation is performed at a position which is known as 'front'. In the queue data structure, the insertion and
deletion operations are performed based on FIFO (First In First Out) principle. (BTech Smart Class, 2020)
Queue features include creating or defining a queue, using it, and completely removing it from memory.
We will cover the basic operations used in the queue:
Manage requests on a single shared resource like CPU scheduling and disk scheduling.
Real-time system or hardware interrupt handling.
Handling website traffic.
Routers and switches in the network.
Maintain playlists in the media player.
Part II
1. Explanation on how to specify an abstract data type using the example of software stack
The functions inside the building are obscured when performing. Data types can be created according to a
uniform principle. Other functions that use the structure, or client code, can use the interface provided by
functions already implemented on the structure. Each variable command and function has its own
implementation. Particularly for structs that do not use an object-oriented implementation, there is another
method. This approach has advantages such as ignoring algorithmic complexity, allowing to swap
different implementations of a struct without changing the actions observed by client code in the main
interface. A unit or package containing an implementation that can be independently modified and built in
several compiled languages without the need to recompile the client code. The rebuild only happens when
the interface has been changed.
Stack ADT
References
BTech Smart Class. (2019). Retrieved from http://www.btechsmartclass.com/data_structures/stack-adt.html