Cs2x1 Dsa Spring 2023 l1
Cs2x1 Dsa Spring 2023 l1
Koteswararao Kondepu
[email protected]
Outline
• Data type is an attribute of data, which tells the compiler (or interpreter) how
the programmer intended to use the data.
Linked
Arrays Stack Queues Trees Graphs
List
Define: Data Structures
• IS all the above properties are necessary to solve the given problem?
a:
Index 0 1 2 3 4 5
• Major Operations:
• Why we have only two operations?
• An element is inserted in a stack --- PUSH
• An element is removed from the stack --- POP
• Exceptions:
• Underflow – Trying to POP from an empty stack
• Overflow – Trying to PUSH an element in a full stack
Stack: PUSH Examples
PUSH (5) PUSH (8)
S
5 5
5
4 4
4
3 3
3
2 2 2
1 1 1 S [1] = 8
0 TOP = 0 0 S [0] = 5 0 5 TOP = 1 + 1
Stack is empty TOP = 0 + 1
PUSH (15)
5 PUSH (S, TOP, Val)
4 begin
3 S[TOP] = Val;
2 S [2] = 15 TOP = TOP + 1;
1 8 TOP = 2 + 1 end
0 5
Stack: POP Examples
S = POP() S = POP()
S = POP() 5 5
5 4 4
4 3 3
TOP = 2 - 1
3 TOP = 3 - 1 2 return S[1] 2
2 15 return S[2] 1 8 1 TOP = 1 - 1
1 0 0
8 5 5 return S[0]
0
5
➢ When you see the close brace, just pop its open brace from the stack
For every open brace,
➢ No braces then ignore other things there should be a close
brace { () [] }
Stack: Applications – Examples
• Following sequence of operations is performed on the stack
push (20), push (30), pop(), push (20), push (30), pop(), pop(),
pop(),push(30), pop(). Sequence of popped elements:
a) 30, 20, 30, 20, 30 b) 30, 30, 20, 20, 30 c) 20, 30, 30, 20, 30
Stack: Applications – Examples
• Following sequence of operations is performed on the stack
push (50), push (40), pop(), push (40), push (-30), pop(), pop(),
t=pop(),push(-10), t=pop(). What is the value of t+20?
a) -10 b) 10 c) 60 d) 70
Stack: Applications – Examples (1)
• What is the output of the program for the following input?
63*443+*+
a) 38 b) 46 c) 40 d) 42
thank you!
email:
[email protected]