R programming unit 3
R programming unit 3
II Semester BCA
Prepared By
Sabnam Pradhan
Professor and Faculty of Computer Applications
Unit-III
Stacks -Definition, Array representation of stacks, Linked representation of stacks, Stack as
ADT, Arithmetic Expressions: Polish Notation, Application of Stacks, Recursion, Towers of
Hanoi, Implementation of recursive procedures by stack.
Queues– Definition, Array representation of queue, Linked list representation of queues Types of
queue: Simple queue, Circular queue, Double ended queue, Priority queue, Operations on
Queues, Applications of queues
--------------------------------------------------------------------------------------------------------------------------
Stacks
Definition
A Stack is a linear data structure that follows a particular order in which the operations are performed. The
order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is
inserted last, comes out first and FILO implies that the element that is inserted first, comes out last.
It behaves like a stack of plates, where the last plate added is the first one to be removed. Think of it this
way:
Pushing an element onto the stack is like adding a new plate on top.
1. D
e
f
i
n
i
t
i
o
n:
2. Key Operations:
Access arr[top].
3. Advantages:
4. Disadvantages:
1. Definition:
o A stack is implemented using a linked list.
2. Key Operations:
Create a new node, set its next to top, and update top.
Access top->data.
3. Advantages:
4. Disadvantages:
Comparison
Array Representation:
o When the maximum stack size is known.
Linked Representation:
ADT Definition: A stack is defined by its operations (push, pop, peek) without specifying the
implementation details.
-----------------------------------------------------------------------------------------------------------------------------------------------------
Arithmetic Expressions
Arithmetic expressions are combinations of operands (numbers or variables) and operators (like +, -, *, /)
that represent a computation.
1. Infix Notation:
o Example: A + B, (A * B) + C.
o Example: + A B, + * A B C.
o Example: A B +, A B * C +.
Polish Notation is a way of writing arithmetic expressions where the operator precedes its operands. It was
introduced by Jan Łukasiewicz.
Key Features:
Example: + A B means A + B.
Example:
Evaluate + * 5 4 3:
2. 4 → push to stack.
3. 5 → push to stack.
6. Result: 23.
Reverse Polish Notation is a way of writing arithmetic expressions where the operator follows its operands.
Key Features:
Example: A B + means A + B.
Example:
Evaluate 5 4 * 3 +:
2. 4 → push to stack.
4. 3 → push to stack.
6. Result: 23.
o Pop from the stack to the output list until the stack is empty or the top has lower
precedence.
6. If the token is ), pop from the stack to the output list until ( is encountered.
7. Pop any remaining operators from the stack to the output list.
Example:
Convert (A + B) * C to postfix:
1. Output: A
2. Stack: (
3. Output: A B
4. Stack: ( +
5. Output: A B +
6. Stack: *
7. Output: A B + C
8. Result: A B + C *
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
----------------------------------------------------------------------------------------------------------------------------------------------
Applications of Stacks
Implementation:
o Each recursive call pushes the current state onto the stack.
o When the base condition is met, states are popped off the stack.
Towers of Hanoi
Recursive Solution:
Definition
A queue is a linear data structure that follows the First In, First Out (FIFO) principle. This means that the
first element added to the queue is the first one to be removed.
1. Structure:
2. Operations:
1. Structure:
o Two pointers, front and rear, point to the first and last nodes.
2. Operations:
o Enqueue: Create a new node and link it to the current rear. Update rear.
o Dequeue: Remove the node at front and update front to the next node.
Types of Queues
2. Circular Queue:
o Types:
4. Priority Queue:
Operations on Queues
Applications of Queues