Docsity

Stack and Queue Operations and Implementation: Data Structures, Exams of Data Structures and Algorithms

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
Date Received 1st
submission
Re-submission Date
Date Received 2nd
submission
Student Name
DAO VAN HIEU
Student ID
GCH211135
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
P5
P6
P7
M4
M5
D3
D4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download Stack and Queue Operations and Implementation: Data Structures and more Exams Data Structures and Algorithms 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 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

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:

Internal Verifier’s Comments:

IV Signature:

  • CONTENTS______________________________________________________________________________________________
  • FIGURE ________________________________________________________________________________________________
  • TABLE _________________________________________________________________________________________________
    1. DESIGN AND IMPLEMENTATION OF STACK ADT AND QUEUE ADT (P4) _______________________________________
    • 1.1. STACK ________________________________________________________________________________________
      • 1.1.1. Introduction ________________________________________________________________________________
      • 1.1.2. Operation And Implementation _________________________________________________________________
    • 1.2. QUEUE _______________________________________________________________________________________
      • 1.2.1. Introduction _______________________________________________________________________________
      • 1.2.2. Operation And Implementation ________________________________________________________________
    1. APPLICATION (P4 + M4) _____________________________________________________________________________
    • 2.1. INTRODUCTION _________________________________________________________________________________
    • 2.2. IMPLEMENTATION _______________________________________________________________________________
    1. IMPLEMENT ERROR HANDLING AND REPORT TEST RESULTS (P5) ___________________________________________
    • 3.1. TESTING PLAN __________________________________________________________________________________
    • 3.2. EVALUATION ___________________________________________________________________________________
    1. DISCUSS HOW ASYMPTOTIC ANALYSIS CAN BE USED TO ASSESS THE EFFECTIVENESS OF AN ALGORITHM (P6) ______
    • 4.1. ASYMPTOTIC NOTATIONS CAN BE EXPLAINED ______________________________________________________________
    • 4.2. BIG O COMPLEXITY CHART _________________________________________________________________________
  • ANSWER WITH AN EXAMPLE (P7) __________________________________________________________________________ 5. DETERMINE TWO WAYS IN WHICH THE EFFICIENCY OF AN ALGORITHM CAN BE MEASURED, ILLUSTRATING YOUR
    1. TRADE-OFF WHEN SPECIFYING AN ADT (M5) ___________________________________________________________
  • FIGURE 1: STACK Figure
  • FIGURE 2: INNITIALIZATION
  • FIGURE 3: IMPLEMEN OF PUSH METHOD
  • FIGURE 4:IMAGE OF PUSH METHOD(1).....................................................................................................................................................
  • FIGURE 5: IMAGE OF PUSH METHOD(2)
  • FIGURE 6: IMAGE OF PUSH METHOD(3)
  • FIGURE 7: IMPLEMEN OF ISEMPTY METHOD
  • FIGURE 8: IMPLEMEN OF SIZE METHOD
  • FIGURE 9: IMPLEMEN OF POP METHOD
  • FIGURE 10: IMAGE OF POP METHOD
  • FIGURE 11: IMPLEMEN OF PEEK METHOD
  • FIGURE 12: IMAGE OF PEEK METHOD
  • FIGURE 13: IMPLEMEN OF SEARCH METHOD
  • FIGURE 14: IMAGE OF SEARCH METHOD
  • FIGURE 15: DEQUEUE METHOD
  • FIGURE 16: ENQUEUE() WORK
  • FIGURE 17: GETFRONT() METHOD.........................................................................................................................................................
  • FIGURE 18: GETFONT() WORK
  • FIGURE 19: GETREAR METHOD
  • FIGURE 20: GETREAR() WORK
  • FIGURE 21: ISEMPTY METHOD..............................................................................................................................................................
  • FIGURE 22: SIZE METHOD
  • FIGURE 23: SEARCH METHOD
  • FIGURE 24: SEARCH() WORK
  • FIGURE 25: REMOVEDUPLICATE OF STACK
  • FIGURE 26: REMOVEDUPLICATE OF STACK WORK
  • FIGURE 27: REMOVEDUPLICATE OF QUEUE
  • FIGURE 28: REMOVEDUPLICATE OF QUEUE WORK
  • FIGURE 29: BIG O (BOARDINFINITY, N.D.)
  • FIGURE 30: THETA NOTATION (BOARDINFINITY, N.D.)
  • FIGURE 31: OMEGA NOTATION (BOARDINFINITY, N.D.).............................................................................................................................
  • FIGURE 32: BIG O COMPLEXITY CHART (WESIONARY, N.D.)
  • FIGURE 33: EXAMPLE 1 OF BIG O
  • FIGURE 34: EXAMPLE 2 OF BIG O
  • FIGURE 35: SOURCE CODE OF ARRAY
  • FIGURE 36: SOURCE CODE OF LINKEDLIST
  • TABLE 1: OPERATION OF STACK Table
  • TABLE 2: OPERATION OF QUEUE
  •  TABLE 3: INITIALIZATION
  •  TABLE 4: ENQUEUE METHOD
  • TABLE 5: ENQUEUE() WORK
  • TABLE 6: TESTING PLAN:
  • TABLE 7: SOLUTION FOR WRONG
  • TABLE 8: THE BIG O CHART
  • TABLE 9: EXAMPLE 3 OF BIG O..............................................................................................................................................................
  • TABLE 10: TRADE-OFF ANALYSIS TABLE

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: InnitializationPush 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

  1. This will generate a new Node with data as 33 line(26). At this point, the list has one element, so it enters the "else" part, and "newNode.next" is used to point to the "top" line(30). After this reference is set, the position of "top" is updated to the "newNode," line(31) and the size is increased to 2 line(33).

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.

1.2. Queue

1.2.1. Introduction

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.

1.2.2. Operation And Implementation

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() workgetFront 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() workgetRear 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

2. Application (P4 + M4)

2.1. Introduction

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.

2.2. Implementation

a. Stack