0% found this document useful (0 votes)
124 views

Data Structure

The document is a coursework submission on data structures and algorithms. It includes sections on queue data structure, implementation of queues, stack operations, sorting algorithms including bubble sort and merge sort, and programs on queues and a library management system using object-oriented programming. Figures and code snippets are provided to demonstrate queue operations, sorting algorithms, and programs for a queue and library management system.

Uploaded by

Claudiu Iordan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

Data Structure

The document is a coursework submission on data structures and algorithms. It includes sections on queue data structure, implementation of queues, stack operations, sorting algorithms including bubble sort and merge sort, and programs on queues and a library management system using object-oriented programming. Figures and code snippets are provided to demonstrate queue operations, sorting algorithms, and programs for a queue and library management system.

Uploaded by

Claudiu Iordan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

ICON College of Technology and Management

Department of Information Technology

BTEC HND in Computing

Coursework

Data Structures and Algorithms

Tutor: Dr Sanjib Raj Pandey

Submitted by

ID No: 16381

Session: February 2020


Table of Contents
Queue Data Structure..................................................................................................................................3
Types of Queues in Data Structure..........................................................................................................3
Basic operations in a queue.....................................................................................................................4
Fig. 1 Queue Data Structure Graphics..............................................................................................5
Implementation of Queue Data Structure...................................................................................................6
Fig.2 Explaining “FIFO” ( Head and Tail ).........................................................................................6
Stack Operations.........................................................................................................................................7
Sorting Array algorithm...............................................................................................................................7
Bubble Sort..............................................................................................................................................7
Merge Sort...............................................................................................................................................8
Shell Sorting.............................................................................................................................................8
Try Catch..................................................................................................................................................8
Queue Program...........................................................................................................................................9
Running Program in C#..........................................................................................................................12
Fig 3. Screenshot Queue Program with two errors and two messages..........................................12
Fig. 5 Screenshot Queue Program working properly.....................................................................13
Fig. 6 Screenshot Running Queue Program...................................................................................14
Library Management System OOP diagram...............................................................................................15
Program.....................................................................................................................................................16
Fig. 9 Screenshot Library Management System OOP Code Program............................................17
Fig. 10 Screenshot Library Management System OOP Code Program.........................................18
Fig. 11 Screenshot Library Management System OOP Code Program.........................................19
Fig. 12 Screenshot Library Management System OOP Code Program..........................................20
Running Library Management System OOP...........................................................................................21
Fig.13 Screenshot Outcome Running Library Management Code Program...................................21
Fig. 13 Screenshot Asymptotic notations.......................................................................................22
References.................................................................................................................................................23
Appendix...................................................................................................................................................24
Queue Data Structure

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 :

 “The consumer who comes first to a shop will be served first. 


 CPU task scheduling and disk scheduling.
 Waiting list of tickets in case of bus and train tickets.”

Types of Queues in Data Structure

Queue in data structure is of the following types

 “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.”

Dequeue (Doubly Ended Queue)

“In a Double Ended Queue, insertion and deletion operations can be done at both FRONT and END of
the queue.”
 

Basic operations in a queue

Basic queue operations in data structure

 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() 

 “Check if the queue is full.”


 “If the queue is full, then print Queue overflow".
 “Else increment REAR by 1.
 Assign QUEUE [ REAR ] = ELEMENT.”
 
dequeue()

 “Check if the queue is empty.”


 If the queue is empty, the print "Queue underflow".
 “Copy the element at the front of the queue to some temporary variable, TEMP =
QUEUE[ FRONT ].
 Increment FRONT by 1. 
 Print temp and delete it.”
Fig. 1 Queue Data Structure Graphics
Implementation of Queue Data Structure

[0] [1] [2] [3] [4] [5] [6] [7]

Head Tail

[0] [1] [2] [3] [4] [5] [6] [7]


27
Adding elements
Head Tail to queue

[0] [1] [2] [3] [4] [5] [6] [7]


27 19 17 7
Removing
Head Tail items from
Queue

[A]

[0] [1] [2] [3] [4] [5] [6] [7]


19 17 7

Head Tail

[B]

[0] [1] [2] [3] [4] [5] [6] [7]


19 17 7

Head Tail

Fig.2 Explaining “FIFO” ( Head and Tail )


Stack Operations

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 Array algorithm

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 .

Real-life example of sorting:


 Smartphones agenda
 Dictionary

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

 Phase 1 − if it is only one element in the list it is already sorted, return.


 Phase 2 − split the list into two halves until it can no more be divided.
 Phase 3 − merge the smaller lists into new list in sorted order.
procedure mergesort( var a as array )
if ( n == 1 ) return a

var l1 as array = a[0] ... a[n/2]


var l2 as array = a[n/2+1] ... a[n]

l1 = mergesort( l1 )
l2 = mergesort( l2 )

return merge( l1, l2 )


end procedure
[ CITATION tut20 \l 2057 ]

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("====================================");

// push 4 items on to the stack


myStack.Push(10);
myStack.Push(20);
myStack.Push(30);
myStack.Push(40);
// print the contents of the stack
myStack.PrintStack();
// show the topmost element of the stack
myStack.Peek();
// pop the top element off the stack
Console.WriteLine("Item popped from Stack: {0}", myStack.Pop());
// show the contents of the stack again
myStack.PrintStack();

// formatting
Console.WriteLine("===");

// QUEUE
// create a new queue object
Queue myQueue = new Queue();

// formatting
Console.WriteLine("===================================");
Console.WriteLine("Queue:");
Console.WriteLine("===================================");

// enque 4 items to the queue


myQueue.Enqueue('A');
myQueue.Enqueue('M');
myQueue.Enqueue('G');
myQueue.Enqueue('W');
// print the contents of the queue
Console.WriteLine("Current queue: ");
foreach (char c in myQueue) Console.Write(c + " ");
Console.WriteLine();

// enque 2 items to the queue


myQueue.Enqueue('V');
myQueue.Enqueue('H');
// print the contents of the queue
Console.WriteLine("Current queue: ");
foreach (char c in myQueue) Console.Write(c + " ");
Console.WriteLine();

// remove first value from queue "A"


Console.WriteLine("Removing some values ");
char ch = (char)myQueue.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
// remove newly first value from queue "M"
ch = (char)myQueue.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
// print the contents of the queue
Console.WriteLine("Current queue: ");
foreach (char c in myQueue) Console.Write(c + " ");
Console.WriteLine();

// formatting
Console.WriteLine("===================================");

// wait for user input to close program


Console.ReadLine();
}
}
// Stack data structure implementation class
internal class Stack
{
static readonly int MAX = 1000;
int top;
// Make the field readonly according to the message IDE0044:
// private readonly int [] stack = new int[MAX];
private readonly int[] stack = new int[MAX];

public Stack()
{
top = -1;
}
internal bool Push(int data)
{
if (top >= MAX)
{
Console.WriteLine("Stack Overflow");
return false;
}
else
{
stack[++top] = data;
return true;
}
}

internal int Pop()


{
if (top < 0)
{
Console.WriteLine("Stack Underflow");
return 0;
}
else
{
int value = stack[top--];
return value;
}
}
// Peek: Return the topmost element of stack.
internal void Peek()
{
if (top < 0)
{
Console.WriteLine("Stack Underflow");
return;
}
else
Console.WriteLine("The topmost element of Stack is : {0}", stack[top]);
}
// Print Stack: Prints the contents of the stack
internal void PrintStack()
{
if (top < 0)
{
Console.WriteLine("Stack Underflow");
return;
}
else
{
Console.WriteLine("Items in the Stack are :");
for (int i = top; i >= 0; i--)
{
Console.WriteLine(stack[i]);
}
}

}
}
Running Program in C#

Fig 3. Screenshot Queue Program with two errors and two messages

Message IDE0051 “Private member ‘Stack.isEmpty’ is unused ” - remove member Stack.isEmpty


Fig. 4 Screenshot Queue Program showing the message “Make field readonly”

Fig. 5 Screenshot Queue Program working properly


Outcome of the Queue Program

Fig. 6 Screenshot Running Queue Program


Library Management System OOP diagram

Fig. 7 Screenshot OOP Diagram


Program

Fig. 8 Screenshot Library Management System OOP Code Program


Fig. 9 Screenshot Library Management System OOP Code Program
Fig. 10 Screenshot Library Management System OOP Code Program
Fig. 11 Screenshot Library Management System OOP Code Program
Fig. 12 Screenshot Library Management System OOP Code Program
Running Library Management System OOP

Fig.13 Screenshot Outcome Running Library Management Code Program


In order to provide a clear understanding of complexity analisys, we have to take into account
the following:
 Asymptotic analisys
 Big-O Notations
 Big-O Rules
 Big-O Complexity
 Determine the complexity of code structure
Types of analysis :
Worst Case, Best Case, Average Case
 Worst Case – Gives an upper
Lower Bound ≤ Running Time ≤ Upper Bound bound at the end. (n
similarities).
 Best Case – Gives a lower
bound at the beginning.(one similarity)

 Average Case – Bound may found between 0, 1, 2….n-1


Average no. of analogies is: (n+1) / 2

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. }

Following is a list of some common asymptotic notations

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 }

g(n) is an asymptotic upper bound for f(n).

cg(n)

f(n)

9
n0

Fig. 13 Screenshot Asymptotic notations

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

 "Queue (Java Platform SE 7)". Docs.oracle.com. 2014-03-26. Retrieved 2014-05-22.


 Okasaki, Chris. "Purely Functional Data Structures" (PDF).
 Hood, Robert; Melville, Robert (November 1981). "Real-time queue operations in pure Lisp".
Information Processing Letters. 13 (2). hdl:1813/6273.
 This article incorporates public domain material from the NIST document: Black, Paul E.
"Bounded queue". Dictionary of Algorithms and Data Structures.
 Donald Knuth. The Art of Computer Programming, Volume 1: Fundamental Algorithms, Third
Edition. Addison-Wesley, 1997. ISBN 0-201-89683-4. Section 2.2.1: Stacks, Queues, and Deques,
pp. 238–243.
 Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to
Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 10.1:
Stacks and queues, pp. 200–204.
 William Ford, William Topp. Data Structures with C++ and STL, Second Edition. Prentice Hall,
2002. ISBN 0-13-085850-1. Chapter 8: Queues and Priority Queues, pp. 386–390.
 Adam Drozdek. Data Structures and Algorithms in C++, Third Edition. Thomson Course
Technology, 2005. ISBN 0-534-49182-0. Chapter 4: Stacks and Queues, pp. 137–169.
 R. K. Treiber, “Systems programming: Coping with parallelism”, “RJ 5118, Almaden Research
Center, and ‘‘April 1986.
 M. Michael and M. Scott. “Nonblocking algorithms and preemption-safe locking on
multiprogrammed shared - memory multiprocessors.” Journal of Parallel and Distributed
Computing, 51(1): 1–26, 1998.
 D. Hendler, N. Shavit, and L. Yerushalmi. “A scalable lock-free stack algorithm.” Technical Report
TR-2004-128, Sun Microsystems Laboratories, 2004.
 J. D. Valois. “Implementing Lock-Free queues.” In Seventh International Conference on Parallel
and Distributed Computing Systems, Las Vegas, NV, October 1994.
 M. M. Michael and M. L. Scott. “Simple, Fast, and Practical Non-Blocking and Blocking
Concurrent Queue Algorithms.” 15th ACM Symp. On Principles of Distributed Computing
(PODC), May 1996. pp.267 – 275.
 [ CITATION gee20 \l 2057 ]
 [ CITATION tut20 \l 2057 ]

Appendix
Program.cs

You might also like