





































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
Assignment 1 1649 who want to merit
Typology: Assignments
Uploaded on 11/17/2023
16 documents
1 / 45
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 02 - 11 - 2023 Date Received 1st submission 02 - 11 - 2023 Re-submission Date Date Received 2nd submission Student Name Nguyễn Văn Quang Student ID Gch Class Gch1107 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
Summative Feedback: Resubmission Feedback: Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:
Stack works on the LIFO pattern. As we can observe in the below figure there are five memory blocks in the stack; therefore, the size of the stack is 5. Suppose we want to store the elements in a stack and let's assume that stack is empty. We have taken the stack of size 5 as shown below in which we are pushing the elements one by one until the stack becomes full. Since our stack is full as the size of the stack is 5. In the above cases, we can observe that it goes from the top to the bottom when we were entering the new element in the stack. The stack gets filled up from the bottom to the top. When we perform the delete operation on the stack, there is only one way for entry and exit as the other end is closed. It follows the LIFO pattern, which means that the value entered first will be removed last. In the above case, the value 5 is entered first, so it will be removed only after the deletion of all the other elements. a, Overview stack operations
Purpose of function Add a new element into the top of the stack Parameters A new element with value is data Return Because the function is of type void, it does not return any value Adds a new element to the top of a stack implemented as a linked list. If the stack is empty, it sets the new element as the top. If the stack isn't empty, it links the new element to the current top and updates the top. The size keeps track of the number of elements.
Figure 3 : push method of stack Explanation of figure 3: Initially, we check if the top is null. Because there are two elements, it means we need to add to the beginning of the list with the parameter value of 7, and as a result, we get a new value of 7 at the top. As can be seen in the illustrated image. c, pop() function
Purpose of function Delete the element at the top of the stack Parameters No parameters Return After deleting, the function will return the deleted value The pop() function checks whether the stack is empty or not, if empty, returns null, if there is an element, deletes that element from the stack, the top element after deletion will be the element below. Returns the deleted value. Figure 4 : pop method of stack Explanation of figure 4 : First check if the list is empty which will return null. Currently the list has 3 elements, the first element will be deleted and the data returned is 2.
Figure 6 : isEmpty method of stack Explanation of figure 6 : The first image in the list has 2 elements so it will return false. In the next image, the list is null so it will return true f. size() function
Purpose of function Returns the total number of elements in the list Parameters No parameters Return If the element does not have a list, return 0. If it does, return the number of elements counted by count Figure 7 : size method of stack
Explanation of figure 7 : The first image, when the list is empty, will return 0, and the next image with 3 elements will return the value 3, which can be seen through the illustration. g. search(int data) function
Purpose of function When you have data and want to find its location Parameters The parameter is the value that I want to search for, the result is index Return Returns the index that corresponds to the input data The function will return - 1 if the corresponding data is not found or the list is empty. The function will return the index if there is valid search data by linear search running through each element. If equal, immediately return the index and stop the program. Figure 8 :search method of stack Explanation of figure 8 : At first check, if there are no elements or the list is null, return - 1. Currently the list has 3 elements and the argument passed to the search function is 1. We can see that with the value 1, the index is also 1 so it will return the value 1. Application of Stack Data Structure: (GeeksforGeeks, 2023)
Figure 9 :queue image (Geeksforgeeks, 2023) Overview Queue operations enQueue(int data): Push an item of data type Element to the end of the Queue (rear). deQueue(): Removes the first element of the Queue (front) and returns that value if deletion succeeds, returns null if deletion fails. isEmpty(): Checks whether the Queue has an element, returns true if the Queue is empty, returns false if the Queue has an item. getFront(): Gets the front element of the Queue without removing it, returns front on success, null on failure. search(int data): Through value find the corresponding index. To illustrate Queue operations, a scenario will be shared for operations. Scenario (Practical example): In schools, bags are distributed to new students and connections are arranged according to student number. For example, a student who has finished retrieving goods, corresponding to the removal and addition of a new student at the same level, is placed last by and is considered as adding a new element. a, endQueue(int number) function enQueueint number) :void Purpose of function Add a new element into the rear of the stack Parameters A new element with value is number
Return Because the function is of type void, it does not return any value The item of type int will be put to the back of the queue by the enQueue function, which will also push down the previous back position. The object being pushed is both front and back if the Queue is empty. Figure 10 :enQueue method of stack Explanation of figure 10 : Currently there are two students in the list and now there will be more students sorted and displayed according to each member's id number. With enQueue(2122) as argument representing the value to add to the end of the row. b. deQueue()
Purpose of function Delete the element at the top of the stack Parameters No parameters Return After deleting, the function will return the deleted value The item at the front of the queue is deleted using the deQueue function, which also returns the item in the event that the deletion is successful. Null is returned if delete fails for any reason, such as an empty queue. The front position will be the item after deletion.
If successful, the getFront function will return the front value of the queue; if the queue is empty, it will return null. The Queue remains unchanged by the function. Figure 13 : getFont method of stack Explanation of figure 1 3: The getFront() function returns the student's top id number. If the list is empty, it returns null. In the illustration, you can see that when calling the function, it will return 2120. e.search(int data)
Purpose of function When you have data and want to find its location Parameters The parameter is the value that I want to search for, the result is index Return Returns the index that corresponds to the input data The function will return - 1 if the corresponding data is not found or the list is empty. The function will return the index if there is valid search data by linear search running through each element. If equal, immediately return the index and stop the program. Figure 14 :search method of stack Here are some of the common application of queue in data structure: (Kochar, 2023)
In programming, whenever a function is called, the program generates a new stack memory block for the function to utilize. This type of memory is located in the stack section of a program’s memory space, which is a reserved restricted memory area. This section is usually located at the top of the memory space and grows downward as more data is added. Stack memory employs an automatic allocation and deallocation of memory that stores temporary data created by functions or procedures. Stack memory uses a “Last In, First Out” (LIFO) data structure, meaning that the most recently added item is the first to be removed. When the function or procedure is finished executing, the stack memory block is released automatically, and the program returns to the previous point of execution (Sciencedirect, 2023)
from the calling function, which allows the computer to know where to resume execution when the called function completes. Function Execution: The called function executes, using the data stored in its stack frame for local variables and parameters. It can also make its own function calls, which create additional stack frames for those functions. Popping Data After Function Call: After a function finishes its execution, the data associated with that function call is popped from the stack. This removal of the stack frame ensures that memory is properly managed and that control returns to the correct point in the calling function. Nested Function Calls: Function calls can be nested within one another, creating a stack of stack frames on the memory stack. This allows functions to call other functions, and when each function completes, its stack frame is removed from the top of the stack, allowing the program to return to the previous function in the call chain. Return to the Caller Function: The return address stored in the stack frame points to the instruction in the calling function where control should resume. This ensures that execution continues in the correct context. Figure 16 :function calls in a computer For example:
Figure 17 : Sourse code of recursively calculate the sum Figure 18 : Caculate example above Explanation of figure 17 and 18: sum(3) is called with num equal to 3 and starts a frame on the stack for the sum function with num as 3. In this frame, the function checks if num is equal to 1. Since num is 3, the function enters the else block and calls sum(2). Next, another frame for sum(2) is created on the stack with num as 2. The sum(2) function also checks if num is equal to 1. Since num is 2, the function enters the else block and calls sum(1). Another frame for sum(1) is created on the stack with num as 1. The sum(1) function does not make any further recursive calls and reaches the base case. It returns 1. The result from sum(1) (which is 1) is computed, and the frame of sum(1) is removed from the stack. Back to the frame of sum(2) with the result from step 7 (which is 1), it calculates 2 + 1 = 3, and the frame of sum(2) is also removed from the stack. Returning to the frame of sum(3) with the result from step 8 (which is 3), it calculates 3 + 3 = 6. Finally, the frame of sum(3) is also removed from the stack, and the final result is 6. During this process, each recursive call creates a new frame on the stack, and when it reaches the base case, the frames are removed from the stack in a Last-In-First-Out (LIFO) manner.