









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of abstract data types (ADTs), focusing on stacks and queues. It introduces ADTs, highlighting their importance in programming by allowing the definition of data types without specifying their implementation. The document then delves into the specifics of stacks and queues, explaining their characteristics, operations, and applications. It covers the LIFO and FIFO principles, implementation using arrays or linked lists, and common use cases. The document also includes a formal specification of a stack ADT, demonstrating the use of pre-conditions, post-conditions, and error conditions to precisely define the behavior of stack operations.
Typology: Assignments
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 19: Data Structures and Algorithms Submission date Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Thai Van Chien Student ID GCH Class GCH1103 Assessor name Do Hong Quan 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 Chien Grading grid P1 P2 P3 M1 M2 M3 D1 D
Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:
Abstract Data Types (ADT) are an important data type for programming, especially for beginners. They provide a way to define data types without specifying how they are implemented. This makes them more flexible and reusable, and can help improve code readability and maintenance. Two common ADTs are stacks and queues. The stack is a LIFO (in, in, first out) data structure, while the queue is a FIFO (first in, first out) data structure. Both are widely used in programming and have many applications in system and software development. In this assignment, I will introduce ADTs and discuss their importance in programming and will also cover ADT stacks and queues, and show how they can be implemented in different programming languages.
An abstract data type (ADT) is a mathematical model for data types. It is determined by its (semantics) behavior from the user's point of view, of the data, namely about possible values, possible operations on data of this type, and the behavior of these manipulations. This mathematical model, in contrast to a data structure, is a specific representation of the data and is the perspective of the implementer, not the user (Drozdek, 2010). Picture 1 : Abstract data type Source: https://www.geeksforgeeks.org/abstract-data-types/
A stack is a data structure that follows the Import-After First-Out (LIFO) principle. This means that the element added to the stack will eventually be the first to be deleted. A stack is a data structure that can be implemented by using arrays, pointers, or linked lists. In this assignment, you'll deploy the stack using the LinkedList. LinkedList is a data structure consisting of a series of cells, each containing data and a pointer to the next cell. The variable E will be used to represent the data type of the elements stored in the stack. Methods Details public E push( E item) Pushes an item onto the top of this stack. Parameters : item - the item to be pushed onto this stack. Returns : the item argument. public E pop() Removes the object at the top of this stack and returns that object as the value of this function. Returns : The object at the top of this stack (the last item of the Vector object). Throws : EmptyStackException - if this stack is empty. public E peek() Looks at the object at the top of this stack without removing it from the stack. Returns : the object at the top of this stack (the last item of the Vector object). Throws : EmptyStackException - if this stack is empty. public boolean empty() Tests if this stack is empty. Returns : true if and only if this stack contains no items; false otherwise. public int search(Object o) Returns the 1-based position where an object is on this stack. If the object o occurs as an item in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1. The equals method is used to compare o to the items in this stack. Parameters : o - the desired object.
Returns : the 1-based position from the top of the stack where the object is located; the return value -1 indicates that the object is not on the stack. Stack operation: push() When I perform a push() operation on a stack, I place a new element at the top of the stack. This means that I put the new element on top of the elements that already exist in the stack. When pushing a new Node into a stack, the new Node will point to the current peak in the stack, and then the new Node will be the new top of the stack. If the stack is empty, the new Node will be the top of the stack. Picture 3 : push() Stack operation: pop() When I call pop() on a stack, I'm doing the removal of the element on top of that stack. The stack is designed according to the principle of 'last in, first out' (LIFO), meaning that the last element placed in the stack will be removed first. First, I define the new element that I want to add to the stack. Then I find my way to the top of the stack, where the existing elements are stored. Next, I placed the new element in that vertex position and adjusted the vertex pointer to point to the new element. After pointing the address of the underlying Node to null, the old top of the stack is deleted. Finally, the top of the new stack will be the Node below. If the stack has only 1 Node left, then after removal, the stack will be empty. Picture 4 : pop()
When a function is called, the stack is used to store the following information: The return address: This is the address of the command that called the function. The function’s arguments: The arguments passed to the function. The function’s local variables: These are the variables declared inside the function. When the function returns, the stack is used to recover the following information: The return address: This address is used to return a command called a function. The function’s arguments: The arguments are no longer needed, so they are discarded. The function’s local variables: Local variables are no longer needed so they are removed. (Drozdek, 2010) Example of the function returns: Picture 6 : Example of Stack Memory(Code) Picture 7 : Example of Stack Memory(Function Calls) When calling the factorial(4) function, the program calculates the factorial of the number 4. First, the program calls back the factorial function with an argument of 3 to calculate the factorial of the number 3. Next, the program calls back the factorial function with an argument of 2 to calculate the factorial of the number 2. Finally, the program calls back the factorial function with an argument of 1 to calculate the factorial of the number 1. When calling the factorial(1) function, the program returns a value of 1. Based
on these results, we calculate the factorial of number 2, number 3 and finally number 4. The final result of factorial(4) was 24. IV. Queue ADT
A queue is a linear data structure in which elements are added behind and removed from the front, creating a queue. Unlike stacks, queues use both ends: one end to add elements and one end to remove them. This means that the last element added to the queue will wait until all previous elements are deleted. A queue follows the principle of 'first in, first out' (FIFO), where the element that has been in the queue the longest is deleted first (Drozdek, 2010).
A queue is a data structure that follows the First-In, First-Out (FIFO) principle. This means that the element added to the queue will be the first to be deleted. A queue can be executed using arrays, cursors, or linked lists. In this assignment, I'll use Linked List to deploy the queue. Variable E will represent the data type of the elements stored in the queue. Methods Details public E dequeue() Removes and returns the item on this queue that was least recently added. Returns : the item on this queue that was least recently added * Throws: NoSuchElementException if this queue is empty public void enqueue( E item) Add the item to the queue. Parameter s: item the item to enqueue public E peek() Returns the item least recently added to this queue. Returns: the item least recently added to this queue Throws: NoSuchElementException if this queue is empty public int size() Returns the number of items in this queue. Returns: the number of items in this queue public boolean isEmpty() Returns true if this queue is empty.
Network connection: In networking, a queue can be used to manage data packets. For example, a router may have a queue of packets waiting to be sent on a specific network interface. This ensures that packages are not lost or delayed and that they are sent in the correct order. Media player: In the media player, queues are used to manage the playback of songs. Songs are added to the queue when the user wants to listen to them, and they are removed from the queue when the user finishes listening. This ensures that users can listen to the songs in the order that they want to listen to.
A formal specification is an accurate and unambiguous representation of the behavior and properties of a system. It involves translating non-mathematical descriptions, such as diagrams, tables, or English text, into a formal specification language. The goal of a formal specification is to describe the external behavior of the system without constraining or specifying implementation details. A formal specification provides a brief description of the system's high-level behavior, including its inputs, outputs, and interactions with the environment. It also specifies the properties and constraints that the system must satisfy. To support formal interpretation of the specification, a formal specification language has well-defined linguistic semantics. This allows logical inference and analysis of the specification using logical theories such as first-order predicate calculus, quadratic predicate calculus, or temporal logic. These logical theories provide the means to infer about its specification, properties, and the programs that execute it (Back, 2003). Picture 9 : Formal specification
Types of formal specification languages: Axiomatic Specificationsuse mathematical axioms to describe the behavior of software systems. Axioms are statements that are assumed to be true and they can be used to prove other statements about the system. The axiomatic specification is usually written in a formal language, which is a language with the correct syntax and semantics. This allows the specification to become clear and for it to be analyzed mathematically. VDM (Workable Design Methodology) is a formal method that uses a combination of mathematical axioms and English-like descriptions to describe the behavior of software systems. VDM is based on set theory and predicate logic, and it is used to describe the structure and behavior of systems. VDM specifications are usually written in the official language, but they can also be written in English.
a) Pre-condition are conditions that must be true before the function can be called. If a prerequisite is not met, the function will not function correctly. For example, a prerequisite for a function calculating the square root of a number is that the number must be greater than or equal to 0. If the number is less than 0, the function will not work correctly and may fail. b) Post-conditions are those that are guaranteed to be true after a function has been called. For example, the following condition for a function calculating the square root of a number is that the return value will be a number greater than or equal to 0. If the return value is less than 0, the function does not work correctly. c) Error-condition are conditions that may occur during the execution of a function that prevent that function from completing its task. For example, an error condition may occur if the function is passed an invalid argument. When an error occurs, the function may return an error code or throw an exception.
Specification Stack: Data : stack: Stack s s.size() n (n >= 0)
Abstract data types (ADTs) serve as essential tools in programming, providing the means to define data types without regard to their underlying implementation.
Drozdek, A. (2012). Data Structures and Algorithms in Java. International Thomson Business Press. Martin, M., 2022. Stack vs Heap: Know the Difference. [Online] Available at: https://www.guru99.com/stack-vs- heap.html Back, Richard J. "Introduction to formal methods." Cambridge University Press, 2003. Algorithms 10: Abstract data types – Stack. (n.d.). Available at: https://learn1.open.ac.uk/mod/oublog/viewpost.php?post=