Unit 1 Introduction of DS Stack Queue
Unit 1 Introduction of DS Stack Queue
Unit 1 Introduction of DS Stack Queue
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.
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.
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
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
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
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]
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
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 B C A B C A B C A B 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:-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
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
Trace of queue
There is one array and that array having 6 elements
Empty (Underflow)
11 11 22
11 22 33 11 22 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