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

Data structures assignment

Uploaded by

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

Data structures assignment

Uploaded by

Mapalo Muonga
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CAVENDISH UNIVERSITY – ZAMBIA

ASSIGNMENT BRIEF AND FEEDBACK FORUM

STUDENT NUMBER: 110 – 026

LECTURER: ER. MOHANA PRIYA

MODULE: DATA STRUCTURES AND ALGORITHM

MODULE CODE: IT 121

ASSIGNMENT NUMBER: ONE

DATE HANDE OUT: 20 MARCH 2024

DATE HANDED IN: 26 APRIL 2024


TABLE OF CONTENT

TYPES OF QUEUES AND STACKS

INTRODUCTION

STACKS

EXPRESSIONS

QUEUES

CIRCULAR QUEUES

CONCLUSION

REFERENCES
INTRODUCTION

A data structure is a particular way of organising and storing data so that it can be accessed easily and
used effectively. The idea is to reduce the space and time complexity of different tasks. The choice of
a good data structure makes it possible to perform a variety of critical operations effectively. An
efficient data structure also uses minimum space and execution space time to process the structure. A
data structure is not only used for organising the data, it is also used for processing, storing and
retrieving data. There are different basic and advanced types of data structures that are used in almost
each and every program or software system that has been developed so we must have good
knowledge of data structures. In this assignment will mainly focus on stacks, queues, expressions and
there examples. Note that a stack data structure does not have any types, but as for queues it has three
types and these are circular, priority, and double ended queues. You can visualise a stack as vertical
collection as well as visualise a queue as a horizontal collection.
Stack: A stack is a container of objects that are inserted and removed according to the last in first
out (LIFO). In the pushdown stacks only two operations are allowed: push the item into the stack, and
pop the item out of the stack. A stack is a limited access data structure element which can be added
and removed from the stack only at the top. In a push, it adds an item to the top of the stack, and pop
remove, the item from the top. A simple example is to think of a stack of books which you can
remove only the top book, for also you can add a new book on the top or a stack of plates which from
you can only remove the top plate. A stack is a structure recursive data. Here is a structural definition
of a Stack:

Implementation
In the standard library of classes, the data type stack is an adapter class, meaning that a stack is built
on top of other data structures. The underlying structure for a stack could be an array, a vector, an
array list, a linked list, or any other collection. Regardless of the type of the underlying data structure,
a Stack must implement the same functionality. This is achieved by providing a unique interface:

LIFO & FILO Principles

Stack: Last In First Out (LIFO): The last object into a stack is the first object to leave the stack, used
by a stack

OR

Stack: First In Last Out (FILO): The first object or item in a stack is the last object or item to leave the
stack.
Expressions: In the realm of data structures and algorithms, expressions play a crucial role in
solving complex problems. Infix, prefix and postfix expressions are three different ways to represent
arithmetic expressions. Each notation has its own advantages and use cases, making it essential for
any programmer or computer scientist to comprehend their differences and applications. In the world
of mathematics and computer science, expressions are mathematical phrases that contain operands
and operators. These expressions are used to perform calculations and comparisons. Expressions can
be written in various forms, with infix, prefix, and postfix being the most common notations.
Expressions find application in various domains. They are used in calculators, spreadsheet software,
and programming languages to perform calculations and decision-making. Different notations provide
flexibility based on the use case.

Infix expressions are the standard arithmetic notations that we are most accustomed to. They place the
operators (+, -, *, /) between the operands, and may also use parentheses to indicate the order of
operations. For example, the infix expression 5+5*2 would be evaluated as 15, not 20, due to the
multiplication taking precedence over addition. Infix expressions closely resemble how we write and
understand mathematical equations. However, this human-friendly format can pose challenges when it
comes to dividing and evaluating the expression programmatically. Operator precedence and
parentheses complicate the algorithmic evaluation process.

Prefix expressions, also known as Polish notation, flip the script by placing the operator before the
operands. For instance, the infix expression 5 + 2 would become + 5 2 in prefix notation. This
eliminates the need for parentheses to indicate precedence. Prefix expressions shine in their simplicity
of evaluation. Processors can easily divide and evaluate, them using stacks, making them efficient for
computers to handle. However, writing and comprehending prefix expressions might prove to be less
intuitive for humans.

Postfix expressions, or Reverse Polish notation, take a different approach by placing operators after
the operands. Following our previous example, 3 + 4 would become 3 4 +. Postfix expressions also
eliminate the need for parentheses and remove ambiguity regarding operator precedence. Postfix
expressions share the computational efficiency of prefix expressions, making them ideal for computer
evaluation. They are less prone to human error due to their explicit nature. However, they might
require some adjustment in thinking for individuals more accustomed to infix notations.

Converting Between Notations

The ability to convert expressions between different notations is a crucial skill for programmers.
Conversion from infix to postfix or prefix notation involves understanding operator precedence and
associativity. This process often employs stacks to reorder operators.

Infix to Postfix Conversion Algorithm

To convert infix expressions to postfix, the shunting yard algorithm is commonly employed. This
algorithm uses stacks to manage operators and parentheses, ensuring correct conversion.

Prefix to Postfix Conversion Algorithm

Converting prefix expressions to postfix can be accomplished using stacks as well. The process
involves iterating through the expression from right to left and using a stack to maintain intermediate
results.
Queues: A queue is a container of objects (a linear collection) that are inserted and removed
according to the first-in first-out (FIFO) principle. An excellent example of a queue is a line of
students in the food cafeteria of a particular school. Any new additions to a line made to the back of
the queue, while removal or serving happens in the front. In the queue only two operations are
allowed enqueue and dequeue. Enqueue means to insert an item into the back of the queue, dequeue
means removing the front item. The picture demonstrates the FIFO access.

The picture demonstrates the FIFO access. The difference between stacks and queues is in removing.
In a stack we remove the item the most recently added; in a queue, we remove the item the least
recently added

FIFO & LILO principle

Queue: First In First Out (FIFO): The first object into a queue is the first object to leave the queue,
used by a queue.
OR.

Queue: Last In Last Out (LILO): The last object or item in a queue is the last object or item to leave
the queue.

Implementation

In the standard library of classes, the data type queue is an adapter class, meaning that a queue is built
on top of other data structures. The underlying structure for a queue could be an array, a Vector, an
array list, a linked list, or any other collection. Regardless of the type of the underlying data structure,
a queue must implement the same functionality. This is achieved by providing a unique interface.
Circular Queue: in a Data Structure a circular queue is an extended version of a linear queue
as it follows the First In First Out principle with the exception that it connects the last node of a queue
to its first by forming a circular link. Hence, it is also called a Ring Buffer. This data structure
efficiently stores processes, ensuring seamless resource allocation. Unlike traditional queues, a
circular queue optimizes space by overwriting old entries when full, enabling continuous operation
and execution management within computing environments. The Circular Queue is similar to a Linear
Queue in the sense that it follows the FIFO (First In First Out) principle but differs in the fact that the
last position is connected to the first position, replicating a circle. Circular Queue works by the
process of circular increment i.e. when we try to increment the pointer and we reach the end of the
queue, we start from the beginning of the queue. A circular queue can be implemented using two data
structures; Array and Linked list

Implementation of circular queue using Array

Circular queues can be implemented using arrays of fixed size. To enqueue an element, we check if
the queue is full and exit if so. If the queue is empty, we set the front pointer to the index of the first
element. Then, we increment the rear pointer and insert the new element. For dequeue, if the queue is
empty, we exit. Otherwise, we fetch the rear element and store it. If it's the last element, we set the
pointers to -1; otherwise, we increment the front pointer and return the stored element.

Implementation of circular queue using linked list

Circular queues implemented using linked lists are flexible as the size can dynamically adjust. The
front and rear pointers are used to manage the linked list, ensuring a circular structure. Enqueueing
involves adding a new node at the end of the list, while memory allocation is checked to avoid
overflow. In enqueue there will be any of two cases seen while inserting a new node: first Insertion in
an empty list second Insertion in a non-empty list/ normal insertion.

Operations on circular queue

Front - Used to get the starting element of the Circular Queue.

Rear - Used to get the end element of the Circular Queue.

En-queue (value) - Used to insert a new value in the Circular Queue. This operation takes place from
the end of the Queue.

De-queue ( ) - Used to delete a value from the Circular Queue. This operation takes place from the
front of the Queue.
CONCLUSION

In conclusion the comprehension and mastery of stacks, expressions, circular queues and queues are
vital in the realm of data structures and algorithms. Stacks operate under the last in first out
principle(LIFO) serves as an essential tool for managing program execution flow, recursion, and
parsing algorithms. Expressions, encompassing mathematical and logical statements, play a central
role in computational tasks such as arithmetic evaluation, parsing and symbol manipulation. Queues
adhering to the first in first out principle(FIFO) discipline are very needed for task requiring
sequential processing such as job scheduling, breadth first search, and networking packet handling.
Circular queues facilitate efficient memory usage and enable cyclic data process, like in circular
buffers, round-robin scheduling, and event driven simulation. This empowers programmers to design
precise and efficient algorithms.
REFERENCES

https//www.w3schools.com

https//www.geeksforgeeks.org

https//techdevguide.withgoogle.com

https//www.programiz.com

https//www.tutorialspoint.com

https//www.freecodecamp.org

You might also like