Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1

Q:-1 what is Data Structure? Explain Hierarchical Representation of Data


 Data may be organized in many ways the logical or mathematical model of particular
organization of data is called a data structure.
Data Structure is collection of data elements whose organization is characterized by
accessing operation that are used to store and retrieve the individual information.
The choice of a particular data model depends on two considerations.
“It must be rich enough in structure to mirror the actual relationships of the data is the
real world.”
The structure should be simple enough that one can effectively process the data when
necessary.

Primitive Data Structure: -


Data at their most primitive level within a computer that is the data structure that
typically are directly operated upon by machine level instruction.
 Integer  Pointer
 Real  Logical
 Char
Integer: - A quantity representing object which are discrete in natural number. It means
number of objects are countable can be represented by an integer.

Real: - Fixed point rotation is sufficient to represent most of real no normally arising
when computations are performed on a computer. i.e. Floating point or scientific
notation.

Character: - The first computer program was written in machine code. Machine code
programming was difficult and programs were difficult to read and correct. To overcome
these problem symbolic codes were developed to represent information item.

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE
2
Pointer: - It is one type of variable which is use to store address of another variable in
memory location.

Non Primitive Data Structure: -


A data structure which is used the primitive data structure to build the further one and
based on the logic it is called Non-Primitive data structure. Means to say it does not
interact directly with machine level transaction.

Linear Data Structure:-A data structure is said to be linear if its elements from a
sequence, or in other words, a linear list.

1. Array (Linear Array):- Array is the finite, order set of homogeneous elements. An
array is collection of similar data elements. An array is also called linear data structure.
There are three types are available.
1. Single dimensional
2. Two dimensional
3. Multi dimensional

2. Linked List: - A linked list can be defined as a collection of nodes where nodes are
nothing but dynamic structure value connected to each other by mean of pointers. There
are three types are available.
1. Singly link list
2. Doubly link list
3. Circular link list

3. Stack: - A stack is a type of data structure where information is based on last in first
out (LIFO) system. In this all the insertion and deletions can take place at one end called
as TOP of Stack (TOS).
Stack may be static or dynamic

4. Queue: - A queue is a type of data structure where information is based on First in first
out (FIFO) system. In this all the insertion and deletions can take place at one end which
is called as rear of the list.

Non –Linear Data Structure


In the Non-linear data structures processing of data items is not possible in linear
fashion means data can not be processed one by one sequentially. In short a data structure
in which insertion and deletion is not possible in a linear fashion called as non- linear
data structure.

1. Tree: - It means the data is organized as branches. In this data contains a hierarchical
relationship between various elements. Following we are define type of tree.
1. Binary tree 2. Expression tree 3. Search tree 4. B- Tree 5. AVG tree 6. 2-3 tree

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE
3
2. Graph: -This data structure contains a relationship between pairs of elements, which is
not necessarily hierarchical in nature. A graph has two pairs i.e. vertices and edges.

Q:-2 what is an Array? OR What is a Linear Array. Explain its Storage structure
and operations with algorithms
A linear array is a list of a finite number n of homogeneous data elements (i.e. data
elements of the same type) such that
(a) The elements of the array are referenced respectively by an index set consisting of n
consecutive numbers.
(b) The elements of the array are stored respectively in successive memory locations.

We will assume our index 1, 2 ……n integers. Formula the calculate length of array.

Length= UB – LB + 1

UB is the largest index called upper bound


LB is the smallest index called lower bound

Example:- An automobile company uses an array Auto to record the number of auto
mobiles sold each year 1932 through 1984.
UB= 1984 , LB= 1932 So Length = UB – LB +1
= 1984-1932+1
= 53 So, array of auto contains 53 elements
Representation of linear array in memory
Let LA be a linear array in the memory of computer.
LOC (LA [K]) = Address of the element LA [K] of the array LA.

1000 As previously noted, the elements of LA are stored in successive memory cells.
1001 Accordingly the computer does not need to keep tack of the address of every
element of LA but needs to keep track only of the address of the first element of LA,
1002 denoted by Base(LA) and called the base address of LA. Using this address
1003 Base(LA) the computer calculates the address of any element of LA by following
formula:-
1004
LOC(LA [K] ) = Base(LA) + W(K- lower bound)
Computer W=Number of words per memory cell for the array LA
Memory K=Scanned the value.
Example: - Consider the array AUTO, which records the number of automobiles sold
each year from 1932 through 1984 suppose AUTO appears in memory as pictured in Fig.
That is Base (AUTO)=200, w= 4 words per memory per memory cell for AUTO then
LOC (AUTO [1932]) =200, LOC (AUTO [1933]) =204………………..
The address element for the year K= 1965
LOC (AUTO [1965]) = Base(AUTO)+w (1965 - lowerbound)
=200 + 4 (1965 - 1932) = 332

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE
4
ALGORITHMS:-
1. Traversing Linear Array:-
Here LA is a linear array with lower bound LB and upper bound UB this algorithm
traverse LA applying an operation PROCESS to each element of LA
1. [Initialize counter] set k := LB
2. Repeat step 3 and 4 while k <= UB
3. [visit element] apply process to LA[k]
4. [Increase counter] set k=k+1 [End of step 2 loop]
5. Exit
2. Inserting an element into Linear Array
Insert (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer such that K<=N.
This algorithm inserts an element ITEM into the Kth position in LA
1. [Initialize Counter] set J = N
2. Repeat steps 3 and 4 while J >=K
3. [Move Jth element downward] set LA[J+1]=LA[J]
4. [Decrease counter] set J = J -1
5. [Insert element] set LA [K]= ITEM
6. [Reset N] set N= N + 1
7. EXIT
3. Deleting an element from linear Array
Delete (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer such that K<=N.
This algorithm deletes an element the Kth element from LA.
1. set ITEM=LA [K]
2. Repeat for J= K to N – 1
[Move J+1st element upward]
Set LA[J]=LA[J+1]
[End of loop]
3. [Reset the number N of element in LA] Set N = N - 1
4. Exit
Advantages:-
Array locations are easily accessible using Array.
Array is useful data structure to store relatively permanent data.
Address calculation is very easy since the memory allocated sequentially.
Operations like sorting, merging, traversing, searching become easy with array.
Disadvantages:-
We can not increase the memory location since data structure
It is relatively time consuming to insert and delete element in array,
Array has static memory allocation for dynamic memory allocation we have to use
pointer.

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE
5
Q:-3 what is Stack? List out application of Stack. Explain all the operations of
stacks with algorithm OR Explain LIFO structure with algorithm.
Definition:- A stack is a linear structure in which items may be added or removed only at
one end called the Top of the stack
The elements are removed from stack in the reverse order of the in which they were
inserted into the stack.
A stack is based on the LIFO (Last in First Out) method.
Examples
1. Stack of dishes 2.Stack of pennies 3. Stack of folded towels
Applications of stack
1. Recursion
2. Tower of Hanoi
3. Polish Notation (Infix to Postfix, Infix to prefix, Postfix Evolution of postfix
expression)
Basic Operations associated with stack are
PUSH: - Insertion in to the stack
POP: - Deletion from the stack
PEEP: - To show a value (Extract information)
UPDATE: - Update information associated at some location in the stack

PUSH ALGORITHM
PUSH(S,TOP,X) This procedure inserts an element X to the top of a stack which is
represent by vector S containing N element with pointer TOP denoting the top element in
the stack.
Say, stack [N] is an array of N elements TOP is pointer that points the topmost element
of the stack.
Say X is an element to be inserted.
1. If TOP = N
Print “Stack over flow”
Stop
End If
2. TOP =TOP + 1
3. Stack [TOP] = X, Return X
4. Exit

POP ALGORITHM
POP(S, TOP) This function removes the top element from a stack which is represented
by vector S and returns this element. TOP is a pointer to the top element of the stack.
Say X is an element received after POP operation
1. If TOP = 0
Print “Stack Underflow”
Stop
End if
2. X = stack [TOP]

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE
6
3. TOP = TOP – 1
4. Return X
5. Exit

PEEP [FIND] ALGORITHM


 Say I is the Ith element from the TOP of the stack, whose value is to be returned.
1. If TOP – I + 1 < 0
Print “Stack under flow”
Stop
End if
2. Return = stack [TOP – I
3.
4. + 1]

REPLACE [UPDATE] ALGORITHM


 Say I is the Ith element from the TOP of the stack, whose value is to be replaced X is
the value to be Replaced.
1. If TOP – I + 1 < 0
Print “Stack under flow”
Stop
End if
2. stack [TOP – I + 1] = X

Q:-4 what is Recursion? Explain its advantages and disadvantages


Recursion is the name given to the technique of defining a set of process in term of
itself.
Every recursive function can be transformed into an iterative function by using a stack.
Functions whose domains can be recursively defined can be given recursive definitions
patterned after the recursive definition of their domain.
The canonical example of a recursively defined function is the following definition of
the factorial function.

f (n) = 1 if n = 0
f (n) = N * f (n-1) if n > 0
Given this definition also called recurrence relation, we work out f (3)

f(3) = 3 * f(3-1)
= 3 * f(2)
= 3 * 2 * f(2-1)
= 3 * 2 * f(1)
= 3 * 2 * 1 * f(1-1)
=3*2*1*1
=6
Advantages

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE
7
Through Recursion one can solve problems in easy way while
its iterative solution is very big and complex.
You reduce size of the code when you use recursive call.
Disadvantages
 Recursive solution is always logical and it is very difficult to trace.(debug and
understand)
 Recursion having very complex process to understand.

Q:-5 Explain Tower of Hanoi


The tower of Hanoi is a famous recursive problem, which is based on three pegs and a
set of discs of graduated sizes. Suppose three pegs, labeled X, Y and Z are given and on
peg X there are finite number of discs n in increasingly order, i.e. biggest one at the
bottom and smallest one at the top.

Rules:-
Only one disc may be moved at the time.
Only the top disc on any peg may be moved to any other peg.
A larger disc can not be placed on a smaller one
N=3

A B C A B C A B C A B C

(a) Initial (1) AC (2) A B (3) C B

A B C A B C A B C A B C

(4) AC (5) BA (6) B C (7) A C

Move top disk from peg A to peg C Move top disk from peg B to peg A
Move top disk from peg A to peg B Move top disk from peg B to peg C
Move top disk from peg C to peg B Move top disk from peg A to peg C
Move top disk from peg A to peg C

Q:-6 write down the algorithm for evaluate postfix expression.


1. Add ‘)’ at the end of P.

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE
8
2. Scan P from left to right repeat step 3 to 4 for each element of P until the ‘)’
encounter.
3. If an operand is encounter then push into S.
4. If an operator is encounter then
(a) Remove the two top element of the stack where a is a top element has a
next to top element.
(b) Evaluate B operator A.
(c) Place the result of back on stack S
[End of If]
[End of step 2]
5. Exit.

Q:-7 write down the algorithm Infix expression into postfix expression.
1. Push ‘(’on to stack & ‘)’ to the end of Q.
2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until the
stack is empty.
3. If operand is encounter add it to P.
4. If a left parenthesis is encounter push it to stack.
5. If an operator is encounter then
(a) Repeatedly POP from stack & add to P each operator which has same
precedents as or high precedent then incoming operator.
(b) Add operator in to stack
[End of If]
6. If a right parenthesis then
(a) Repeatedly POP from stack & add to P each operator until a left
parenthesis an encounter.
(b) Removed he left parenthesis
[End of If]
[End of step2]
Note: - Here Q is a variable which contain Infix expression P and stack are the variables
to store intermediate results.

Q:-8 write down the algorithm Infix expression into prefix expression.
+AB, -CD, /GH (operator placed before two operands)
1. push ‘)’ into stack and ‘(’ in beginning of Q
2. Scan Q for right to left and repeat step 3 to 6 for each element of until the stack is
empty.
3. If operand is encounter then push it to P
4. If a ‘)’ is encounter push into stack.
5. If an operator is encounter then
(a) Repeatedly POP from stack and add to P each operator which has a same
precedence or higher precedence then operator.
(b) Add (push) operator into stack [End of if]
6. If ‘(’ encounter then

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE
9
(a) Repeatedly POP from stack and add to P each operator until a right
parenthesis an encounter.
(b) Removed the ‘)’ from stack [End of If]
7. Display P in Reverse order
8. Exit

Q:-9 what is Queue? Explain Trace of Queue.


This is a linear data structure used to represent a linear list and permits deletion to be
performed at one end of the list and the insertion at the other.
The information in such a list is processed in the same order as it was received; i.e.
FIFO (First in First Out) or First come first serve (FCFS). The element which are inserted
first and deleted first.
General Example:-
1. Banking queue 2. Railway reservation counter
Computer Example:-
1. Time sharing (use share 2. Printout out from printer
simultaneously)

Deletion Insertion

Front Rear
Example:-
Q has 40 elements but at present there are only 4 elements in the queue.
1 2 3 4 5 6 ……………………. 39 40
11 22 33 44

Front Rear
When we remove one element from Q we get resulting Q as in figure
1 2 3 4 5 6 …………………… 39 40
22 33 44

Front Rear
So after removing first element front pointer is increased by 1 i.e. front = front + 1
When we insert one element from Q we get resulting Q as in figure .
1 2 3 4 5 6 ……………………… 39 40
22 33 44 55

Front Rear
So after insert one element in queue rear pointer is increased by 1 i.e. rear=rear+1

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE
10

Trace of queue
There is one array and that array having 6 elements

Empty (Underflow)

11 11 22

Front Rear Insert 11 Front Rear Insert 22

11 22 33 11 22 33 44

Front Rear Insert 33 Front Insert 44


Rear
22 33 44 33 44

Remove 11 Remove 22
Front Rear Front Rear
33 44 55 33 44 55 66

Insert 55 Insert 56
Front Rear Front Rear
33 44 55 66

Insert 77 Overflow Rear


Front
Q:-10 Write down algorithm insert and delete an element in Queue.
Insert an element in queue
Here Size is the variable of array size. rear and front pointer for both end of queue. Q is the
name of array
1. [check overflow condition] If (rear >=size)
Output “Overflow and return
2. [Increment rear pointer] rear=rear + 1
3. [Insert an element] Q[rear] =value
4. [set the front pointer] If (front = 0)
front=1
5. Return.

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE
11
Delete an element in queue
1. [check underflow condition] If (front =0)
Output “underflow and return
2. [Remove an element] value = Q[front]
3. [Check for empty queue] If front = rear
front =0
rear = 0
output “Queue is empty”
else
front = front + 1
4. Return (value).

Prepared by: - Vimal Vaiwala (lecturer) Msc (I.T)


SDJ INTERNATIONAL COLLEGE

You might also like