1) What Is Data Structure ? Explain Types of Data Structure
1) What Is Data Structure ? Explain Types of Data Structure
1) What Is Data Structure ? Explain Types of Data Structure
DATA STRUCTURE :
Data may be organized in different ways in computer memory. The logical or
mathematical model of particular organization of data in computers memory is called as “
Data Structure ”.
The choice of particular data structure depends on two considerations.
ALGORITHM ANALYSIS :
It is necessary to learn how to perform analysis of algorithm. The first step of analysis
is the “correctness” of an algorithm. This analysis can be done by manual execution of
algorithm on some data values.
Isolating the instructions of two types “Active instruction” and “Book keeping
instruction”. The instruction, which are executed by computer or contains arithmetic
operations are called active instruction. The instructions, which are used for initialization or
for maintaining the values of variable, are called “Book keeping instruction”.
In above algorithm there is only one active instruction, which is
SUM SUM + A [ I ]
Suppose, this instruction requires t1 amount of time and if get executed N number of
times. Therefore, the approximate time require for above algorithm is t1 * N. Here,
bookkeeping instructions are not taken in consideration because they require minimum time
for their execution.
Space Analysis :
It is very difficult to calculate the space analysis of a algorithm because
it requires lot of factors to be considered and therefore generally space
analysis of algorithm is not perform.
ARRAY :
Array is a finite set of finite number of homogeneous element which is also known as
“vector”. Array elements are stored as a successive memory set.
REPRESENTATION :
Loc( Ai,j ) = L0 + ( ( j – 1 ) * m + ( i – 1 ) ) * C
Loc( Ai,j ) = L0 + ( ( i - 1 ) * n + ( j – 1 ) ) C
1) ENTITY :
Any entity has certain properties refers as attributes, which may be designed values.
The values may be either numeric or non numeric.
2) FIELDS :
It is a single elementary unit of information representing an attribute of an entity set
where entities with similar attributes ( e.g. all students in an educational institute ) from an
entity set.
3) RECORDS :
It is the collection of field’s records of a given entity such as number of students in
educational institutes.
4) FILES :
It is the collection of records of the entities in a given set. Each record in a file consists
of fields, but the values in a certain field may uniquely determine the record in a file. Such
field id called primary keys.
STACK :
STACK :
Stack is list of elements in which an element may be inserted or deleted only at one
end, called top of stack. This means, in particular, that elements are removed from a stack in
the reverse order of that in which they are inserted into a stack.
It is also known as FILO ( First in Last out ) type of memory because the element
which is inserted first is the element which is deleted at last. It is also called as LIFO ( Last in
First out ).
A special terminology can be used to describe the operation of insertion and deletion.
The operation of insertion is called “PUSH” and the operation of deletion is called “POP”.
i) A+ (B*C)
= A + ( BC* )
=A+X X = BC*
= AX+
= A*BC+
ii) ( A + B ) / (C – D )
= ( AB+ ) / ( CD- )
=X/Y X = AB+; Y = CD-
= XY/
= AB+CD-/
iii) ( A+ B ) / ( C + D * E )
= ( AB+ ) / ( C + DE* )
= ( AB+ ) / ( CDE*+ )
= AB+CDE*+/
A Stack can be represented either using sequential data structure like array or non
sequential data structure like linked list.
The stack can be defined as a data structure in which elements can be inserted only
from one end called top of the stack and deleted only from one end called top of the stack i.e.
First is Last out or Last in First out.
If you want to use a linked list as a stack then, there must be the constraints on insert
and delete operation in linked list, you want insert node as well as you can delete node.
When we use linked list as stack then we can insert node only as last node and not
anywhere else. Similarly, we can delete a node, which are only last node and not any other
node.
Following algorithm shows the implement stack using link list.
3) [ If list is Empty ]
HEAD = END= TEMP
4) Exit;
1) [ Is list Empty ?]
If HEAD == NULL then
Print “Underflow” & Exit
5) Exit
1) POLISH NOTATIONS :
E.g. : a + b
a*b
Polish notation refers to the notation in which operator symbol is placed before its
operands. It is also known as prefix notation.
E.g. : +ab
*ab
The fundamental property of polish notation is that the orders in which operator are
evaluated is completely determined by position of operator and not by priority. We never
requires parenthesis while writing expression using polish notation. The expression in polish
notation is fast for execution as compare to infix notation.
E.g. : A+(B*C)
= A + ( * BC )
= A+X * BC = X
= + AX
= + A * BC
Reverse polish notation is also called postfix notation. In this notation the operator is
placed ofter the operands.
E.g. : ab+
ab*
2) RECURSIVE FUNCTION :
E.g. : Consider following program for calculate factorial of a number using recursive
function.
void main( )
{
int n=4, f;
f = Fact (n);
printf (“ \n FACTORIAL IS : %d”,f);
}
The function control is transferred from main function to fact function the value 4 is
passed as an argument to the function fact when the statement :
F = n * F( n – 1)
Is executed again the function Fact ( ) called with an argument value three before calling the
second Fact( ) with argument the there the value of n will get push( ) in stack. Regularly in
different function call to Fact the value 3,2 are push in stack. The recursive process stop
when value of n = 1 transfers the control the previous control will value 1 at this stage
function POP( ) will get called relative values of n. The value 2 will get poped and whole
statement is executed. Successively the POP( ) are executed to relative top most value in
stack.
5) What is stack ? Write an algorithm to insert & delete an element from the
stack .
STACK :
Stack is list of elements in which an element may be inserted or deleted only at one
end, called top of stack. This means, in particular, that elements are removed from a stack in
the reverse order of that in which they are inserted into a stack.
It is also known as FILO ( First in Last out ) type of memory because the element
which is inserted first is the element which is deleted at last. It is also called as LIFO ( Last in
First out ).
A special terminology can be used to describe the operation of insertion and deletion.
The operation of insertion is called “PUSH” and the operation of deletion is called “POP”.
PUSH : This algorithm insert element ITEM on TOP of the stack. The variable
MAXSTK gives maximum number of elements that can be stored in
stack.
1) [ Is stack full ? ]
If TOP = MAXSTK
Then write “OVERFLOW” & Exit
2) [ Increase value of TOP ]
TOP = TOP + 1
3) [ Insert element ]
STACK [ TOP ] = ITEM
4) STOP
POP : This algorithm deletes element (TOP) from stack and store it in
variable ITEM.
1) [ Is stack empty ? ]
If TOP = 0
Then print “UNDERFLOW” and Exit
4) STOP
QUEUE :
QUEUE :
A queue is a linear list of elements in which deletions can take place only at one end,
called the “Front”, and insertions can take place only at the other end, called the “Rear”. The
terms “Front” and “Rear” are used in describing a linear list only when it is implemented as
a queue.
Queue a are also called “First in First out” ( FIFO ) lists, since the first element in a
queue will be the first element out of the queue. In other words, the order in which elements
enter a queue is the order in which they leave. This contrasts with stacks, which are “Last in
First out” ( LIFO ) lists.
2) State the disadvantages & advantages of stack & queue using linked list.
ADVANTAGES :
The advantages of the stack and queue are that all stacks and queue being used by a
program can share same available list. When these both need a node takes from an available
list and when stack no longer need a node it returns to available list. No space has been pre-
allocated to any single stack.
Advantage is the entire stack and queue of a program have access to the same free list
of nodes. Another may use nodes not used by one stack, as long as the total number of nodes
in use at any one time is not greater than the total number of nodes available.
DISADVANTAGES :
A node in a link list occurs more storage than a element in an array, string in a link list
Two types of information is stored ( info, pointer part i.e. next ) for a single node, where in
array only one type of information is needed ( info part ) because array elements are stored
in continuous memory location hence no need to stored address of next element.
Another disadvantage is the additional time spent in managing the available list. Each
addition and deletion of an element from stack or a queue involves a corresponding deletion
or addition to the available to the available list.
QUEUE :
A queue is a linear list of elements in which deletions can take place only at one end,
called the “Front”, and insertions can take place only at the other end, called the “Rear”. The
terms “Front” and “Rear” are used in describing a linear list only when it is implemented as
a queue.
Queue a are also called “First in First out” ( FIFO ) lists, since the first element in a
queue will be the first element out of the queue. In other words, the order in which elements
enter a queue is the order in which they leave. This contrasts with stacks, which are “Last in
First out” ( LIFO ) lists.
3) Exit
PRIORITY QUEUE :
A priority queue is collection of elements such that each element has been assigned a
priority. The order in which the elements are deleted or process comes from following rules :
The priority queue is represented with help of “Link List” is computer’s memory.
The structure array is not suitable to represent a priority queue.
The array representation of a priority queue is more time-efficient than the one-way
list. This is because when adding an element to a one-way list, one must perform a linear
search on the list. On the other hand, the one-way list representation of the priority queue
may be more space-efficient than the array representation. This is because in using the array
representation, overflow occurs when the number of elements in any single priority level
exceeds the capacity for that level, but in using the one-way list, overflow occurs only when
the total number of elements exceeds the total capacity. Another alternative is to use a linked
list for each priority level.
CIRCULAR QUEUE :
LINKLIST :
LENGTH : This function is used to find the length of string stored as linked list.
HEAD is a starting node of linked list.
1) [ Initialization ]
LEN = 0
TEMP = HEAD
2) [ Is list Empty ? ]
If HEAD = = NULL then
Print “List is Empty.” & Exit
5) Exit
Computers are being used very frequently for word processing i.e. for inputting
processing and outputting matter. Therefore, computer must able to correct and modify
printed matter, which usually means deleting changing, and inserting words, sentences or
even paragraphs in a text. For this matter the fix length memory cells do not easily provide
all their operations.
According for most extensive word processing applications strings are stored by
means of linked list. The string can be store by two different ways one character per node or
a word per node.
Advantages :
The advantage of representing a string using a linked list is that you can perform word-
processing easily i.e. insertion, deletion or changing a character becomes easy.
Disadvantages :
The disadvantage is that the manipulating algorithms may becomes complicated.
1) [ Initialize ptr ]
ptr = START
2) [ Initialize counter ]
count = 0
4) [ Increase count ]
count := count +1
5) [ Update ptr ]
ptr := LINK [ ptr ]
6) print count
7) Exit
TREE
a) Tree : A tree is finite set of nodes with one special node called the root and remaining
nodes are portion into n >=0 disjoined set t1,t2,……,tn where each of those sets
is a tree t1,t2,….,tn are called “Sub Trees” of the root.
c) Leaf Node : A Leaf node is a terminal node of a tree it does not have any node
connected to it.
d) Degree of Node : Degree of sub trees of a node is called it’s degree.
e) Degree of Tree : A degree of tree is maximum degree of the node in the tree.
f) Parent Node : A parent node is a node having other node connected to it. These nodes
are called children of that node.
h) Desendence: Desendence of a node are all those nodes, which are reachable from hat
node.
i ) Ancestors : Ancestors of a node are all the nodes along the path from the root to that
node.
j) Forest : If the root node and the edges connecting to the root to the nodes at level one are
deleted them. Number of sub trees with roots which are the nodes at level one
are obtain. This set of disjoint trees is known as “Forest”