DS Unit 2
DS Unit 2
SUBJECT: DATA
STRUCTURE
UNIT-2 : Linear Data Structure- Array and Stack
-PROF. ANKIT
PATEL
-- Explain Array in detail
❑ Simplest data structure that makes use of computed address to locate its
elements is the one-dimensional array or vector; number of memory locations is
sequentially allocated to the vector.
❑ A vector size is fixed and therefore requires a fixed number of memory
locations. Vector A with subscript lower bound of “one” is represented as below.
❖ Two Dimensional Array:
❑ Two dimensional arrays are also called table or matrix, two dimensional arrays
have two subscripts
❑ Two dimensional array in which elements are stored column by column is called
as column major matrix
❑ Two dimensional array in which elements are stored row by row is called as row
major matrix
❑ First subscript denotes number of rows and second subscript denotes the number
of columns
❑ Two dimensional array consisting of two rows and four columns as above Fig is
stored sequentially by columns : A [ 1, 1 ], A [ 2 , 1 ], A [ 1 , 2 ], A [ 2 , 2 ], A [ 1 , 3 ],
A [ 2 , 3 ], A [ 1, 4 ], A [ 2, 4 ]
Applications of Array:
❑ 1. Symbol Manipulation (matrix representation of polynomial equation)
2. Sparse Matrix
Algorithm : RECOGNIZE
❑ Given an input string named STRING on the alphabet {a, b, c} which contains a blank in
its rightmost character position and function NEXTCHAR which returns the next
symbol in STRING, this algorithm determines whether the contents of STRING belong
to the above language. The vector S represents the stack, and TOP is a pointer to the
top element of the stack.
1. [Initialize stack by placing a letter ‘c’ on the top]
TOP ← 1
S [TOP] ← ‘c’
2. [Get and stack symbols either ‘c’ or blank is encountered]
NEXT ← NEXTCHAR (STRING)
Repeat while NEXT ≠ ‘c’
If NEXT = ‘ ‘
Then Write (‘Invalid String’)
Exit
Else Call PUSH (S, TOP, NEXT)
NEXT ← NEXTCHAR (STRING)
3. [Scan characters following ‘c’; Compare them to the characters on stack]
Repeat While S [TOP] ≠ ‘c’
NEXT ← NEXTCHAR (STRING)
X ← POP (S, TOP)
If NEXT ≠ X
Then Write (‘INVALID STRING’)
Exit
4. [Next symbol must be blank]
If NEXT ≠ ‘ ‘
Then Write (‘VALID STRING’)
Else Write (‘INVALID STRING’)
5. [Finished]
Exit
What is recursion? Write a C program for GCD using recursion.
❑ A procedure that contains a procedure call to itself or a procedure call to
second procedure which eventually causes the first procedure to be called is
known as recursive procedure.
❑ There are two important conditions that must be satisfied by any recursive
procedure
o a. Each time a procedure calls itself it must be nearer in some sense to a
solution
o b. There must be a decision criterion for stopping the process or computation
❑ There are two types of recursion
o Primitive Recursion: this is recursive defined function. E.g. Factorial function
o Non-Primitive Recursion: this is recursive use of procedure. E.g. Find GCD of
given two numbers.
• Write an algorithm to convert infix expression to postfix expression.
1. [Initialize stack] TOP 1
S[TOP] ‘(‘
2. [Initialize output string and rank count ] POLISH ‘ ‘
RANK 0
3. [Get first input symbol] NEXT NEXTCHAR (INFIX)
4. [Translate the infix expression ] Repeat thru step 7 while NEXT != ‘ ‘
5. [Remove symbols with greater precedence from stack] IF TOP < 1
Then write (‘INVALID’) EXIT
Repeat while G (S[TOP]) > F(NEXT) TEMP POP (S, TOP) POLISH POLISH O
TEMP RANK RANK + R(TEMP) IF RANK <1
Then write ‘INVALID’) EXIT
6. [Are there matching parentheses] IF G(S[TOP]) != F(NEXT) Thencall PUSH
(S,TOP, NEXT) Else POP (S,TOP)
7. [Get next symbol]
NEXT NEXTCHAR(INFIX) 8.[Is the expression valid]
IF TOP != 0 OR RANK != 1 Then write (‘INVALID ‘)
Else write (‘VALID ’)