Data Structure
Data Structure
Coursework
Submitted by
ID No: 16381
A Queue is an abstract data structure. And it is same as stack but in stack we can add items on both ends
but in queue we can add items from one side and on other end we can dequeue the items. Queue follow
the FIFO (first in first out) method. Following diagram shows the working on queue with its operations.
Examples of queues :
“Simple Queue
Circular Queue
Priority Queue
Dequeue (Double Ended Queue)”
Simple Queue
The simple queue is a normal queue where insertion takes place at the FRONT of the queue and deletion
takes place at the END of the queue.
Circular Queue
“In a circular queue, the last node is connected to the first node.
Circular queue is also called as Ring Buffer.
Insertion in a circular queue happens at the FRONT and deletion at the END of the queue.”
Priority Queue
“In a priority queue, the nodes will have some predefined priority.
Insertion in a priority queue is performed in the order of arrival of the nodes.
The node having the least priority will be the first to be removed from the priority queue.”
“In a Double Ended Queue, insertion and deletion operations can be done at both FRONT and END of
the queue.”
enqueue() - Adds an element at the beginning of the queue. If the queue is full, then it is an
overflow.
dequeue() - Deletes an element at the end of the queue. If the queue is empty, then it is an
underflow.”
enqueue()
Head Tail
[A]
Head Tail
[B]
Head Tail
Stack Operations are Abstract Data Types (ADT), used in a lot of programming languages. The name
“stack” comes from the real life, as its conduct is like real world stack, at one end only. If we want to
access an element, we will have access at the top element only. Stack can be one size only or have a
dynamic resizing.
The implementation of a stack can be used with Arrays, Structure, Linked List etc.
Basic Operations
Primary operations:
Push() - push element on the stack
Pop() - remove element from the stack
Stack Status Checking Operations:
isEmpty( ) - Stack is empty
isFull( ) - Stack is full
peek( ) - Get the highest data element of the stack
Let’s suppose we have push and pop functions in program then we can call these functions like following
list.
Push(1);
Push(2);
Push(3);
Push(push (2) *push(4));
Pop(4);
Sorting algorithm is rearranging the data according the sorted list. If we have array list that contains
random numbers like 3,2,1,8,5,6,7,4,9 then using this algorithm we can sort the list in ascending order, 1,
2, 3, 4, 5, 6, 7, 8, 9 . It can be sorting in a descending order as well, depending on the user requirements to
make easy to access.
If data is stored in a in a sorted method, the optimization of data can reach a high level. Sorting can be
used for different formats to read .
Bubble Sort
Sorting algorithms may necessitate additional space for comparison and temporary storage of data items.
These algorithms do not require additional storage space and sorting must be performed in-place. This is
called in-place sorting. Bubble sort is an example of on-site classification.
Bubble Sort Example
static void bubbleSort(int []arr)
{
int n = arr.Length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
Merge Sort
With some sorting algorithms the program needs a space that is larger than or equal to the ordered
elements. Sorting with the same or larger storage space is called non-in-place sorting. Merge-sort is an
example of non-local sorting.
Merge-Sort Example
l1 = mergesort( l1 )
l2 = mergesort( l2 )
Shell Sorting
Shell sorting is a very efficient sorting algorithm and is built on the insert sorting algorithm. This
algorithm evades large shifts like when implanting if the smallest value is on the far right and must be
shifted to the extreme. For medium-sized data, this
Try Catch
In try catch conditions we can detect the error in program without closing the system. Using these
conditions if code detect any error like we need to input user data in integer format, but user enter data in
string format then try catch detect the error and show the error message.
Syntax
Try {
Conditions
}
Catch(Exception e)
{
Console.write(“Error!”, e.Message);
}
Queue Program
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QueueProgram
{
class Program
{
static void Main(string[] args)
{
// STACK
// create a new stack object
Stack myStack = new Stack();
// formatting
Console.WriteLine("====================================");
Console.WriteLine("Stack:");
Console.WriteLine("====================================");
// formatting
Console.WriteLine("===");
// QUEUE
// create a new queue object
Queue myQueue = new Queue();
// formatting
Console.WriteLine("===================================");
Console.WriteLine("Queue:");
Console.WriteLine("===================================");
// formatting
Console.WriteLine("===================================");
public Stack()
{
top = -1;
}
internal bool Push(int data)
{
if (top >= MAX)
{
Console.WriteLine("Stack Overflow");
return false;
}
else
{
stack[++top] = data;
return true;
}
}
}
}
Running Program in C#
Fig 3. Screenshot Queue Program with two errors and two messages
By asymptotic analysis we can understand to find the exact complexity, or the simple operations
of an algorithm. This can be quite difficult.
F(n)= “±” “÷” “×”
We approach f (n) by a function g (n) in a way that does not modify the amplitude of f(n). - the
function g (n) is reasonably close to f (n) for big values of size n.
This measure is called asymptotic complexity.
Big-O Notation
Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ g(n) for all n > n0. }
Constant O(1)
Linear O(n)
Cubic O(n3 )
n log n O(n log n)
Logarithmic O(log n)
quadratic O(n2 )
exponential 2O(n)
polynomial nO(n)
O-notation
O(g(n)) = {f(n) : the c is constant positive and n0 such that, 0 ≤ f(n) ≤ cg(n), for n ≥ n0 }
cg(n)
f(n)
9
n0
Big-O Rules
If an algorithm does a sequence of actions f(n) times for a mathematical function f, it requires
O(f(n)) steps
If an algorithm does a process that takes steps f (n) and another operation that makes stages
g (n) for the function f, the overall performance of the algorithm is O (g (n) + f (n)).
If algorithm takes O(f(n)) + g(n), and f(n) is bigger than g(n), algorithm will be shown like this
O(f(n)).
If algorithm has f(n) steps and for every stage does extra operation with g(n) steps the
performance looks like this O(f(n) x g(n)).
The complexity of code structure is determined by the number of restatements, the characteristics
of a loop
for (int i = 0; i < n; i++) sum = sum – i.
References
Appendix
Program.cs