


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



































CONTENTS______________________________________________________________________________________________ 1
FIGURE ________________________________________________________________________________________________ 2
TABLE _________________________________________________________________________________________________ 3
P1 + M1 CREATE A DESIGN SPECIFICATION FOR DATA STRUCTURES EXPLAINING THE VALID OPERATIONS THAT CAN BE CARRIED OUT ON THE STRUCTURES. ________________________________________________________________________ 4
P2 DETERMINE THE OPERATIONS OF A MEMORY STACK AND HOW IT IS USED TO IMPLEMENT FUNCTION CALLS IN A COMPUTER. ___________________________________________________________________________________________ 14
M2 SORTING ALGORITHMS. ______________________________________________________________________________ 19
P3 USING AN IMPERATIVE DEFINITION, SPECIFY THE ABSTRACT DATA TYPE FOR A SOFTWARE STACK. _________________ 31
FIGURE 1: ABSTRACT DATATYPE (GEEKSFORGEEKS, N.D.) ________________________________________________________________ 4 FIGURE 2: STACK ADT (TUTORIALSPOINT, N.D.) ______________________________________________________________________ 5 FIGURE 3: STACK PUSH METHOD ________________________________________________________________________________ 6 FIGURE 4: STACK POP METHOD _________________________________________________________________________________ 7 FIGURE 5: STACK PEEK METHOD _________________________________________________________________________________ 8 FIGURE 6: STACK EMPTY METHOD _______________________________________________________________________________ 8
FIGURE 26: :EXAMPLE OF QUICK SORT (2) _________________________________________________________________________ 28 FIGURE 27: EXAMPLE OF QUICK SORT (3) _________________________________________________________________________ 29 FIGURE 28:EXAMPLE OF QUICK SORT (4) __________________________________________________________________________ 29 FIGURE 29: SINGLY LINKED LIST ________________________________________________________________________________ 38 FIGURE 30: WRITE-ONLY _____________________________________________________________________________________ 39 FIGURE 31: READ-ONLY _____________________________________________________________________________________ 39
TABLE 1: PUSH(INT DATA) FUNCTION _____________________________________________________________________________ 5 TABLE 2: POP(): INTEGER _____________________________________________________________________________________ 6 TABLE 3: PEEK(): INTEGER _____________________________________________________________________________________ 7 TABLE 4: ISEMPTY(): BOOLEAN _________________________________________________________________________________ 8 TABLE 5: SIZE(): INT _________________________________________________________________________________________ 9 TABLE 6: ENDQUEUE(INT NUMBER) FUNCTION ______________________________________________________________________ 11
TABLE 7: DEQUEUE() _______________________________________________________________________________________ 11 TABLE 8: ISEMPTY _________________________________________________________________________________________ 12 TABLE 9: PEEK()___________________________________________________________________________________________ 13 TABLE 10: COMPARE SELECTION SORT AND QUICK SORT _______________________________________________________________ 21 TABLE 11:PSEUDOCODE OF SELECTION SORT _______________________________________________________________________ 21
An object's behavior is determined by a set of values and a set of actions, and they are referred to as the abstract data type (ADT) or class. The ADT specification merely specifies the tasks that must be completed; it makes no statement of how they will be carried out. It is unclear what algorithms will be applied to carry out the operations and how the data will be arranged in memory. Because it provides an implementationindependent view, it is referred to be "abstract." (Geeksforgeeks, n.d.)
Figure 1: Abstract Datatype (Geeksforgeeks, n.d.)
Return The function will return true if the add is successful and fail if the add is not successful Table 1: Push(int data) function
The push method checks whether the stack is empty or not. If it is empty then it places the new element as the top. If not empty, it creates a new node and assigns the element to be added to that node, then assigns the new node to the top of the stack and updates the top of the stack to point to the new node.node.
Figure 3: Stack push method
Explanation: First, check if the list is empty or not. Given that there are two entries, we must append the parameter value of 91 to the beginning of the list. Consequently, the list's beginning now has the new value of 91.
b. Pop()
Pop(): Integer Purpose Delete the element at the top of the stack Parameters No parameter Return If the list is null return null, if the list is not null method will return the deleted value Table 2: Pop(): Integer
Check if the stack is empty or not. If empty, return null. If not empty, then access the element at the top of the stack and store it in a temporary variable. Then, remove that element from the stack, and update the top of the stack to point to the next element. Returns a temporary variable containing the deleted element.
Figure 4: Stack pop method
Explanation: First, check if the list is empty or not. As you can see, there are now three elements in the list. The first element will be deleted and 3 will be returned
c. Peek
Peek(): Integer Purpose Get the value from the top element of the stack Parameters No parameter Return If the list is null return null, if the list is not null return the value of top Table 3: Peek(): Integer
Check if the stack is empty or not. If empty, then return null. If not empty, then access the element at the top of the stack and return it.
Figure 5: Stack peek method
Return If the list has no elements, return 0. If there are, return the value of the counter variable count. Table 5: size(): int
First initialize a counter variable and assign it to 0. Initialize a pointer and assign it to the top of the stack. Repeat until the pointer is null: Increase the counter variable by 1. Update the pointer to the next element in the stack. Returns the counter variable.
Figure 7: Stack size method
Explanation: The image on the left, because the list has 2 elements, will return the value 3. The remaining image, because it has no elements, will return 0.
A queue is a linear ADT that is limited to allowing insertions at one end and deletions at the other. It operates according to the first-in, first-out (FIFO) principle. As a result, the element added first is also the element that is initially taken out of the queue. (Datta, 2023)
Enqueue : Add an element to the back of the queue.
Dequeue : Remove the element at the front of the queue.
Peek : Return the element at the front of the queue without removing it.
isEmpty : Check if the queue is empty.
Return The function will return the deleted value after deletion. Table 7: deQueue() The deQueue function removes the item at the head of the queue and, should the deletion be successful, returns the item. If delete fails for any reason—for example, an empty queue—Null is returned. The item that is in front will be the one that is deleted.
Figure 9: deQueue
Explanation: When they finish buying food, they will leave the cave and delete the id number they just left on the line. By calling the deQueue() function to delete and return the newly deleted value. c. isEmpty()
isEmpty(): boolean Purpose Check if the queue is empty Parameters No parameter Return It returns true if the list is empty and false otherwise. Table 8: isEmpty
The isEmpty method returns true if there are entries in the queue and false otherwise. The isEmpty method looks for items in the queue to test if it is empty.
Figure 10: isEmpty
Explanation: returns true if there are no food buyers in the queue. Returns false in case someone buys food. d. peek()
peek(): Integer Purpose Get the value of the front of the queue without removing it Parameters No parameter Return The function shall return data at the top of the stack after obtaining data. Table 9: peek()
The front value in the queue will be returned by the peek method if it is successful; null will be returned if the queue is empty. The function does not alter the Queue.
Figure 11: peek
Explanation: The getFront() function returns the top id number of the food buyer. If the list is empty, it returns null. In the illustration you can see that when calling the function, it returns 98.
A call stack is composed of stack frames (also called activation records or activation frames). These are machine dependent and ABI-dependent data structures containing subroutine state information. Each stack frame corresponds to a call to a subroutine which has not yet terminated with a return (wikipedia, 2023).
Return address : The address in memory of the calling function's instruction that controls should go back to once the called function has finished.
Local variables: Reserving space for variables unique to the function that is invoked.
Parameters : The values or references to the input arguments of the function. Additional details about bookkeeping.
Pushing data for a Function Call : Upon calling a function, all required information is pushed onto the stack, such as the function-specific variables and return address. In order for the computer to know where to start running again after the called function has finished, it is necessary to store the return address from the calling function. Function Execution : When a function is called, its stack frame contains the data that is used to hold local variables and parameters. Additionally, it has the ability to call its own functions, adding new stack frames for those operations.
Data Popping Following Function Call : Following the completion of a function's execution, the data related to that function call is removed off the stack. By removing the stack frame, control is returned to the correct location in the calling code and memory is managed appropriately.
Nested Function Calls : On the memory stack, a stack of stack frames is produced when function calls are nested inside one another. Functions can call other functions in this way, and as each function finishes, its
stack frame is taken down from the top of the stack, enabling the program to go back to the call chain's preceding function.
Return to the Caller Function : The calling function's command directing where control should be taken over is indicated by the return address kept in the stack frame. This guarantees that operations continue within the appropriate framework.
Figure 12:Function calls in a computers For
instance:
Figure 13:Sourse code of recursively calculate the fatorial
Figure 15: Memory Stack
Figure 16: Memory Stack
An array or list of elements can be rearranged using a sorting algorithm based on a comparison operator applied to the elements. The new element order in the corresponding data structure is determined by the comparison operator. (geeksforgeeks, 2023)
Some popular methods :
Discuss the purpose of sorting
Sorting is the process of arranging items systematically, usually in ascending or descending order, based on certain criteria such as numerical, alphabetical, or any defined characteristic. Its purpose is multifaceted and serves various practical applications across different domains: