Docsity

Data Structures & Algorithms: Practical Guide with Linked Lists & Stack Memory, Study Guides, Projects, Research of Information Technology

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
Nguyen Duc Thien
Student ID
GCD201512
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
Thien
Grading grid
P1
P2
P3
M1
M2
M3
D1
D2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Data Structures & Algorithms: Practical Guide with Linked Lists & Stack Memory and more Study Guides, Projects, Research Information Technology 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 Nguyen Duc Thien 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 Thien Grading grid P1 P2 P3 M1 M2 M3 D1 D

 Summative Feedback:  Resubmission Feedback:

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

  • Chapter I: Abstract data type Table of Contents
      1. Definition
      • 1.1 Definition of Abstract Data Type
      • 1.2 Common ways to represent ADT
      1. Examples
  • Chapter II: ADT usages
      1. Application of Stack in memory
      • 1.1 How the memory is organized
      • 1.2 How a function calls is implemented with the stack
  • Chapter III: Application of an ADT
      1. The problem that we need an ADT to solve
      1. Why does a Circular Linked List can solve the above problem?
  • References
  • Figure 1: Abstract data type 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: Insert after a node
  • Figure 8: Insert after a node
  • Figure 9: Code inserting a node after a node
  • Figure 10: Insert a node at the end
  • Figure 11: Insert a node at the end
  • Figure 12: Code adding a node at the end..................................................................................................................
  • Figure 13: Remove the first node
  • Figure 14: Code removing the first node
  • Figure 15: remove the last node
  • Figure 16: Code removing the last node
  • Figure 17: Remove a node after a node
  • Figure 18: Code snippet of testing the singly linked list
  • Figure 19: Result of testing singly linked list...............................................................................................................
  • Figure 20: How data is organized in a stack................................................................................................................
  • Figure 21: How many stack frames are designed
  • Figure 22: Example of function calls
  • Figure 23: Example of function calls
  • Figure 24: Illustrate with stack frame
  • Figure 25: Circular linked list.......................................................................................................................................
  • Figure 26: Inserting operation
  • Figure 27: Removing operation
  • Figure 28: Traversing method
  • Figure 29: Class Player
  • Figure 30: Example of a game
  • Figure 31: result of above game

Chapter I: Abstract data type

1. Definition

1.1 Definition of Abstract Data Type

Before considering the definition of Abstract Data Type (ADT), we need to understand what abstract is. The concept of "abstraction" in programming is a feature that allows programmers to consider the high-level characteristics of an object without going deep into the details. Similar to abstract, an abstract data type is a data structure abstraction that only provides the interface to which the data structure must adhere. The interface provides no specifics about how something should be implemented or in what programming language. In general, an ADT will define the behavior of an object by a set of properties and operations. These properties will be hidden from users and can be used only by operations. Figure 1 : Abstract data type

1.2 Common ways to represent ADT

Generally, there are some common ADT: ArrayList: a list is a linear collection of data items that are ordered in some way, with each element occupying a specific position in the list. The order could be alphabetical, numeric, or simply the order in which the list elements were added. In contrast to a set, the elements of a list do not have to be unique. In the ADT list, there are items that contain the data of an object known as nodes. List ADT functions :

One of the implementations of the ADT list can mention is the Singly Linked List. Normally, the Singly Linked List is a linear data structure where each element is a separate node. A Singly Linked List will contain a collection of nodes that are connected with only the next pointer in the list. These nodes have two fields: data stored at that specific address and a pointer containing the address of the next node in memory. Operations on Singly Linked List:

  • Insertion(): this function inserts a new element into the list.
  • Deletion(): this function removes an element in the list.
  • Traversing(): this function prints all nodes in the list.
  • Searching(): this function will return the location of the given element. How the Singly Linked List works : Firstly, we have a list with 3 nodes as below: Figure 2 : Singly Linked List How to add a new element at the beginning of the list: Step 1: Creating a new node.

Figure 3 : Add first in a singly linked list Step 2: Connect the new node with the list by assigning the next node of the new node equal to the head and then assigning the head equal to the new node. Figure 4 : Add first in a singly linked list Code snippet:

Step 1: Create a new node and reach the desired node by a loop statement. Figure 7 : Insert after a node Step 2: Make the new node point to the next element of the current node and make the current node point to the new node. Figure 8 : Insert after a node Code snippet:

Figure 9 : Code inserting a node after a node How to add a new node at the end: Step 1: Creating a new node. Figure 10 : Insert a node at the end Step 2: Make the tail node point to the new node and assign the tail to the new node.

Figure 13 : Remove the first node Code snippet: Figure 14 : Code removing the first node How to remove the last node: Step 1: we need to assign a temporary node equal to the head node. Step 2: Using a while-loop statement to reach the previous node of the tail node through the above temporary node. Step 3: Make the previous node of the tail node point to null and assign the tail to it.

Figure 15 : remove the last node Figure 16 : Code removing the last node How to remove a node after another node: Step 1: Using a loop statement to reach the desired node. Step 2: Assign the previous node of the current node point to the next node of the current node. Step 3: Assign the next node of the current node to null.

Figure 19 : Result of testing singly linked list Application of Singly Linked List:

  • Act as a base for certain data structures like Queue, Stack, and Graph.
  • Keep track of free space in the secondary disk. All the free spaces can be linked together.
  • To keep track of items such as music, videos, images, web pages, and so on that are linked to one another and can be traversed sequentially.
  • Previous and next page in a web browser - Because the URLs are linked as a linked list, we can access the previous and next URLs by pressing the back and next buttons.

Chapter II: ADT usages

1. Application of Stack in memory

1.1 How the memory is organized

Stack memory is a memory management mechanism that allows system memory to be used as temporary data storage, acting as a last-in-first-out buffer. A register known as the Stack Pointer is a fundamental component of stack memory operation. The stack pointer indicates the memory address of the last data element added to the stack and is automatically updated each time a stack action is performed. Basically, the stack will store data from the top down through the Last-In-First-Out data structure. This means that new data will be stored at the top of a stack and data is also removed from the top of the stack.

Adding and removing data from a stack will be performed through a push operation and a pop operation. The push operation works by adding new data to the top of a stack, so when a new element is added to the stack, it will be placed on top of the current element and become the top element. The pop operation works by removing the top element of a stack from it, so the element below the removed element will become the top element. Figure 20 : How data is organized in a stack

1.2 How a function calls is implemented with the stack

When a function call is performed within a program, a stack frame is typically constructed on the stack to handle the function's data. Each function creates its own stack frame, which serves as a logical framework for storing the function's parameters, local variables, and other data. The figure below shows a conceptual overview of how many stack frames are constructed. The data from the current stack frame, which is connected with the most recently called function, is stored at the top of the stack. A second stack frame is located below the current stack frame, and it contains the function known as the current function. It was the final active stack frame before the function call was made.

Figure 22 : Example of function calls Result: Figure 23 : Example of function calls

As we see, the main function is performed first and it also exit finally. Whereas, the methodC is performed final, but it exits firstly. This means that method calls also follow the Last-In-First-Out principle like the stack. Illustrating the above example with stack frame: Figure 24 : Illustrate with stack frame

Chapter III: Application of an ADT

1. The problem that we need an ADT to solve

Currently, I am developing an online card game where up to 4 players can play at the same time. This game will be a turn-based type which means while a player is playing, other players have to wait for their turn. During each turn, each player will be played once, and at the end of a turn, a new turn will begin again. And the new turn will start over at the first player. The game will end until the game finds a winner or the game is over 4 turns. For this problem, I noticed that a Circular Linked List can solve it.

2. Why does a Circular Linked List can solve the above problem?

We know that a circular linked list is a collection of nodes in which the tail node also points back to the head node, completing a circle of nodes. With the circular linked list, any node can be the head node of the list. As a result, traversing from the last node to the head node can be done in constant time, which means traversing among nodes is a linear operation. With this characteristic of Circular Linked List, the problem of restarting the turn from the first player will be solved. Code snippet: Firstly, we need to declare the necessary properties for the circular linked list: