Download Data Structures and Algorithms: Understanding Abstract Data Types, Queues, and Stacks and more High school final essays Computer science in PDF only on Docsity!
ASSIGNMENT 1 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit^ 19:^ Data^ Structures^ and^ Algorithms
Submission date 26/06/2022 Date Received 1st submission 26/06/
Re-submission Date Date Received 2nd submission
Student Name Do Nguyen Huy Hoang Student ID GCH
Class GCH0906 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
Grading grid
P1 P2 P3 M1 M2 M3 D1 D
Summat iv e Feedba ck: Resubm issio n Feedba ck:
Grade: Assesso r Signat u re: Date:
Internal Verifier’s Comments:
IV Signature:
Figure 1. Abstract data type model There are 3 main types of ADTs: Figure 2. 3 main Types of ADTs List ADT
- A list is an ordered collection of the same data type. Moreover, a list contains a finite number of values. We can’t store different data types in the same list. There are 3 types of lists:
Figure 3. types of list
- List also has several operation: get(), insert(), remove(), removeAt(), replace(), size(), isEmpty(), isNull(). Figure 4. operation of list Queue ADT
- A queue is a linear ADT with the restriction that insertion can be performed at one end and deletion at another. It works on the principle of FIFO (first-in, first-out). Hence, the first element to be removed from the queue is the element added first. We can store only one data type in a queue. There are 4 types of queues
- Stack operation: Figure 6. types of stacks ➢ Push(): adds a new item to the top of the stack. It needs the item and returns nothing. ➢ Pop(): removes the top item from the stack. It needs no parameters and returns the item. The stack is modified. ➢ Peek(): returns the top item from the stack but does not remove it. It needs no parameters. The stack is not modified. ➢ is_Empty(): tests to see whether the stack is empty. It needs no parameters and returns a boolean value. ➢ Size(): returns the number of items on the stack. It needs no parameters and returns an integer. ➢ Stack(): creates a new stack that is empty. It needs no parameters and returns an empty stack. (Brad Miller)
Figure 7. push and pop of stack Example: Push(): First create a new Integer stack called stack, then use for loo to push I into stack ad the result will be: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Figure 8.Push example Pop(): we use pop to remove an element out of stack, pop operator return a element that have been popped.
Figure 11. Pop and push of memory stack
How Memory stack is used to implement function calls in computer
(Joseph Yiu, 2015) whenever a function is called a new stack frame is created with all the function’s data and this stack frame is pushed in the program stack, and the stack pointer that always points the top of the program stack points the stack frame pushed as it is on the top of the program stack. (Prakash, 2020) Example: Step 1: when running program, when main function was founded java runtime will create a memory stack and will be used by main()
- In line 5 Int x=2 is global variable so it will be create and store in memory stack of main()
- In line 6 Int y=3 is global variable so it will be create and store in memory stack of main()
- In line 7 Int sum=x+y is global variable so it store in memory stack of main() Step 2: Stack Memory follow LIFO rule so at line 11, main() method terminates, and the Stack memory created for it is destroyed. Because the program ends at this line, Java Runtime frees all the memory and ends the execution of the program
- Queue ADT Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a 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, i.e., the data item stored first will be accessed first. (Aarthi,2020) Queue Operations:
- Queue() creates a new queue that is empty. It needs no parameters and returns an empty queue.
- enqueue(item) adds a new item to the rear of the queue. It needs the item and returns nothing.
- dequeue() removes the front item from the queue. It needs no parameters and returns the item. The queue is modified.
- is_empty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value.
- size() returns the number of items in the queue. It needs no parameters and returns an integer. P3 Using an imperative definition, specify the abstract data type for a software stack. Formal specification language is a limited specification in words, it bases on signs. Depending on the language specifications, there are different types of symbols. There are language specifications such as SDL, VDM
# Operation Pre-condition Post-condition Error-condition 1 Push(Integer i) s.isFull() == False s.Size() == n+1, Peek() == i s.isFull() == False 2 Pop( ): Integer s.isEmpty() == False,peek()=y s.Size() == n-1, Pop() = y s.isEmpty() == True 3 Peek( ): Integer s.isEmpty() == False s.Size()==n s.isEmpty() == True 4 isFull( ): Boolean None s.Size() == maxSize None 5 isEmpty( ): Boolean None s.Size() < 0 None 6 Size(): Integer None returns the number of elements in the stack None Variable describe:
- Int maxSize: maximum number can be stored into stack.
- Stack s
- int n = size() References GeeksforGeeks (2022, 25 May) “Abstract Data Types”. [Online]. Available at https://www.geeksforgeeks.org/abstract-data-types/ GeeksforGeeks (2022, 13 June) “Stack vs Heap Memory Allocation”. [Online]. Available at https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ Sommerville (2009) “Chapter 27 Formal”. [Online]. Available at Specification”https://web.mit.edu/16.35/www/lecturenotes/FormalMethods.pdf Neil Fox (2021, 23 March) “Stack Memory: An Overview (Part 3)”. [Online]. Available at https://www.varonis.com/blog/stack-memory- 3