Docsity

Data Structures and Algorithms - Assignment 1, Papers of Data Structures and Algorithms

Toggle sidebar
bg1
1 | H o N g o c K h a n h
ASSIGNMENT 1 FRONT SHEET
Qualification
BTEC Level 5 HND Diploma in Computing
Unit number and title
Unit 19: Data Structures and Algorithms
Submission date
17 April 2022
Date Received 1st
submission
Re-submission Date
Date Received 2nd
submission
Student Name
Hồ Ngọc Khánh
Student ID
GCS200074
Class
GCD0901
Assessor name
Phạm Thanh Sơn
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
Khanh
Grading grid
P1
P2
P3
M1
M2
M3
D1
D2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Data Structures and Algorithms - Assignment 1 and more Papers Data Structures and Algorithms in PDF only on Docsity!

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 19 : Data Structures and Algorithms

Submission date 17 April 2022 Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Hồ Ngọc Khánh Student ID GCS

Class GCD0901 Assessor name Phạm Thanh Sơn

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 Khanh

Grading grid

P1 P2 P3 M1 M2 M3 D1 D

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:

Internal Verifier’s Comments:

IV Signature:

  • A. DATA STRUCTURES
    • I. Abstract data type (P1)
        1. Definition
        1. Examples: Singly Linked List
    • II. ADT Usages (P2 – P3)
        1. Application of Stack in memory (P2)
        1. Application of an ADT: Implement Redo/Undo buttons using Linked List (P3)
  • B. References
  • Figure 1: TYPES OF ADTS FIGURES AND TABLES
  • Figure 2: CODE FOR INSERTING NEW DATA INTO LINKED LIST
  • Figure 3: RESULT FOR CREATION AND INSERTATION
  • Figure 4: CODE FOR FUNCTION OF TRAVERSAL............................................................................................................
  • Figure 5: RESULT OF TRAVERSAL
  • Figure 6: CODE FOR DELETION BY KEY FUNCTIONALITY
  • Figure 7: RESULT FOR DELETION BY KEY
  • Figure 8: CODE FOR DELETION AT POSITION
  • Figure 9: RESULT FOR DELETION AT POSITION
  • Figure 10: STACK'S MEMORY MANAGEMENT
  • Figure 11: STACK MEMORY ORGANIZATION ILLUSTRATION
  • Figure 12: EXAMPLE OF METHOD CALL STACK
  • Figure 13: TWO CHAR STACKS FOR THE PROGRAM
  • Figure 14: CODE FOR WRITE FUNCTION
  • Figure 15: CODE FOR UNDO FUNCTION
  • Figure 16: CODE FOR REDO FUNCTION
  • Figure 17: CODE FOR READ FUNCTION
  • Figure 18: CODE FOR QUERY FUNCTION
  • Figure 19: CODE FOR THE MAIN FUNCTION
  • Figure 20: RESULT FOR THE ABOVE PROBLEM
  • Table 1: SINGLY LINKED LIST STRUCTURE
  • Table 2: SINGLY LINKED LIST CREATION AND INSERTATION
  • Table 3: SINGLY LINKED LIST DELETION
  • Table 4: SINGLY LINKED LIST DELETION AT POSITION

A. DATA STRUCTURES

I. Abstract data type (P1)

1. Definition

An abstract data type (ADT) is a data type notion or paradigm. ADT eliminates the need for a user to

be concerned about how a data type is implemented. Furthermore, ADT handles the implementation of

functions on a data type. In this section, the user will find preset functions for each data type that are

ready to use for any action.

ADTs are implemented differently in each programming language, employing distinct techniques and

logic. Regardless of language, we can still conduct all of the related operations described for that ADT.

ADTs, for example, are generally implemented in C using structure. In C++ or JAVA, on the other hand, they

are implemented using classes.

ADTs are a common and significant form of data. ADTs are mathematical or logical notions that may

be implemented on many computers using various languages. Furthermore, they are exceedingly

adaptable and do not rely on languages or machinery. (Datta, 2021)

Figure 1 : TYPES OF ADTS

Are a set of objects and procedures that offer their users with well-defined abstract features while

concealing how they are represented in terms of lower-level data representations. When a module is used

Because a Linked List is normally represented by its head pointer, traversing the list to the final node

and then changing the next to last node to the new node is necessary.

In this function, it will insert in the linked list the new data. However, it also will check on the status

of the new nodes, the current nodes and the last nodes to implement them into the linked list.

Figure 2 : CODE FOR INSERTING NEW DATA INTO LINKED LIST

LinkedList: 1 2 3 4 5 6 7

Figure 3 : RESULT FOR CREATION AND INSERTATION

TRAVERSAL

PrintList() is a general-purpose function for traversal that prints any provided list by traversing the

list from head node to final node.

In this function, the program will allow the function to print out all the data in the linked list by

traversing from the head node to the final node of the linked list.

Figure 4 : CODE FOR FUNCTION OF TRAVERSAL

LinkedList: 1 2 3 4 5 6 7 8

Figure 5 : RESULT OF TRAVERSAL

DELETION BY KEY

Head

Prev

Tmp

A B C → D → NULL

Data Next Data Next Data Next Data Next

Table 3 : SINGLY LINKED LIST DELETION

Follow the steps below to remove a node from the linked list. Look for the key's first appearance in

the list. Now, any of the three criteria can exist:

Case 1 : The key is discovered at the head.

  1. In this situation, change the node's head to the node after the current head.
  2. Free the memory associated with the replacement head node.

Case 2 : Except at the beginning, the key is discovered in the center or at the end.

  1. Find the predecessor node of the node to be eliminated in this circumstance.

4 found and deleted

LinkedList: 2 3 5 6 7 8

10 not found

LinkedList: 2 3 5 6 7 8

Figure 7 : RESULT FOR DELETION BY KEY

DELETION AT POSITION

Head

Prev

Tmp

A B C → D → NULL

Data Next Data Next Data Next Data Next

Table 4 : SINGLY LINKED LIST DELETION AT POSITION

The following are the measures to take:

  1. Traverse the list by counting the node indexes.
  2. Match the index to the place for each index.
  3. Now, any of the three criteria can exist:

Case 1 : The position is 0, indicating that the head is to be removed.

  1. In this situation, change the node's head to the node after the current head.
  2. Remove the memory from the replacement head node.

Case 2 : The position is larger than 0 but less than the number of items in the list, i.e., in the middle

or at the end, except at the top.

  1. Find the predecessor node of the node to be eliminated in this circumstance.
  2. Change the previous node's next to the current node's next.
  3. Release the memory of the substituted node.

Case 3 : The position is bigger than the list's size, i.e., the location is not found in the list.

  • In this instance, no surgery is required.

Figure 8 : CODE FOR DELETION AT POSITION

LinkedList: 1 2 3 4 5 6 7 8

0 position element deleted

LinkedList: 2 3 4 5 6 7 8

Figure 10 : STACK'S MEMORY MANAGEMENT

A stack is a straightforward last-in-first-out (LIFO) data structure. It is usual to teach stacks to young

computer scientists by imagining a stack of dishes at a cafeteria. In the cafeteria, dishes are only removed

or added to the stack at the top. When we initially enter the cafeteria, one of our first activities is to take

a plate from the top of the stack of plates before proceeding through the line. Similarly, the dishwashers

place each clean plate one at a time on top of the stack. As a result, the last clean plate deposited into the

stack is the first plate a client pulls off the top. It is not permissible to insert or remove a plate from the

stack's center. (Anon., 2017)

The stack pointer on a 64-word stack has 6 bits since 26 = 64. Because SP only has six bits, it cannot

be more than 63. (111111 in binary). When 63 is multiplied by one, the output is zero since 111111 + 1 =

1000000 in binary, but SP can only allow the six least significant bits. When 000000 is decremented by

one, the result is 111111. When the stack is full, the one-bit register Full is set to 1, and when the stack is

empty, the one-bit value EMTY is set to 1. The data register (DR) stores the binary data that will be written

to or retrieved from the stack.

Figure 11 : STACK MEMORY ORGANIZATION ILLUSTRATION

HOW A METHOD CALLS IS IMPLEMENTED

The method-call stack (also known as the program-execution stack) is a data structure that operates

in the background to enable the method call/return mechanism. It also allows for the construction,

maintenance, and removal of the local variables of each called method. (Deitel, 2016)

The running Java program's call stack is represented by three interfaces: Frame, which encapsulates

the data stored in a single stack frame, such as the operand stack and local variables; FrameSource, which

encapsulates the allocation and layout of Frames, controlling things like the argument-passing mechanism;

and Context, which encapsulates the call stack storage and management as well as the locking logic

required by synchronized methods.

In this instance, I will show how method call stack works by this small problem by calculating the

factorial result of a specific number. For the number that equals to one, we will let the program

automatically calculate the factorial result to one and from that we will calculate more on the next bigger

numbers. For example, for Factorial(2), it will equal to 2 * Factorial(1) so we will take the result from the

previous Factorial to calculate and then so on for three and four.

Whenever a user performs an action, capture it and move it to the bottom of the list.

Take the final item on the list when UNDO is issued (essentially, use it a stack, push actions when

user takes one, pop actions when undo is issued)

Using the recorded data, restore the state to what it was before the action was made.

Of course, each application will necessitate a very different concrete implementation.

Because it is often assumed that:

  • We undo activities in the reverse order in which they were performed
  • We redo actions that we undo in the same manner.

A stack, rather than a double-ended queue, is the standard data structure for storing undo and redo

information. Of course, you'd utilize different stacks for undo and redo data, and simply move objects

between them, i.e.

  • Every new action you do is added to the undo stack
  • Every undo removes an item from the undo stack and adds it to the redo stack
  • Every redo removes an item from the redo stack and adds it to the undo stack.

However, by simply adding and deleting items from the same end of the queue, you may easily utilize

a deque to imitate a stack.

Using a deque for the undo stack has the extra benefit of allowing you to trim the stack when it

becomes too big (e.g., you may want to limit undoes to the last 1,000 actions). Trimming the stack simply

implies deleting things from the opposite end of the stack from where they were inserted.

Figure 13 : TWO CHAR STACKS FOR THE PROGRAM

  • WRITE : Write a character X into the document.

Figure 14 : CODE FOR WRITE FUNCTION

  • UNDO : Erases the last change made to the document.

Figure 15 : CODE FOR UNDO FUNCTION

  • REDO : Restores the most recent UNDO operation performed on the document.

Figure 16 : CODE FOR REDO FUNCTION

  • READ : Reads and prints the contents of the documents.

Figure 19 : CODE FOR THE MAIN FUNCTION

AB ABC

Figure 20 : RESULT FOR THE ABOVE PROBLEM

B. References

Anon., 2002. Usenix. [Online]

Available at: www.usenix.org

[Accessed 14 April 2022].

Anon., 2017. ICARUS. [Online]

Available at: icarus.cs.weber.edu

[Accessed 14 April 2022].

Datta, S., 2021. Baeldung. [Online]

Available at: www.baeldung.com

[Accessed 11 April 2022].

Deitel, P., 2016. Method-Call Stack. In: C# 6 for Programmers, 6th Edition. s.l.:Pearson.

Desai, A., 2019. Opengenus. [Online]

Available at: iq.opengenus.org

[Accessed 15 April 2022].

Mehta, K., 2022. Scaler. [Online]

Available at: www.scaler.com

[Accessed 15 April 2022].

Poornima, S., 2014. Memory Management by Using the Heap and the Stack in Java, Warangal: s.n.