




































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!





































Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit 19: Data Structures and Algorithms
Submission date Date Received 1st submission
Re-submission Date Date Received 2nd submission
Student Name DAO VAN HIEU Student ID GCH
Class GCH1105^ 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 HIEU
Grading grid
P 4 P 5 P 6 P 7 M 4 M 5 D3 D
Grade: Assessor Signature: Date:
Internal Verifier’s Comments:
IV Signature:
First, I have a generic class named "Stack" and apply encapsulation in this Stack class, which is represented by the "Node" class inside of the "Stack" class. Each Node object has two properties: data and next. Data stores the value of the node and next will point to the next node. When a new Node object is created by calling “new Node(data)”, this method is invoked. It takes an argument, which is the data that the new node will store. This method initializes the 'data' field with the value of the argument, and the 'next' field is initially set to Null.
Figure 2: Innitialization Push method
Figure 3: Implemen of push method
push() adds a new item to the stack's top. It requires the item but returns nothing
Figure 4:image of push method(1)
First, with an initially empty linked list and providing an argument with a value of 98, a Node with data 98 and next as null will be generated as evidenced by the code (line 26). Currently, the list is empty, so the condition head == null is satisfied (line 27) , and you only need to assign "top" to the newly created Node (line28) and increase the size to 1 (line 33)
Figure 5: image of push method(2)
Currently, in the list, there is a newly added element with a data value of 98, and then I set the argument to
Figure 8: Implemen of size method
The size function in this Stack implementation returns the number of elements currently in the stack.
The way this function works is quite simple. When an element is added to the stack via the push method, the number of elements increases. When the element is removed via the pop method, the number of elements is reduced.
The size variable in the Stack class is updated at each push and pop method to ensure that it accurately represents the number of elements currently in the stack.
The size method simply returns the value of the size variable, which indicates the number of elements in the stack at the time of calling the method.
Pop method
Figure 9: Implemen of pop method
pop() removes the item at the top of the stack. It doesn't require any arguments and just returns the item. The stack has been changed. If the stack is empty, it prints the message "There are no elements in the stack!!!" and returns null to indicate that there are no elements to remove. Conversely, retrieve the value of the node at the top of the (current) stack, update top to point to the next node. Return the value of the removed current node
Figure 10: image of pop method
At the moment, the list contains 3 elements, so it will satisfy the 'else' condition and create a 'current' variable that is set equal to 'top' (line 41). It then moves 'top' down by one unit (line 42) and sets 'current.next' to null (line 43). Finally, it returns the 5 value that has been removed (line 45). After that, the size will be reduced to 2.
Peek method
Figure 11: Implemen of peek method
Check if the stack is empty. If it's empty, , it prints the message "There are no elements in the stack!!!" and returns null to indicate that there are no elements to access. If it's not empty, return the value of the element at the top of the stack through top.data, without removing the element
Figure 12: image of peek method
Figure 14: image of search method
First, you call the search function with an argument of data equal to 12. Then, you create a current node and set it to top (line 68). After declaring and initializing lastIndex and currentIndex (lines 69, 70), because current is initially set to top, you will traverse the entire list and compare it with the argument passed. If current != null (line 71), you start the while loop at the beginning by checking if current.data is not equal to arg (line 72). Then, you move current to the next node (line 75) and increase currentIndex by one.
You repeat the loop, and this time, current.data matches, so lastIndex becomes 1. I continue to move current to the next node and increase currentIndex by one, repeatedly checking, and when current becomes null, you return 1. This indicates that the last element with the value equal to arg was found at index 1 in the list.
In programming, a queue is a helpful data structure. It's like the line for tickets outside the movie theater, where the first person to go in is the one who receives the ticket.
The first item in the queue goes out first, according to the First In First Out (FIFO) rule.
a. Operation Method Description Parameter Return Value enqueue() Add a new element to the rear of queue T data No dequeue() Removes^ the^ elements^ from^ the^ front^ of queue.
No That element
getFront() Get the front element of the queue No That element getRear() Get the rear element of the queue^ No^ That element isEmpty() Check if the queue is empty No Boolean
size() Return the current number of elements in the queue
No Size
search() Find the last occurrence of the given data in the queue
T data Index
Table 2: Operation of queue b. Implementation
First, I have a generic class named "Queue" and apply encapsulation in this Stack class, which is represented by the "Node" class inside of the "Queue" class. Each Node object has 3 properties: data, prev and next. Data: Represents the actual data stored within each node of the queue. This property holds the generic type T, allowing the Queue class to handle elements of any data type. Prev: Points to the previous node in the queue, creating a doubly linked list structure that allows traversal both forwards and backwards within the queue. Next: Points to the next node in the queue, enabling sequential traversal from one node to the next. Besides the Node class, Queue also contains three other properties: front: Points to the first element of the queue, i.e. the element added first and will be removed first. rear: Points to the last element of the queue, i.e. the last added element. size: Stores the current number of elements in the queue. The variables front and rear help keep track of the position of the first and last element in the queue, while size holds information about the number of elements. This is important when performing inserts, retrieves, and checking the status of the queue.
Figure 15: deQueue method
First, Check if the queue is empty through the isEmpty() method. If the queue is empty, there are no elements to remove, the function will return null. If the queue is not empty, the data of the element at the beginning of the queue (stored in front.data) is retrieved and assigned to the variable data to then return. Checks if there is only one element in the queue (condition front == rear), which means removing the only element. In this case, both front and rear are set to null to indicate that the queue has become empty. In case the queue contains more than one element, a temporary Node (temp) is created to store the address. only of front. The front is then updated to point to the next element in the queue by assigning front = front.next. Also update the prev of the newly pointed front to null, to cut the link to the previous element. Finally, temp.next is set to null to cut the association of the first element with the next element. Decreases the size of the queue by one to indicate that an element has been removed and returns the value of that element.
Figure 16: enQueue() work getFront method
Figure 17: getFront() method
First, Check if the queue is empty through the isEmpty() method. If the queue is empty, meaning there are no elements, the method returns null to indicate that there is no data to get from the queue. If the queue is not empty, front.data is returned. This refers to the data of the first element in the queue without changing the structure of the queue.
Figure 18: getFont() work getRear method
Figure 19: getRear method
First, Check if the queue is empty through the isEmpty() method. If the queue is empty, meaning there are no elements, the method returns null to indicate that there is no data to get from the queue. If the queue is not empty, rear.data is returned. This references the data of the last element in the queue without changing the structure of the queue.
Figure 23: search method
First of all, it checks whether the queue is empty via the isEmpty() method. If the queue is empty, meaning there are no elements, the method returns -1 to indicate that no elements were found in the queue. If the queue is not empty, the lastIndex variable is initialized to -1, which is the last index of the element we are looking for. The index variable is initialized to 0 to count the current position of the element in the queue. The search process starts from rear (the last element of the queue). Through each loop, it checks the data of current (current element) to compare with the provided data. If there is a match, lastIndex is updated to index, representing the data's final position in the queue. After each check, current is moved to the previous element in the queue (this is done by moving to current.prev) and the index is increased by one. The returned result is lastIndex, which represents the last position of the data in the queue. If data is not found, the method returns -1, indicating that the data is not in the queue.
Figure 24: search() work
Assuming that Stack and Queue have duplicate values, I will create and execute the operations that receive Stack and Queue as inputs, respectively, and then eliminate the duplicate values from these data structures.
a. Stack