DS Theory
DS Theory
DS Theory
1] Linear data structure: Data structure in which data elements are arranged
sequentially or linearly, where each element is attached to its previous and
next adjacent elements, is called a linear data structure.
Examples of linear data structures are array, stack, queue, linked list, etc.
* Static data structure: Static data structure has a fixed memory size. It is
easier to access the elements in a static data structure.
* Dynamic data structure In dynamic data structure, the size is not fixed. It can
be randomly updated during the runtime which may be considered efficient
concerning the memory (space) complexity of the code.
2] Non-linear data structure: Data structures where data elements are not
placed sequentially or linearly are called non-linear data structures. In a non-
linear data structure, we can’t traverse all the elements in a single run only.
The time complexity is defined as the process of determining a formula for total
time required towards the execution of that algorithm. This calculation is totally
independent of implementation and programming language.
A primitive data type is a data type that is predefined. The predefined data type is
also known as built-in data type. These primitive data type may be different for
different programming languages. For example- int, float, char etc.
Non primitive data type is a data type that is not predefined. It is defined by the
user like arrays, linked list, files etc.
An abstract data type (ADT) is a mathematical model for data types. An abstract
data type is defined by its behavior (semantics) from the point of view of a user,
of the data, specifically in terms of possible values, possible operations on data of
this type, and the behavior of these operations.
Q6) Write and explain Merge sort algorithm using suitable example.
Ans) Merge sort is a sorting technique based on divide and conquer technique.
With worst-case time complexity being Ο(n log n), it is one of the most respected
algorithms.
Merge sort first divides the array into equal halves and then combines them in a
sorted manner.
We know that merge sort first divides the whole array iteratively into equal halves
unless the atomic values are achieved. We see here that an array of 8 items is
divided into two arrays of size 4.
This does not change the sequence of appearance of items in the original. Now
we divide these two arrays into halves.
We further divide these arrays and we achieve atomic value which can no more
be divided.
Now, we combine them in exactly the same manner as they were broken down.
The smaller value will be placed first.
In the next iteration of the combining phase, we compare lists of two data values,
and merge them into a list of found data values placing all in a sorted order.
After the final merging, the list should look like this −
Q7) Write a short note on Link list
Ans) A linked list is a sequence of data structures, which are connected together
via links.
Linked List is a sequence of links which contains items. Each link contains a
connection to another link. Linked list is the second most-used data structure
after array.
Types of Linked List
Following are the various types of linked list.
Simple Linked List − Item navigation is forward only.
Doubly Linked List − Items can be navigated forward and backward.
Circular Linked List − Last item contains link of the first element as next and
the first element has a link to the last element as previous.
Q13) What is Stack? Explain its operations.
Ans )A stack is a data structure which is used to store data in a linear fashion. It
follows a Last in, First out (LIFO) principle i.e. the data which is inserted most
recently in the stack will be deleted first from the stack. Some of the stack
operations are given below. The following are some common operations
implemented on the stack:
o push(): When we insert an element in a stack then the operation is known
as a push. If the stack is full then the overflow condition occurs.
o pop(): When we delete an element from the stack, the operation is known
as a pop. If the stack is empty means that no element exists in the stack,
this state is known as an underflow state.
o isEmpty(): It determines whether the stack is empty or not.
o isFull(): It determines whether the stack is full or not.'
o peek(): It returns the element at the given position.
o count(): It returns the total number of elements available in a stack.
o change(): It changes the element at the given position.
o display(): It prints all the elements available in the stack.
Q16)Write a note on insertion and deletion in Queue data structure.
o Queue is a linear data structure where the first element is inserted from
one end called REAR and deleted from the other end called as FRONT.
o Front points to the beginning of the queue and Rear points to the end of
the queue.
o Queue follows the FIFO (First - In - First Out) structure.
o According to its FIFO structure, element inserted first will also be removed
first.
o In a queue, one end is always used to insert data (enqueue) and the other
is used to delete data (dequeue), because queue is open at both its ends.
o The enqueue() and dequeue() are two important functions used in a
queue.
Reduce the length of code Not more effective in terms of space and time
Help in solving data structure
Hard to analyze code
problems
In this traversal method, the left subtree is visited first, then the root and later
the right sub-tree..
Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and
finally the right subtree.
Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we
traverse the left subtree, then the right subtree and finally the root node.
Q25)Define the following Graph Terminologies: 1. Directed Graph 2. Out degree
and In degree 3. Adjacent Vertices 4. Hamiltonian Path 5. Multigraph
1) a directed graph (or digraph) is a graph that is made up of a set
of vertices connected by directed edges, often called arcs.
2)Indegree-The number of edges coming into a vertex in a directed graph.
Outdegree-Outdegree of vertex V is the number of edges which are going out
from the vertex V.