Docsity

Data Structures and Algorithms: Stack and Queue Implementation, Cheat Sheet of Data Compression

Toggle sidebar
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
12/3/2023
Date Received 1st submission
12/3/2023
Re-submission Date
Date Received 2nd submission
Student Name
Luu Ngoc Hai
Student ID
GCH190599
Class
GCH1006
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Data Structures and Algorithms: Stack and Queue Implementation and more Cheat Sheet Data Compression 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 12/3/2023 Date Received 1st submission 12/3/ Re-submission Date Date Received 2nd submission Student Name Luu^ Ngoc^ Hai^ Student ID GCH Class GCH 1006 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

Assignment Brief and Guidance Scenario As assigned by the manager, in this work, you are asked to provide proper implementations and evaluations for the following tasks. (a) Implement a stack by a "linked list" that follows this graph representation below as underlying data structure. (b) Implement a queue by a "doubly-linked list" that follows this graph representation below as underlying data structure. (c) Implement and compare two solutions to reverse a Stack, that are using Recursion and without Recursion. The implemented operation should take a Stack as the input. Consider using only those data structures that are already implemented in Q.a and Q.b. Note that: (1) Most defined operations of Stack and Queue as stated in Assignment 1 should be implemented. (2) Errors should be handled carefully by exceptions and in the report some tests should be executed to prove the correctness of algorithms / operations. (3) Besides, in the report, the work should evaluate the use of ADT in design and development, including the complexity, the trade-off and the benefits.

Contents

  • P4 P5 P6 P7 M4 M5 D3 D
  • I. Design and implementation of Stack ADT and Queue ADT (P4).
    1. Stack
  • a. Introduction.....................................................................................................................................................
  • b. Operations.......................................................................................................................................................
    1. Queue
  • a. Introduction...................................................................................................................................................
  • b. Operations.....................................................................................................................................................
  • c. Implementation
  • II. Implement error handling and report test results (P5)
    1. Testing plan
    1. Evaluation
  • III. Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm (P6)
    1. Definition
    1. Asymptotic notations
    1. Examples about big O notation.
  • answer with an example(P7) IV. Determine two ways in which the efficiency of an algorithm can be measured, illustrating your
  • V. Reference....................................................................................................................................................

Figure 2: Description of Node Line: 8 + 9 Declare Data and NextNode are the two components of a node. The author assigns two values to the starting value. Both Data and NextNode are Null. To put it another way, the array of values contains nothing at first. b. Operations How the stack works as follows: o void push ( item) : adds a new item to the stack's top. It requires the item but does not provide a response; o pop() : pops the item at the top of the stack. It returns the item with no arguments. The stack has been changed. o peek() retrieves the stack's top item but does not delete it. There are no parameters required. The stack isn't changed. o boolean isEmpty() : Determines whether or not the stack is empty. It has no inputs and outputs a boolean value. When the stack is empty, return "true." o int size() : Returns the number of accessible stack items (Top+1). It has no inputs and outputs an integer. c. Implementation - My Stack interface: The author created NextNode to mark a new Node added to MyStack. The author initialize the variable count = the Nodes that MyStack has.MyStack.Max_size: is the maximum number of items that MyStack scan store that the user enters from the screen.

Figure 3: Stack interface

  • Push (item): a void Push (int item) is a new method that adds a new item to the top of the stack. It requires the item but does not provide a response. Lines 30 to 34: The author created a conditional function to check if the elements are overvalued. At line 30 the author defaulted to Max_size() the maximum value of the array to be customized by the user. If in the case that is equal to or greater than the default element, the statement will return: Stack is overflow. Otherwise, it will run and print as usual and without the statement of this function.

Figure 5: Description of line code 40 to 47.

  • Pop (): In the next action, the author has created a new Pop () function for the purpose of deleting the element he wants. First in lines 53 to 57 , the author created a function that checks if there is an element in that function, if there is no element, it will return: Stack is underflow and will not execute the next statements. From line 58 to 73 , the author has created a variable temp that stores the first element to be deleted. If FirstNode.NextNode is non-null, the delete attempt will be assigned with firstNode , and then firstNode = FirstNode.NextNode. The count counter will decrement by 1 element. In lines 67 to 69 , which is the case with only 1 element, when deleted, firstNode will be equal to Null and count will decrease back to its original value of 0.
  • Peek (): The author created the Peek(): method to rerun the first element. If there is no first element, Peek will return: Stack is empty.
  • isEmpty (): The author created a boolean isEmpty (): method to check if the function is empty or not. By first assigning the condition firstNode = Null , in this case the statements will return Stack is empty and true ; and if not null, this value will return false.
  • Constrain(int Data): The author created a search function and named it Constrain(): To search for an element in the array, we must have an element in the array, the author gave a statement to check if that element is in the array if none will return false. If there is a value, it will return that value and return true.

2. Queue a. Introduction A queue is a linear structure that executes tasks in a predetermined sequence. First in, first out (FIFO) refers to the order in which the pieces are placed in the Queue. A queue is a line of customers waiting for a resource, with the first client being served first. The stack and queue are dropped in the various procedures. The most recently added item is taken out of the stack, whereas the most recently added item is taken out of the queue. The author will discuss Queues' uses and construct a queue in the parts that follow. The ADT algorithm is built on a double topology. Figure 6: Double LinkedList

Because it is similar to Stack, Queue also has the same way of adding as that of Stack. The author on lines 27 to 30 has created a test condition in Methol isFull(). In isFull() will be responsible for checking if the number of elements in the array is greater than the allowed element. if greater than the element returns: Queue is Overflow and true. If it is not greater than the given element, it will just return false and continue running the following statements. In line 34, the author put another statement to check if this function is empty or not, if empty, head will point to that element. If the author only adds 1 element. Then Head = newNode. Figure 7: add 1 data in Queue

And in case you want to add elements to the existing elements. Then head will project on the newly added element, tail.next = newNode. newNode.prev = tail and increment count by 1. Figure 8: Add 1 data to many data in Queue

  • Dequeue(): In the next action, the Author also creates a function named Dequeue() for the purpose of deleting elements in the array. To delete, the author has put a conditional sentence in line 49, to check if the Head element is empty, if it is empty, the Queue will have no elements to delete. If it is not empty, it will be run

Pickfirst (): Method Pickfirst created with the purpose of saving the initial value for the array. Start method Pickfirst will check if function is empty or not if empty function will return Stack is empty and value -

If the function is not empty, it will return the head value of the array (Head).

  • Size():

The size() method returns a counter function: that counts the elements in the array. This function is quite special in case if the function has no value then it will return zero by itself and don't need to give anything like other methods.

  • isFull():

Boolean isFull() method as mentioned above this function helps to check whether the newly added elements in the array exceed the specified Max_Capacity? If it passes will return Queue is overflow and true. Otherwise, it will return False. II. Implement error handling and report test results (P5)

1. Testing plan No Scope Operation (^) Testing type Input Expected Output Actual Output Status 1 Stack Push(int Normal MyStack[2,3,5] MyStack[8,2,3,5] The same as Pass data) push(8); size()=4 expected output peek():return 8 2 Push(int Normal MyStack[] MyStack[8,] The same as Pass data) (^) push(8); size()= peek():return 8 expected output 3 Push(int Data MyStack[2,3,5] Printf(“Incorrect program stop Fail data) (^) validation push(@@@); input”) 4 Push(int^ Normal MyStack[8,2,3,5]^ MyStack[8,2,3,5]^ The^ same^ as^ Pass data) Max_element=4 size()=4 expected output push(10); peek():return 8 “Overflow” 5 Pop() Normal MyStack[2,3,5] MyStack[3,5] The same as Pass pop(); pop():return 2 expected output size()= peek():return 3