1. Trang chủ
  2. » Luận Văn - Báo Cáo

1649_Data Structures & Algorithms_Greenwichvn_Asm1 (1).Docx

14 1 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Table of Contents LIST OF FIGURE 4 I INTRODUCTION 5 II CREATE A DESIGN SPECIFICATION FOR DATA STRUCTURES EXPLAINING THE VALID OPERATIONS THAT CAN BE CARRIED OUT ON THE STRUCTURES 5 1 STACK ADT 5 2 QUE[.]

Table of Contents LIST OF FIGURE: I INTRODUCTION: II CREATE A DESIGN SPECIFICATION FOR DATA STRUCTURES EXPLAINING THE VALID OPERATIONS THAT CAN BE CARRIED OUT ON THE STRUCTURES: STACK ADT: QUEUE ADT: III DETERMINE THE OPERATIONS OF A MEMORY STACK AND HOW IT IS USED TO IMPLEMENT FUNCTION CALLS IN A COMPUTER: 13 OPERATION OF MEMORY STACK: .13 HOW STACK USED TO IMPLEMENT FUNCTION CALLS IN A COMPUTER: 14 IV USING AN IMPERATIVE DEFINITION, SPECIFY THE ABSTRACT DATA TYPE FOR A SOFTWARE STACK: 16 QUEUE: 16 STACK: 16 V CONCLUSION: 17 REFERENCES 17 LIST OF FIGURE: Figure Stack ADT(tutorialspoint) .6 Figure Peek() example Figure isFull() example .7 Figure isEmpty() example Figure push() (tutorialspoint) Figure push() example Figure pop() (tutorialspoint) .9 Figure pop() (example) .9 Figure Queue ADT (tutorialspoint) 10 Figure 10 isFull() example 10 Figure 11 isEmpty() example 11 Figure 12 Front and rear (tutorialspoint) 11 Figure 13 Enqueue() (tutorialspoint) 12 Figure 14 Enqueue() example 12 Figure 15 Dequeue() (tutorialspoint) 13 Figure 16 Dequeue() example 13 Figure 17 Memory stack (ScienceDirect) 14 Figure 18 Function call (Zeyuan Hu's page) 15 I INTRODUCTION: This report will create design specifications for the data structure that explains the valid operations that can be performed on the structure, defining the memory stack's operations, and how it is used to perform these actions function call in the computer and use the command definition, specifying an abstract data type for the software stack II CREATE A DESIGN SPECIFICATION FOR DATA STRUCTURES EXPLAINING THE VALID OPERATIONS THAT CAN BE CARRIED OUT ON THE STRUCTURES: STACK ADT: A Stack is an Abstract Data Type (ADT), commonly used in most programming languages It is named the stack because it behaves like a real-world stack A stack can be implemented using Arrays, Structures, Cursors and Linked Lists Stacks can be fixed size or can be dynamically resizable Here, we will implement the stack using arrays, which makes it a fixed-size stack implementation (tutorialspoint, 2021) LIFO stands for Last-in-first-out Here the element is placed (inserted or added) last, accessed first In stack terminology, the insert operation is called a "PUSH" operation, and the remove operation is called a “Pop” operation (tutorialspoint, 2021) Figure Stack ADT(tutorialspoint) A Stack contains elements of the same type arranged in sequential order All operations take place at a single end that is top of the stack and following operations can be performed (tutorialspoint, 2021): Push( item i ): Insert an element at one end of the stack called top Pop( ): Remove and return the element at the top of the stack, if it is not empty Peek( ): Return the element at the top of the stack without removing it, if the stack is not empty Size( ): Return the number of elements in the stack Isempty( ): Return true if the stack is empty, otherwise return false Isfull( ): Return true if the stack is full, otherwise return false STACK OPERATIONS:  Peek(): Get the top data element of the stack, without removing it (tutorialspoint, 2021) Implementation of this algorithm in Java: Figure Peek() example  IsFull(): Check if stack is full or not then returns a boolean value (tutorialspoint, 2021) Implementation of this algorithm in Java: Figure isFull() example  isEmpty(): Returns true if the stack is empty, otherwise false Since the index in the array starts at 0, so that it returns the value "True" only if the highest value is at "-1" (tutorialspoint, 2021) Implementation of this algorithm in Java: Figure isEmpty() example  Push(item i): Insert an element at one end of the stack called top The process of inserting a new data element onto the stack is called the Push activity It consists of main steps (tutorialspoint, 2021) Check if the stack is full If the stack is full, generate an error and exit the program If the stack is not full, increments “top” to point next empty space Add data elements to the pointing stack Return results on success Figure push() (tutorialspoint) Implementation of this algorithm in Java: Figure push() example  Pop(): Removing and returning the element at the top of the stack, if it is not empty, is called the Enable operation During the array implementation of the pop () operation, the data element is not actually removed Instead, the top element is pushed down a position in the stack to point to the next value This is different from a linked list implementation It consists of main steps (tutorialspoint, 2021): Checks if the stack is empty If the stack is empty, produces an error and exit If the stack is not empty, accesses the data element at which “top” is pointing Decreases the value of top by Return results on success Figure pop() (tutorialspoint) Implementation of this algorithm in Java: Figure pop() (example) QUEUE ADT: Unlike a stack, a queue is opened at both ends of it Queues containing elements of the same type are arranged in an ordered sequence Operation takes place at both ends, one end is always used to insert data (enqueue) and the other end is used to delete data (dequeue) Just like in a stack, queues can also be implemented using Arrays, Linked Lists, Pointers, and Structures For simplicity, we will implement queues using one-dimensional arrays (tutorialspoint, 2021) Figure Queue ADT (tutorialspoint) Queue operations may involve creating or defining the queue, using it, and then removing it completely from memory Some of the following actions can be done (tutorialspoint, 2021): enqueue(item i) – Insert an element at the end of the queue dequeue() – Remove and return the first element of the queue, if the queue is not empty isEmpty() – Return true if the queue is empty, otherwise return false isFull() – Return true if the queue is full, otherwise return false Front() and rear()  isFull(): Since we are using a one-dimensional array for the implementation of the queue So we just need to check if the back pointer reaches MAXSIZE or not The purpose is to determine that the queue is full In case we maintain the queue in a circular linked list, the algorithm will be different (tutorialspoint, 2021) Implementation of this algorithm in Java: Figure 10 isFull() example  isEmpty(): If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty (tutorialspoint, 2021) Implementation of this algorithm in Java: Figure 11 isEmpty() example  Front() and rear(): Items are removed from a queue at one end, called the FRONT of the queue And elements are added at the other end, called the REAR (tutorialspoint, 2021) Figure 12 Front and rear (tutorialspoint)  Enqueue(item i): Queues maintain two data pointers, “front” and “rear” Hence, its operations (Inserting an element at the end of the queue) are more difficult to perform than the stack operations Here are the steps to put (insert) data into the queue (tutorialspoint, 2021): Check if the queue is full If the queue is full, produce overflow error and exit If the queue is not full, increment rear pointer to point the next empty space Add data element to the queue location, where the rear is pointing Return results on success Figure 13 Enqueue() (tutorialspoint) Implementation of this algorithm in Java: Figure 14 Enqueue() example  Dequeue(): Accessing data from a queue is a process consisting of two tasks - accessing data where forwards points to and deleting data after the access That is, remove and return the first element of the queue, if the queue is not empty Here are the steps to perform the dequeue operation (tutorialspoint, 2021): Check if the queue is empty If the queue is empty, produce underflow error and exit If the queue is not empty, access the data where “front” is pointing Increment “front” pointer to point to the next available data element Return results on success Figure 15 Dequeue() (tutorialspoint) Implementation of this algorithm in Java: Figure 16 Dequeue() example III DETERMINE THE OPERATIONS OF A MEMORY STACK AND HOW IT IS USED TO IMPLEMENT FUNCTION CALLS IN A COMPUTER: OPERATION OF MEMORY STACK: Stack memory is a memory usage mechanism that allows the system memory to be used as temporary data storage that behaves as a first-in-last-out buffer One of the essential elements of stack memory operation is a register called the Stack Pointer The stack pointer indicates where the current stack memory location is, and is adjusted automatically each time a stack operation is carried out (ScienceDirect, 2015) Stack memory is sequential memory with specific access limits that allow writing and reading information to / from this location in memory called the "top" of the stack Stack memory acts as a queue where data can be written to the top of the stack, simultaneously inducing the movement of all information stored in the next location to one location deep in this queue Reading from the stack is only possible from the top of the stack The stack memory works in a similar way as the magazine of a rifle, in which introduction of a new cartridge pushes all remaining cartages in depth of the magazine and the use of a cartridge (a shot) makes the top cartridge be removed and replaced by the first cartridge residing under the used one (ScienceDirect, 2015) The three main instructions used for stack manipulation are push (write data into the stack), pop (remove data from the top of the stack with a shift of its contents up) and read stack (read the top of the stack) (ScienceDirect, 2015) Figure 17 Memory stack (ScienceDirect) HOW STACK USED TO IMPLEMENT FUNCTION CALLS IN A COMPUTER: Each computer program that runs uses a region of memory called the stack to enable functions to work properly Machine uses the stack to pass function arguments, to store return information, to save registers for later restoration, and for local variables The portion of the stack allocated for a single function call is called a stack frame In other words, for each function call, new space (i.e., stack frame) is created on the stack (Hu, 2017) The computer's stack lives at the very top addresses of memory As the name suggests, stack is a stack data structure with the "top" of the stack growing from the high value addresses towards low values addresses We use push S to push the source onto stack, and we use pop D to remove the top value from the stack and place it into a destination (i.e a register or memory location) (Hu, 2017) When we talk about function calls, what we really care about is the topmost stack frame because that's the memory region that is associated with our current function calls: Figure 18 Function call (Zeyuan Hu's page) Calling a function: Before executing a function, a program pushes all of the parameters for the function onto the stack in the reverse order that they are documented Then the program issues a call instruction indicating which function it wishes to start (Hu, 2017) When you call a function, you should assume that everything currently in your registers will be wiped out Thus, if there are registers you want to save before calling a function, you need to save them by pushing them on the stack before pushing the function's parameters (Hu, 2017) IV USING AN IMPERATIVE DEFINITION, SPECIFY ABSTRACT DATA TYPE FOR A SOFTWARE STACK: THE QUEUE: Queue (q); size = size(q); f = front(s); r = rear(s) Operation front() return first item rear() return last item Pre-conditon isEmpty(q) = = false isEmpty(q) = = false Post-condition Error exception y = front(), isEmpty(q) = = true y==f queue underflow y = rear(), isEmpty(q) = = true y==r queue underflow isFull(): Boolean None size(q) = = Max_Capacity None isEmpty(): Boolean None size(q) = = None size(q) = = size + 1, isFull(q) = = true rear(q) = i queue overflow size(q) = = size – 1, isEmpty(q) = = true y = dequeue (), y = = f queue underflow Enqueue(item i): integer (Add to the end of the isFull(q) = = false queue.) Dequeue(): integer (Delete from the isEmpty(q) = = false beginning of the queue.) STACK: Stack (s); size = size(s); t = top(s) Operation Peek(): Integer (Return an item) Pre-conditon isEmpty(s) = = false, Post-condition Error exception y = peek(), isEmpty(s) = = true y==t stack underflow isFull(): Boolean None size(s) = = Max_Capacity None isEmpty(): Boolean None size(s)

Ngày đăng: 01/04/2023, 18:37

w