Docsity

Data structure & Algorithms, Assignments of Information Technology

Toggle sidebar
bg1
1
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
Dinh Duc Thuan
Student ID
GCD18677
Class
GCD0705
Assessor name
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
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 structure & Algorithms and more Assignments 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 Dinh Duc Thuan Student ID GCD Class GCD0705 Assessor name 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 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 Contents

  • Dinh Duc Thuan-GCD
    • Class: GCD
  • ASSIGNMENT
  • P1
  • structures........................................................................................................................................................................ I. Create a design specification for data structures explaining the valid operations that can be carried out on the
      1. What is Queue?
      1. Specify Queue data structures including Data type and its operations.
      • a) Queue data structure
      • b) Operation
      • c) Real life example
  • P2
  • II. Determine the operations of a memory stack and how it is used to implement function calls in a computer.
      1. How a memory stack is used to implement function calls in a computer?
      • a) Stack memory in computers
      • b) Stack implementation the computer
      • c) Function call
  • P3
  • III. Using an imperative definition, specify the abstract data type for a software stack.
      1. What is Stack?
      1. Specify Stack data structures including Data type and its operations
      • a) Stack data Structure
      • b) Operation
      • c) Implementation
      • d) Real life example
  • IV. Reference

P

I. Create a design specification for data structures explaining the valid operations that can be carried out on the structures.

1. What is Queue?

  • Queue is a data structure designed to work in the sense of FIFO (First in First out). Elements from the rear end are placed in queue and removed from the front end.
  • Queue class is an adapter to containers. Container is an object of the same type which holds data. Queue can be formed from containers of different sequences. Therefore, container adapters do not allow iterators so we cannot use them for manipulating data. However, they support member functions push () and pop () (for insertion and deletion of data, respectively. Figure 1 Queue

b) Operation Enqueue:

  • Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively difficult to implement than that of stacks.
  • The following steps should be taken to enqueue (insert) data into a queue
    • Step 1 − Check if the queue is full.
    • Step 2 − If the queue is full, produce overflow error and exit.
    • Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
    • Step 4 − Add data element to the queue location, where the rear is pointing.
    • Step 5 − return success. Figure 3 Enqueue
  • Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen situations.

Dequeue

  • Accessing data from the queue is a process of two tasks − access the data where front is pointing and remove the data after access. The following steps are taken to perform dequeue operation −
    • Step 1 − Check if the queue is empty.
    • Step 2 − If the queue is empty, produce underflow error and exit.
    • Step 3 − If the queue is not empty, access the data where front is pointing.
    • Step 4 − Increment front pointer to point to the next available data element.
    • Step 5 − Return success. Figure 4 Dequeue

Queue Code – Input: class Queue { public: int Max; int Q[ 100 ; int front, last; Queue () { front = 0 ; last = 0 ; Max = 100 ; } bool isEmpty () { if ( isEmpty()) cout<<"khong lay duoc nua"; else { int x = Q[front] ; front = (front + 1 ); } return last == front; } void enqueue ( int x () { if (soPhanTu() == Max + 1 ) cout<<"da day"; else Q[last] = x; last = (last + 1 ) % Max; last = (last + 1 ) % Max; Q[last] = x; }

int dequeue () { if (isEmpty()) cout<<""; return - 1 ; int x = Q[front] ; front = (front + 1 ) % Max; return x ; } int peek () { return Q[front] ; } int soPhanTu () { return ((Max - front) + last) % Max; } }; Output : Figure 6 Result of programming

  • Sometimes arithmetic operations are related to the two top locations of the stack used as arguments and the location of the result. Following a mere arithmetic operation, the pop operation is done and the result is visible at the top of the stack. Figure 7 Stack functioning principles

b) Stack implementation the computer

  • The first stack architectures were based on the use of registers where data is transferred in parallel during Push and Pop operations.
  • Contemporary stack implementations are based on the use of main memory supported by special registers that make it easy to access and control the stack. The stack is reserved for the operating system, a continuous series of sequential memory positions. The boundary of the stack is defined by two separate registers-the stack base register and the stack boundary register comprising the upper and lower boundary stack addresses. The address space between such addresses is prohibited from usage other than for the implementation of the stack. The top of the stack is calculated by the address holder in the third register called the stack pointer register.
  • Once a machine begins running the program, the contents of the stack base register are set to the top of the stack register. During each Push operation, the contents of the stack pointer register are modified by the byte number corresponding to the stack term in the direction of the stack cap. During every Pop procedure, the contents of the stack pointer are changed in the direction of the stack center. Figure 8 Stack implementation the computer - the top of the stack in the memory

c) Function call

  • when a method is named, an activation record (AR, also named a Stack Frame) is allocated to it.
  • The record typically contains the following information:
    • The parameters and local variables used in the process called
    • A guide to the caller activation log.
    • Return location, the location of the caller 's guidance directly after the call.
    • Return meaning for process not declared null and void: As the size of the activation record varies with various method calls, the returned value is put right above the caller's activation record.
  • Every new activation record is put at the top of the run-time stack.
  • When the process ends, its activation record is removed from the top of the runtime stack.
  • Thus, the first AR put on the stack is excluded from the last one. **P III. Using an imperative definition, specify the abstract data type for a software stack.
  1. What is Stack?**
  • The distinguishng characteristic of a stack is that the insertion or removal of elements takes place at the same end.
  • Stack contains a set of elements, but different from the Array or Set, such elements can only be accessed from one end of the stack. It is commonly known that this end is the top of the stack.
  • The theory by which the stack is ordered shall be called LIFO (last-in first-out).
  • The final element to be placed in the stack will be the first element removed from the stack.
  • It's easy to view the Stack as a pile of books, the last book on the top of the stack is the easiest book to pick from the stack. Figure 10 Stack representation

2. Specify Stack data structures including Data type and its operations a) Stack data Structure

  • Stack is an abstract data form with a small (predefined) power. It is a basic data structure that allows elements to be inserted and removed in a given order. Each time an element is inserted, it moves to the top of the stack, and the only element that can be deleted is the element that is at the top of the stack, much like a stack of objects. Figure 11 Stack Data Structure

d) Real life example

  • In this case , the following items need to be considered:
    • There's a bucket that holds the balls.
    • There are various kinds of balls in the bucket.
    • The ball that continues to reach the bucket will be pulled out first.
    • The ball entering the bucket closest to the last one will be pulled out of the ball above it (the older one).
    • In this way, the ball reaching the bucket will leave the bucket for the last time.
    • Therefore, the Last Ball (Blue) to enter the bucket is removed first, and the First Ball (Red) to enter the bucket is removed last. Figure 12 Example

Stack Code - Input: class Stack { //data public : int Max; int Top; int S [ 100 ]; //operation public : Stack () { Max = 100 ; Top = - 1 ; } bool isEmpty () { return (Top == - 1 ); } void push ( int x){ Top++; S[Top] = x; } int pop (){ int x = S[Top]; Top--; return x; } int top (){ return S[Top]; } void convert ( int x){ while (x != 0 ) { int r = x % 2 ; push(r) ; x = x/ 2 ; } while (! isEmpty ()) { Cout<<pop(); } } };