Assignment 1
Assignment 1
1. Array
The array is a type of data structure that stores elements of the same type. These are the most
basic and fundamental data structures. Data stored in each position of an array is given a positive
value called the index of the element. The index helps in identifying the location of the elements in
an array.
If supposedly we have to store some data i.e. the price of ten cars, then we can create a structure
of an array and store all the integers together. This doesn’t need creating ten separate integer
variables. Therefore, the lines in a code are reduced and memory is saved. The index value starts
with 0 for the first element in the case of an array.
2. Stack
The data structure follows the rule of LIFO (Last In-First Out) where the data last added element is
removed first. Push operation is used for adding an element of data on a stack and the pop
operation is used for deleting the data from the stack. This can be explained by the example of
books stacked together. In order to access the last book, all the books placed on top of the last
book have to be safely removed.
3. Queue
This structure is almost similar to the stack as the data is stored sequentially. The difference is that
the queue data structure follows FIFO which is the rule of First In-First Out where the first added
element is to exit the queue first. Front and rear are the two terms to be used in a queue.
Enqueue is the insertion operation and dequeue is the deletion operation. The former is performed
at the end of the queue and the latter is performed at the start end. The data structure might be
explained with the example of people queuing up to ride a bus. The first person in the line will get
the chance to exit the queue while the last person will be the last to exit.
4. Linked List
Linked lists are the types where the data is stored in the form of nodes which consist of an
element of data and a pointer. The use of the pointer is that it points or directs to the node which is
next to the element in the sequence. The data stored in a linked list might be of any form, strings,
numbers, or characters. Both sorted and unsorted data can be stored in a linked list along with
unique or duplicate elements.
5. Hash Tables
These types can be implemented as linear or non-linear data structures. The data structures
consist of key-value pairs.
Application of Strings:
Spam email detection.
Plagiarism detection.
Search engine.
Spell checkers.
Application of Matrix:
Matrix is an ordered collection of columns and rows of elements. It is necessary to enclose the
elements of a matrix within the brackets.
Some applications of a matrix are:
In geology, matrices are used for making seismic surveys.
Used for plotting graphs, and statistics and also to do scientific studies and research in almost
different fields.
Media player.
Mailing list.
Application of Stack:
A stack is a data structure that uses LIFO order.
Some Applications of a stack are:
Converting infix to postfix expressions.
History of visited websites.
Java Virtual Machine.
Application of Queue:
A queue is a data structure that uses FIFO order.
Some applications of a queue are:
Operating System uses queues for job scheduling.
Sending an e-mail, it will be queued.
Server while responding to request
In a linked list data is stored in nodes and each node is linked to the next and, optionally, to
the previous. Each node in a list consists of the following parts:
1. data
2. A pointer (Or reference) to the next node
3. Optionally, a pointer to the previous node
7. What are the advantages of a linked list over an array? In which scenarios do we use Linked
List and when Array?
Arrays
Linked Lists
A linked list is another approach to collecting similar data. However, unlike an array,
elements in a linked list are not in consecutive memory locations. A linked list is
composed of nodes which are connected with each other using pointers.
Linked lists are preferable over arrays when:
1. you need constant-time insertions/deletions from the list (such as in real-time computing where
time predictability is absolutely critical)
2. you don't know how many items will be in the list. With arrays, you may need to re-declare and
copy memory if the array grows too big
3. you don't need random access to any elements
4. you want to be able to insert items in the middle of the list (such as a priority queue)
Arrays are preferable when:
1. you need indexed/random access to elements
2. you know the number of elements in the array ahead of time so that you can allocate the correct
amount of memory for the array
3. you need speed when iterating through all the elements in sequence. You can use pointer math
on the array to access each element, whereas you need to lookup the node based on the pointer
for each element in linked list, which may result in page faults which may result in performance
hits.
4. memory is a concern. Filled arrays take up less memory than linked lists. Each element in the
array is just the data. Each linked list node requires the data as well as one (or more) pointers to
the other elements in the linked list
Definition of Algorithm
The word Algorithm means ” A set of finite rules or instructions to be followed in calculations or
other problem-solving operations ”
Or
” A procedure for solving a mathematical problem in a finite number of steps that frequently
involves recursive operations”.
Therefore Algorithm refers to a sequence of finite steps to solve a particular problem.