0% found this document useful (0 votes)
9 views11 pages

R programming unit 3

The document provides an overview of stacks and queues as fundamental data structures in computer science, detailing their definitions, memory representations, key operations, advantages, and disadvantages. It explains stack operations such as push, pop, and peek, and describes different types of queues, including simple, circular, double-ended, and priority queues. Additionally, it covers applications of these structures, including expression evaluation, recursion, and task scheduling.

Uploaded by

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

R programming unit 3

The document provides an overview of stacks and queues as fundamental data structures in computer science, detailing their definitions, memory representations, key operations, advantages, and disadvantages. It explains stack operations such as push, pop, and peek, and describes different types of queues, including simple, circular, double-ended, and priority queues. Additionally, it covers applications of these structures, including expression evaluation, recursion, and task scheduling.

Uploaded by

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

Faculty of Computer Applications

DS2T1 : Data Structures using C

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.

 Popping an element removes the top plate from the stack.

Memory Representation of Stacks :

There are two ways to represent stacks in memory:

1. Array representation (Static data structure)

2. Linked list representation (Dynamic data structure)


Array Representation of Stacks

1. D
e
f
i
n
i
t
i
o
n:

o A stack is implemented using a fixed-size array.

o The top variable tracks the index of the top element.

2. Key Operations:

o Push: Add an element to the top of the stack.

 Increment top and insert the element at arr[top].

o Pop: Remove the top element from the stack.

 Decrement top and return arr[top + 1].

o Peek: Return the top element without removing it.

 Access arr[top].

o isEmpty: Check if the stack is empty.

 Return true if top == -1.

o isFull: Check if the stack is full.

 Return true if top == MAX_SIZE - 1.

3. Advantages:

o Simple and easy to implement.

o Memory-efficient (no extra pointers).

4. Disadvantages:

o Fixed size, leading to potential stack overflow.

o Resizing the array dynamically is costly.

Linked Representation of Stacks

1. Definition:
o A stack is implemented using a linked list.

o Each node contains:

 data: The element.

 next: Pointer to the next node.

o The top pointer points to the top node.

2. Key Operations:

o Push: Add a new node at the beginning of the list.

 Create a new node, set its next to top, and update top.

o Pop: Remove the top node from the list.

 Store top->data, update top to top->next, and free the node.

o Peek: Return the data of the top node.

 Access top->data.

o isEmpty: Check if the stack is empty.

 Return true if top == NULL.

3. Advantages:

o Dynamic size (no fixed capacity).

o No risk of stack overflow (unless memory is exhausted).

4. Disadvantages:

o Slightly more complex due to pointer management.

o Extra memory overhead for storing pointers.

Comparison

Feature Array Representation Linked Representation

Size Fixed size Dynamic size

Memory Efficiency More memory efficient Less memory efficient

Implementation Simpler More complex

Performance Faster access (no pointers) Slightly slower (pointer overhead)

Overflow/Underflow Possible (fixed size) No overflow (unless memory exhausted)

When to Use Which?

 Array Representation:
o When the maximum stack size is known.

o When memory efficiency is critical.

o When simplicity and performance are priorities.

 Linked Representation:

o When the stack size is dynamic and unpredictable.

o When flexibility in resizing is required.

o When memory is not a constraint.

Stack as Abstract Data Type (ADT)

 ADT Definition: A stack is defined by its operations (push, pop, peek) without specifying the
implementation details.

-----------------------------------------------------------------------------------------------------------------------------------------------------

Arithmetic Expressions and Polish Notation

Arithmetic Expressions

Arithmetic expressions are combinations of operands (numbers or variables) and operators (like +, -, *, /)
that represent a computation.

Types of Arithmetic Expressions:

1. Infix Notation:

o Operators are placed between operands.

o Example: A + B, (A * B) + C.

o Most common and human-readable form.

o Requires parentheses to define precedence.

2. Prefix Notation (Polish Notation):

o Operators are placed before operands.

o Example: + A B, + * A B C.

o No need for parentheses; precedence is unambiguous.

3. Postfix Notation (Reverse Polish Notation):

o Operators are placed after operands.

o Example: A B +, A B * C +.

o No need for parentheses; precedence is unambiguous.


2. Polish Notation (Prefix Notation)

Polish Notation is a way of writing arithmetic expressions where the operator precedes its operands. It was
introduced by Jan Łukasiewicz.

Key Features:

 No parentheses are needed to define precedence.

 Easier to evaluate using stacks.

 Example: + A B means A + B.

Evaluation of Prefix Notation:

1. Scan the expression from right to left.

2. Push operands onto the stack.

3. When an operator is encountered:

o Pop the top two operands from the stack.

o Apply the operator.

o Push the result back onto the stack.

4. The final result is the only element left in the stack.

Example:

Evaluate + * 5 4 3:

1. Scan from right: 3 → push to stack.

2. 4 → push to stack.

3. 5 → push to stack.

4. * → pop 5 and 4, compute 5 * 4 = 20, push 20.

5. + → pop 20 and 3, compute 20 + 3 = 23, push 23.

6. Result: 23.

3. Reverse Polish Notation (Postfix Notation)

Reverse Polish Notation is a way of writing arithmetic expressions where the operator follows its operands.

Key Features:

 No parentheses are needed to define precedence.

 Easier to evaluate using stacks.

 Example: A B + means A + B.

Evaluation of Postfix Notation:


1. Scan the expression from left to right.

2. Push operands onto the stack.

3. When an operator is encountered:

o Pop the top two operands from the stack.

o Apply the operator.

o Push the result back onto the stack.

4. The final result is the only element left in the stack.

Example:

Evaluate 5 4 * 3 +:

1. Scan from left: 5 → push to stack.

2. 4 → push to stack.

3. * → pop 4 and 5, compute 5 * 4 = 20, push 20.

4. 3 → push to stack.

5. + → pop 3 and 20, compute 20 + 3 = 23, push 23.

6. Result: 23.

4. Conversion Between Notations

Infix to Postfix (Using Stack):

1. Initialize an empty stack and an empty output list.

2. Scan the infix expression from left to right.

3. If the token is an operand, add it to the output list.

4. If the token is an operator:

o Pop from the stack to the output list until the stack is empty or the top has lower
precedence.

o Push the current operator onto the stack.

5. If the token is (, push it onto the stack.

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 *

5. Applications of Polish Notation

 Used in stack-based calculators (e.g., HP calculators).

 Simplifies expression evaluation in compilers and interpreters.

 Eliminates ambiguity in expressions without parentheses.

No. Infix Notation Prefix Notation Postfix Notation

1 a+b +ab ab+

2 (a + b) ∗ c ∗+abc ab+c∗

3 a ∗ (b + c) ∗a+bc abc+∗

4 a/b+c/d +/ab/cd ab/cd/+

5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗

6 ((a + b) ∗ c) - d -∗+abcd ab+c∗d-

----------------------------------------------------------------------------------------------------------------------------------------------

Applications of Stacks

1. Expression Evaluation: Evaluate postfix expressions.

2. Expression Conversion: Convert infix to postfix or prefix.

3. Recursion: Implicitly handled using a function call stack.

4. Towers of Hanoi: Solve using recursion or stack simulation.


5. Undo/Redo Operations: Used in text editors.

Recursion and Stacks

 Recursion uses a function call stack to store intermediate states.

 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

 Problem: Move disks from source to destination using an auxiliary rod.

 Recursive Solution:

o Move disks from source to auxiliary.

o Move the -th disk from source to destination.

o Move disks from auxiliary to destination.

Implementation of Recursive Procedures by Stack

 Replace recursive calls with an explicit stack:

o Push the parameters of the function onto the stack.

o Pop and execute until the stack is empty.


Queues

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.

Array Representation of Queues

1. Structure:

o Represented as an array with two pointers: front and rear.

2. Operations:

o Enqueue (Insertion): Add an element to the rear of the queue.

 Check if the queue is full.

 Increment rear and insert the element.

o Dequeue (Deletion): Remove an element from the front of the queue.

 Check if the queue is empty.

 Remove the element and increment front.

o Peek/Front: Retrieve the front element without removing it.

o IsEmpty: Check if the queue is empty.

o IsFull: Check if the queue is full.

Linked List Representation of Queues

1. Structure:

o Each element is a node in a linked list.

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

1. Simple Queue: Follows the basic FIFO principle.

2. Circular Queue:

o Rear and front are connected to make the queue circular.


o Prevents wastage of space in array representation.

3. Double-Ended Queue (Deque):

o Insertion and deletion are allowed at both ends.

o Types:

 Input-Restricted Deque: Insertion allowed only at one end.

 Output-Restricted Deque: Deletion allowed only at one end.

4. Priority Queue:

o Each element has a priority.

o Elements are dequeued based on priority, not arrival time.

Operations on Queues

1. Enqueue: Add an element to the queue.

2. Dequeue: Remove an element from the queue.

3. Peek/Front: Retrieve the front element without removing it.

4. IsEmpty: Check if the queue is empty.

5. IsFull: Check if the queue is full (in array representation).

Applications of Queues

1. Task Scheduling: CPU scheduling (e.g., round-robin).

2. Resource Sharing: Printer queue.

3. Breadth-First Search (BFS): Used in graph traversal.

4. Data Buffering: Used in IO buffers.

5. Simulation: Model real-world systems like ticket counters.

You might also like