0% found this document useful (0 votes)
13 views20 pages

DS Unit 2

The document provides an in-depth explanation of linear data structures, specifically arrays and stacks, including their definitions, types, applications, and algorithms for various operations. It covers one-dimensional and two-dimensional arrays, their applications in symbol manipulation and sparse matrices, as well as stack operations like PUSH, POP, and PEEP. Additionally, it discusses recursion, algorithms for converting infix expressions to postfix, and a method for recognizing strings based on specific grammar.

Uploaded by

ramkarbhavik8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views20 pages

DS Unit 2

The document provides an in-depth explanation of linear data structures, specifically arrays and stacks, including their definitions, types, applications, and algorithms for various operations. It covers one-dimensional and two-dimensional arrays, their applications in symbol manipulation and sparse matrices, as well as stack operations like PUSH, POP, and PEEP. Additionally, it discusses recursion, algorithms for converting infix expressions to postfix, and a method for recognizing strings based on specific grammar.

Uploaded by

ramkarbhavik8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

SILVER OAK COLLEGE OF COMPUTER APPLICATION

SUBJECT: DATA
STRUCTURE
UNIT-2 : Linear Data Structure- Array and Stack

-PROF. ANKIT
PATEL
-- Explain Array in detail

❖ One Dimensional Array

❑ 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

❖ Symbol Manipulation using Array:


❑ We can use array for different kind of operations in polynomial equation such
as addition, subtraction, division, differentiation etc…
❑ We are interested in finding suitable representation for polynomial so that
different operations like addition, subtraction etc… can be performed in
efficient manner
❑ Array can be used to represent Polynomial equation
❑ Once we have algorithm for converting the polynomial equation to an array
representation and another algorithm for converting array to polynomial
equation, then different operations in array (matrix) will be corresponding
operations of polynomial equation
Applications of Array:
❑ 1. Symbol Manipulation (matrix representation of polynomial equation)
2. Sparse Matrix

❖ Sparse Matrix using Array:


❑ Array can be used to represent Polynomial equation
❑ An mXn matrix is said to be sparse if “many” of its elements are zero. · A
matrix that is not sparse is called a dense matrix.
❑ We can device a simple representation scheme whose space requirement
equals the size of the non-zero elements
❑ A more efficient representation in terms of storage requirement and access
time to the row of the matrix is shown in fid (d). The row vector changed so that
its ith element is the index to the first of the column indices for the element in
row I of the matrix.
Write algorithms for Stack Operations – PUSH, POP, PEEP :
❑ A linear list which allows insertion and deletion of an element at one end only is
called stack.
❑ The insertion operation is called as PUSH and deletion operation as POP.
❑ The most and least accessible elements in stack are known as top and bottom
of the stack respectively.
❑ Since insertion and deletion operations are performed at one end of a stack, the
elements can only be removed in the opposite orders from that in which they
were added to the stack; such a linear list is referred to as a LIFO (last in first
out) list.
❑ A pointer TOP keeps track of the top element in the stack. Initially, when the
stack is empty, TOP has a value of “one” and so on.
❑ Each time a new element is inserted in the stack, the pointer is incremented by
“one” before, the element is placed on the stack. The pointer is decremented
by “one” each time a deletion is made from the stack.
Applications of Stack :
❑ Recursion
❑ Keeping track of function calls
❑ Evaluation of expressions
❑ Reversing characters
❑ Servicing hardware interrupts
❑ Solving combinatorial problems using backtracking.
Procedure : PUSH (S, TOP, X)
❑ This procedure inserts an element x to the top of a stack which is represented by a
vector S containing N elements with a pointer TOP denoting the top element in the
stack.
1. [Check for stack overflow]
If TOP ≥ N Then
write (‘STACK OVERFLOW’)
Return
2. [Increment TOP]
TOP ←TOP + 1
3. [Insert Element]
S[TOP] ←X
4. [Finished]
Return
Function : POP (S, TOP)
❑ This function removes the top element from a stack which is represented by a
vector S and returns this element. TOP is a pointer to the top element of the
stack.
1. [Check for underflow of stack]
If TOP =0 Then
write (‘STACK UNDERFLOW ON POP’)
Take action in response to underflow
Return
2. [Decrement Pointer]
TOP ←TOP - 1
3. [Return former top element of stack]
Return (S[TOP + 1])
Function : PEEP (S, TOP, I)
❑ This function returns the value of the ith element from the TOP of the stack
which is represented by a vector S containing N elements. The element is not
deleted by this function.
1. [Check for stack Underflow]
If TOP - I +1 ≤ 0
Then
Write (‘STACK UNDERFLOW ON PEEP’)
Take action in response to Underflow
Exit
2. [Return Ith element from top of the stack]
Return
(S[TOP – I + 1])
Write an algorithm to change the ith value of stack to value X

PROCEDURE : CHANGE (S, TOP, X, I)


❑ This procedure changes the value of the Ith element from the top of the stack
to the value containing in X. Stack is represented by a vector S containing N
elements.

1. [Check for stack Underflow]


If TOP – I + 1 ≤ 0
Then
Write (‘STACK UNDERFLOW ON CHANGE’)
Return
2. [Change Ith element from top of the stack]
S[TOP – I + 1] ← X
3. [Finished]
Return
Write an algorithm which will check that the given string belongs to following
grammar or not. L={wcwR | w Є {a,b}*}(Where wR is the reverse of w)

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 ’)

You might also like