Data Structures and Algorithms: Implementation of Stack and Queue using LinkedList, High school final essays of Computer science

An assignment solution for Unit 19 of a BTEC Level 5 HND Diploma in Computing, focusing on Data Structures and Algorithms. The assignment involves the design and implementation of Stack and Queue Abstract Data Types (ADT) using a linked list. explanations of stack and queue concepts, their operations, and implementation using a linked list in Java. It also includes sample code and output.

Typology: High school final essays

2021/2022

Uploaded on 10/18/2022

handsome-hoang
handsome-hoang 🇻🇳

3.7

(7)

8 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT 2 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/2022
Re-submission Date
25/07/2022
Date Received 2nd submission
25/07/2022
Student Name
Do Nguyen Huy Hoang
Student ID
GCH200184
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
P4
P5
P6
P7
M4
M5
D3
D4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Data Structures and Algorithms: Implementation of Stack and Queue using LinkedList and more High school final essays Computer science in PDF only on Docsity!

ASSIGNMENT 2 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 25/07/2022 Date Received 2nd submission 2 5/07/

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

P4 P5 P6 P7 M4 M5 D3 D

Summative Feedback:

Resubmission Feedback: Grade: Assessor Signature: (^) Date: Internal Verifier’s Comments:

IV Signature:

I. Design and implementation of Stack ADT and Queue ADT (P4)

  1. Stack Stack is a 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, 2022) Figure 1. Stack Stack have a number of operations:
    • push() − Pushing (storing) an element on the stack.
    • pop() − Removing (accessing) an element from the stack.
    • peek() − get the top data element of the stack, without removing it.
    • isFull() − check if stack is full.
    • isEmpty() − check if stack is empty. In programming there are two ways to implement a stack is using an array or linked-list and in this report I’m going to use linked-list to implement a stack.

Implementation:

First I created a class Node.

Figure 2. Node class Then create class Linkedlist, then create head with type Node then check if it null then return true else it have value return false. Figure 3. Linked-list class

Figure 6. Peek operation Figure 7. Main function And the result is:

  • 1 was pushed to stack!
  • 2 was pushed to stack!
  • 3 was pushed to stack!
  • Top Element is: 3
  • Element was popped is: 3

Figure 8. Result of operations Figure 9. Push As diagram shown above, the last element added to stack is considered as the top of stack. When stack is empty 1 added to stack then 1 is top of stack, then add 2 into stack now 2 is top of stack then add 3 into stack, now 3 is top of stack.

Implementation:

Figure 11. Node class Figure 12. My queue class

Figure 13. enqueue function Figure 14. Dequeue function

Figure 17. Queue result II. Application (P4) Introduction In order to design and implement the remove duplicate value function out of linked-list here is my source code to execute that function First, I create a Node class with next is the next node.

Then in Linked-list class, I declare a Node called head

III. Implement error handling and report test results No Scope Type Operation Input Expected output Actual status output 1 Stack Normal Push() 10,5,4,2 10,5,4,2 was pushed Same as Pass ADT to stack expected Normal Pop() Pop() 2 Same as Pass expected Validation Pop() Empty stack Stack is empty and Same as Pass random min integer expected Validation Peek() Empty stack Stack is empty and Same as pass random min integer expected Normal Peek() stack[5,4,3] 5 Same as pass expected 2 Queue Normal Enqueue() Enqueue(1) Queue Element: 1 2 3 Same as pass ADT Enqueue(2) expected Enqueue(3) Normal Dequeue() Dequeue() Dequeue node: 1 Same as Pass expected Validation Dequeue() Empty queue Queue is empty Same as Pass expected 3 remove Normal removeDump() 10,12,12,13 10,12,13 Same as Pass duplicate expected Table 1. Testing table IV. Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm (P6) Asymptotic analysis of an algorithm refers to defining the mathematical boundation/framing of its run-time performance. Using asymptotic analysis, we can very well conclude the best case, average case, and worst case scenario of an algorithm. Asymptotic analysis is input bound i.e., if there 's no input to the algorithm, it is concluded to work in a constant time. Other than the "input" all othe r factors are considered constant.

  • Θ Notation: The theta notation bounds a function from above and below, so it defines exact asymptotic behavior. A simple way to get the Theta notation of an expression is to drop low - order terms and ignore leading constants. For example, consider the following expression. - Example: 3n2 + 6n2 + 6000 = Θ(n2) Dropping lower order terms is always fine because there will always be a number(n) after which Θ(n3) has higher values than Θ(n2) irrespective of the constants involved. For a given function g(n), we denote Θ(g(n)) is following set of functions. Θ(g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 <= c1g(n) <= f(n) <= c2g(n) for all n >= n0}

The above definition means, if f(n) is theta of g(n), then the value f(n) is always between c1g(n) and c2g(n) for large values of n (n >= n0). The definition of theta also requires that f(n) must b e non- negative for values of n greater than n0. (GeeksforGeeks, 2022)

  • Big O Notation: The Big O notation defines an upper bound of an algorithm, it bounds a function only from above. For example, consider the case of Insertion Sort. It takes linear time in the best case and quadratic time in the worst case. We can safely say that the time complexity of Insertion sort is O(n^2). Note that O(n^2) also covers linear time. ➢ If we use Θ notation to represent time complexity of Insertion sort, we have to use two statements for best and worst cases: o 1. The worst-case time complexity of Insertion Sort is Θ(n^2). o 2. The best-case time complexity of Insertion Sort is Θ(n). The Big O notation is useful when we only have an upper bound on the time complexity of an algorithm. Many times we easily find an upper bound by simply looking at the algorithm. O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 <= f(n) <= c*g(n) for all n >= n0}

V. Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an example (P7) space complexity: Space Complexity is misused for Auxiliary Space at many places. Following are the correct definitions of Auxiliary Space and Space Complexity. Auxiliary Space is the extra space or temporary space used by an algorithm. Space Complexity of an algorithm is the total space taken by the algorithm with respect to the input size. Space complexity includes both Auxiliary space and space used by input. Time complexity: The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input. Note that the time to run is a function of the length of the input and not the actual execution time of the machine on which the algorithm is running on. (GeeksforGeeks, 2021) Example: public static int sum(int k) { if (k <= 0 ) { return 0 ; } else { return k + sum(k - 1 ); } } In this example, k=5 each call will add new sum()to stack: sum( 5 ), sum(4), sum(3), sum(2), sum(1), sum(0) and each of these added to stack will take actual memory so the more k we input the more space we need to sum up it takes O(n) space and This function is being called recursively n times before reaching the base case so its O(n). public static int sum() { int a = 1 , b = 2 , c = 3 ; int result = a + b + c; return result; } In this example, space complexity for data will be 4 variable are a, b, c, result and time complexity is O(1) because we the process will do exactly one time.

References GeeksforGeeks (2022, June 14) “Analysis of Algorithms | Set 3 (Asymptotic Notations)”. [Online]. Available at: https://www.geeksforgeeks.org/analysis-of-algorithms-set-3asymptotic-notations/ GeeksforGeeks (2022, June 02) “Analysis of Algorithms | Set 1 (Asymptotic Analysis)”. [Online]. Available at: https://www.geeksforgeeks.org/analysis-of-algorithms-set- 1 - asymptotic- analysis/#:~:text=Asymptotic%20Analysis%20is%20the%20big,increases%20with%20the%20input%20size. Kalkicode (2021, November 06) “Implement queue using doubly linked list in java”. [online]. Available at: https://kalkicode.com/implement-queue-using-doubly-linked-list-in-java GeeksforGeeks (2022, June 18) “Stack Data Structure”. [Online]. Available at: https://www.geeksforgeeks.org/stack-data-structure/