Docsity

Data Structures and Algorithms: BTEC HND Diploma Unit 19 Assignment, Study Guides, Projects, Research of Data Structures and Algorithms

Toggle sidebar
bg1
ASSIGNMENT 1 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
Phan Nguyen Dinh Trong
Student ID
GCD201526
Class
GCD0905
Assessor name
Pham Thanh Son
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
Trong
Grading grid
P1
P2
P3
M1
M2
M3
D1
D2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Data Structures and Algorithms: BTEC HND Diploma Unit 19 Assignment and more Study Guides, Projects, Research 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 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Phan Nguyen Dinh Trong Student ID GCD Class GCD0905 Assessor name Pham Thanh Son 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 Trong Grading grid P1 P2 P3 M1 M2 M3 D1 D

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:

  • Table Of Figures
  • Chapter I: Abstract data type
    • 1.1 Definition..............................................................................................................................................
    • 1.2 Examples
        1. Add a new element at the beginning of the list:
        1. Add a new element to the end of the list:
        1. Remove the first node:
        1. Remove the last node:
  • Chapter II: ADT usages
      1. Application of Stack in memory
      • 1.1 How the memory is organized........................................................................................................
      • 1.2 The implementation of a function call using the stack
  • Chapter III: Application of an ADT
      1. The problem that we need an ADT to solve
      1. circular linked list
  • References
  • Figure 1 Abstract data types Table Of Figures
  • Figure 2 Singly linked list
  • Figure 3 Add first in a singly linked list
  • Figure 4 Add first in a singly linked list
  • Figure 5 Code Singly Linked List
  • Figure 6 Code Singly Linked List
  • Figure 7 Create a new node
  • Figure 8 Insert a node at the end
  • Figure 9 Code adding a node at the end
  • Figure 10 Remove the first node
  • Figure 11 Code removing the first node
  • Figure 12 : remove the last node
  • Figure 13 Code removing the last node
  • Figure 14 Code of display the singly linked list
  • Figure 15 Code of testing the singly linked list............................................................................................
  • Figure 16 Result of testing singly linked list
  • Figure 17 How to operate data in the stack
  • Figure 18 How many stack frames are designed
  • Figure 19 Example of function calls
  • Figure 20 Example of function calls
  • Figure 21 Circular linked list........................................................................................................................
  • Figure 22 Inserting operation
  • Figure 23 Traverse method...........................................................................................................................
  • Figure 24 Main method
  • Figure 25 result of above game

Chapter I: Abstract data type

1.1 Definition

In programming, the term "abstract" is used to conceal fundamental information or any implementation of superfluous data so that the user only sees what is required. One of the most significant and fundamental aspects of object-oriented programming is this. An object whose behavior is determined by a set of values and a set of actions is known as an abstract data type (ADT). The ADT specification merely describes the tasks that must be completed; it makes no mention of how they must be carried out. It is not specified what algorithm will be utilized to carry out the operations or how the data will be structured in memory. Because it offers an implementation independent view, it is dubbed "abstract." ArrayList : A list is an ordered linear collection of data elements, each piece of which has a fixed location within the list. The order could simply be the order in which the list members were added, or it could be alphabetical, numerical, or both. The components of a list do not need to be unique, in contrast to a set. Items that hold the data for an object known as a node can be found in the ADT list. List ADT functions:  get(): Return an element from the list at the specified position.  insert(): Insert an element at any position of the list.  remove(): Remove the first occurrence of any element from a non-empty list.  removeAt(): Remove an element at the specified position from a non-empty list.  replace(): This replaces any element with the given element in the list. Figure 1 Abstract data types

 Searching(): this function will return the location of the given element. Here's how the single linked list works: First of all we have 3 nodes:

  1. Add a new element at the beginning of the list: Step 1 : Creating a new node. Step 2 : By first setting the next node of the new node equal to the head, and then setting the head equal to the new node, you can join the list with the new node. Figure 2 Singly linked list Figure 3 Add first in a singly linked list

Code: Figure 4 Add first in a singly linked list Figure 5 Code Singly Linked List

Figure 8 Insert a node at the end Code:

  1. Remove the first node: Step 1: The following node of the current head node must be assigned to a temporary node. Step 2: The head node is set to point to null, and the head is assigned to the temporary node above. Figure 9 Code adding a node at the end

Code:

  1. Remove the last node: Step 1: The temporary node must be assigned as the head node's equal. Step 2: Using a while-loop statement, connect the temporary node above to the previous node of the tail node. Step 3: Assign the tail to the previous node of the tail node, which should now point to null. Figure 10 Remove the first node Figure 11 Code removing the first node

Result of running the program: Figure 15 Code of testing the singly linked list Figure 14 Code of display the singly linked list

Application of Singly Linked List:  Stacks and queues can be implemented using linked lists.  A linked list can also be used to perform arithmetic operations on long integers, such as addition and subtraction.  On our laptops or PCs, we view the pictures, and we can quickly access the previous or next picture. A linked list is used to implement this function.  We can make a song playlist in music players and choose to play a song from the beginning or end of the list. And a linked list is used to implement these music players.

Chapter II: ADT usages

1. Application of Stack in memory

1.1 How the memory is organized

Stack memory is a memory management technique that enables system memory to be used as a last-in, first-out buffer for temporary data storage. An essential part of stack memory operation is a register known as the Stack Pointer. Every time a stack action is carried out, the stack pointer, which displays the memory address of the most recent data element added to the stack, is immediately updated. Basically, the stack will use the Last-In-First-Out data structure to store data from the top down. This implies that data is added to a stack at the top and that data is also subtracted from the top. A push operation and a pop operation will be used to add and remove data from a stack, respectively. When a new element is added to the stack, it will be stacked on top of the existing element and become the top element since the push action adds new data to the top of a stack. In order for the element below the removed element to become the top element, the pop operation removes the top element from the stack. Figure 16 Result of testing singly linked list

Features :  The memory allocated for a function call in the stack lives only the time when the function is executing once the function gets completed we can’t access the variables of that function.  Once the calling function completes its execution its stack frame is removed and the thread of execution of called function resumes from that position where it was left.  Stack is used for storing function calls so in the case when we are using a lot of recursive calls in our program the stack memory gets exhausted by the function calls or subroutines which may result in stack overflow because the stack memory is limited.  Each stack frame maintains the Stack Pointer (SP), and the frame pointer (FP). Stack pointer and frame pointer always point to the top of the stack. It also maintains a program counter (PC) which points to the next instruction to be executed.  Whenever a function call is made a stack frame is created in the stack segment the arguments that were given by the calling function get some memory in the stack frame of called function, and they get pushed into the stack frame of the called function. When their execution is finished they get popped from the stack frame. And the thread of execution gets continues in the called function. Example of function calls with the stack: Figure 19 Example of function calls

Figure 20 Example of function calls

Chapter III: Application of an ADT

1. The problem that we need an ADT to solve

I am currently working on a two player checkerboard game. I discovered that ring linked lists can solve this problem because the time-based gameplay in this game requires other players to wait for their turn. Each player will play once in each turn, and a new turn will begin when the previous turn is over. And the first player will now start the next turn. Until a winner is determined or there have been more than 6 turns, the game continues.

2. circular linked list

A circular linked list is a list in which every node is connected in a circle. In a circular linked list, a circle is formed by the connections between the first and last node. Finally, there are no NULLs. Thus, the problem of players switching between plays can be solved. Code:

Figure 22 Inserting operation We must first build a new node before determining whether the list is empty or not. If the list is empty, the head and tail of the list will be assigned to the new node, and the head of the new node will be assigned to the next node. If the list has no empty elements, we will make the new node the tail by making the next node in the tail point at it. The new tail's subsequent node will point toward the head. we will create a method to visit all nodes: Figure 23 Traverse method

First, we will add players. Then the traver() method is implemented to start the gameplay. Result: The outcome is exactly what we wanted; the game will allow for two players to have six turns. Thus, until the conclusion of the final turn, the winner will be determined. Figure 24 Main method Figure 25 result of above game