


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A. Examine abstract data types, concrete data structures and algorithms I. Create a design specification for data structures explaining the valid operations that can be carried out on the structures. 1. Stack: Definition: a stack presents a sequence of objects or elements in a linear data structure format. The stack consists of a bounded bottom and all the operations are carried out on the top position. Whenever an element is added to the stack by the push operations, the top value is incremented by one, and when an element is popped out from the stack, the top value is decremented by one. A pointer to the top position of the stack is also known as the stack pointer. A stack may be fixed in size or may have dynamic implementation where the size is allowed to change. Om the case of the bounded capacity stacks, trying to add an element to an already full stack causes a stack overflow exception. Similarly, a condition where a pop operation tries to remove an element from an already empty
Typology: Exercises
1 / 42
This page cannot be seen from the preview
Don't miss anything!



































Qualification BTEC Level 5 HND Diploma in Business Unit number and title Unit 20: Algorithms and Data Structure Submission date Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Võ Lâm Tr ường Student ID FB Class BSAI-17081 Assessor name Nguy ễn Hùng M ạnh 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
A. Examine abstract data types, concrete data structures and algorithms I. Create a design specification for data structures explaining the valid operations that can be carried out on the structures.
1. Stack: Definition: a stack presents a sequence of objects or elements in a linear data structure format. The stack consists of a bounded bottom and all the operations are carried out on the top position. Whenever an element is added to the stack by the push operations, the top value is incremented by one, and when an element is popped out from the stack, the top value is decremented by one. A pointer to the top position of the stack is also known as the stack pointer. A stack may be fixed in size or may have dynamic implementation where the size is allowed to change. Om the case of the bounded capacity stacks, trying to add an element to an already full stack causes a stack overflow exception. Similarly, a condition where a pop operation tries to remove an element from an already empty stack is known as underflow Basic stack operator: A stack is considered to be a restricted data structure as only a limited of operations are allowed. Beside the push and pop operations, certain implementations may allow for advanced operations such as: Peek: View the topmost item in the stack. Duplicate: Copy the top item’s value into a variable and push it back into the stack.
Swap: Swap the two topmost items in the stack. Rotate: Move the topmost elements in the stack as specified by a number or move in a rotating fashion. Software implementation of the stack concept are done using arrays and linked lists where the top position is tracked using a variable or header pointer respectively. Many programming languages provide built-in features to support stack implementation. Hardware stacks are implementation for the purpose of memory allocations and access using a fixed origin and size. Stack registers are used to store the value of the stack pointer.
2. Queue: Definition: Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (endqueue) and the other is used to remove data (dequeue). Queue follows First-In- First-Out methodology, i.e., the data item stored first will be accessed first. A real-world example of queue can be a single-lane, where the vehicle enters first, exits first. More real-world examples can be seen as queues at the ticket windows and bus- stop. Basic queue operations: Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queue: Enqueuer() - add (store) an item to the queue. Dequeue() – remove (access) an item from the queue. Few more functions are required to make the above-mentioned queue operation efficient. Peek() – gets the element at the front of the queue without removing it.
available can result in a stack overflow error, which is frequently associated with security issues. Uses of memory stack on memory devices with demonstration: Basically, the main use of the memory stack on memory devices is to store the temporary variables created by each function (including the main () function). Basically, RAM is the best example of memory device and RAM store the data temporarily and the main use of the memory stack is to store the temporary variable. The data of the memory stack is stored in the RAM of the computer. So, in conclusion it can be said that the use of the memory stack on the memory device like RAM is to store the temporary variables. In the above figure spped limitation data of the traffic mangment system is stored in the stack. Two different operation are done Push and POP. The data that are finally obatined after doing the push and pop operation will be the temporary
variables and those temporary sata are stored in the RAM of the computer. RAM is considerd as the memory device of computer which store the data permanently. From the above statement is can be concluded that the use of the memory stack in the memory device like RAM is to store the temporary data. Show this is the best demonstration of the use of memory stack on the memory devices. How memory stack help to solve function calling of different operation in computer? Basically, the main function of stack is to store the data. In the above screenshot I have shown the process of storing the data of speed limitation. There are two types of function in the stack they are Push and Pop and their operation is to store and abstract the data respectively. With the help of the push function I can easily store the needed data of the speed limitation in the systematic way. The data that are store in the stack are the temporary variables. In our computer system the RAM play the vital role to store the temporary data. The data store in stack are save in the computer memory i.e. RAM. In the RAM different operations like: insert, delete, update, etc. takes places and in the stack also the same operation is performed. The push operation is used to insert the data as well as the pop operation is used to delete the data in the stack. The data are arranged in systematic order in stack. So the update operation can easily be done in stack.
2. Stack Frame 2.1. The Basics The stack frame, also known as activation record is the collection of all data on the stack associated with one subprogram call. The stack frame generally includes the following components: The return data address Argument variables passed on the stack
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BTthem2.MangSo { public class MyStack { #region field private int top = -1; private int[] data; #endregion #region methods public void Push(int element) { if (IsFull()) throw new Exception("Stack is full!!"); data[++top] = element; } public int Pop() { if (IsEmpty()) throw new Exception("Stack is empty!!"); return data[top--]; } public int Top() { if (IsEmpty()) throw new Exception("Stack is empty!!"); return data[top]; } public bool IsEmpty() => top < 0;
public bool IsFull() =>(top + 1) == data.Length; public int Size() => top +1; public int[] GetElements() { if(!IsEmpty()) { int[] currentElement = new int[Size()]; for (int i = 0; i < Size(); i++) { currentElement[i] = data[i]; } return currentElement; } return new int[0]; } #endregion #region Constructor public MyStack() { data = new int[100]; } public MyStack(int length) { if (length < 1) throw new Exception("Lenght must be greater than 0 !!!!"); data = new int[length]; } #endregion } } using System; using System.Collections.Generic;
ms.Push(5); ms.Push(10); ms.Push(15); MyStack ms = new MyStack(5); //ms: instance(object) ms.Push(5); ms.Push(10); ms.Push(15); ms.Push(20); ms.Push(25); ms.Push(30); MyStack ms = new MyStack(5); //ms: instance(object) ms.Push(5); ms.Push(10); ms.Push(15); ms.Push(20); ms.Pop(); ms.Pop();
C. Implement complex data structures and algorithms I. Implement a complex ADT and algorithm in an executable programming language to solve a well defined problem.
1. Publish/subscribe 1.1. What is Publish/subscribe The publish-subscribe pattern (or pub/sub, for short) is a Ruby on Rails messaging pattern where senders of messages (publishers), do not program the messages to be sent directly to specific receivers (subscribers). Instead, the programmer “publishes” messages (events), without any knowledge of any subscribers there may be. Similarly, subscribers express interest in one or more events, and only receive messages that are of interest, without any knowledge of any publishers. To accomplish this, an intermediary, called a “message broker” or “event bus”, receives published messages, and then forwards them on to those subscribers who are registered to receive them. In other words, pub-sub is a pattern used to communicate messages between different system components without these components knowing anything about each other’s identity.
1.2. Example Suppose you would like to check if the queue exists and if it does, send a message to it. If the queue doesn't exist, you may want to create a new one and then send it a message. static void Main(string[] args) { MessageQueue messageQueue = null; string description = "This is a test queue."; string message = "This is a test message."; string path = @".\Private$\IDG"; try { if (MessageQueue.Exists(path)) { messageQueue = new MessageQueue(path); messageQueue.Label = description; } else { MessageQueue.Create(path); messageQueue = new MessageQueue(path); messageQueue.Label = description; } messageQueue.Send(message); } catch { throw; } finally { messageQueue.Dispose(); } }
2. Implementation
2.1. Client.Pub 2.1.1. MessageServiceExtension.cs using Client.Pub.EventService; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Client.Pub { static class MessageServiceExtension { public static void Test(this IMessageService client, string message) { Thread.Sleep(2000); Console.WriteLine($"Message: {message}"); Console.WriteLine(client.Send(message)? "Send successfully!": "Something went wrong!"); Console.WriteLine("-------------------------------\n"); } } } 2.1.2. Program.cs using Client.Pub.EventService; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks;