0% found this document useful (0 votes)
33 views80 pages

IDS - Unit-4

The document outlines the syllabus for a first-year engineering course on data structures, focusing on stacks, queues, and hashing. It details the concepts, operations, and applications of these data structures, including algorithms for push and pop operations for stacks, enqueue and dequeue operations for queues, and collision resolution strategies for hash tables. Additionally, real-world applications and case studies are provided to illustrate the use of these data structures in various scenarios.

Uploaded by

rc47363637
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)
33 views80 pages

IDS - Unit-4

The document outlines the syllabus for a first-year engineering course on data structures, focusing on stacks, queues, and hashing. It details the concepts, operations, and applications of these data structures, including algorithms for push and pop operations for stacks, enqueue and dequeue operations for queues, and collision resolution strategies for hash tables. Additionally, real-world applications and case studies are provided to illustrate the use of these data structures in various scenarios.

Uploaded by

rc47363637
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/ 80

MIT Art, Design and Technology University

MIT School of Computing, Pune


Department of Computer Science and Engineering
Class: First Year Engineering
Subject : Introduction to Data Structure (23INT1102)

Unit-IV –Stack, Queue and Hashing

Prof.Chaya Maske & Mrs. Babeetta Bbhagat

AY 2024-2025 SEM-II
Syllabus
Topic 1: Stacks- concept, Stack ADT, Representation of Queues (array and linked list),
Primitive operations (push, pop, top, etc.), Applications of Stack- Expression Evaluation
and Conversion, Case Study: Use of Stack in a Text Editor Application

Topic 2: Queue- Concept, Queue ADT, Representation of Queues (array and linked list),
Primitive operations (insert, delete, etc.), Applications of Queue- Priority queue in
bandwidth management. Case Study: Use of Queue in a Ticketing System for an
Amusement Park

Topic 3: Hash Table- Concepts-hash table, hash functions, collision, open hashing,
closed hashing, Collision resolution strategies- open addressing and chaining. Case
Study: Use of Hashing in a Password Management System
What is Stack?

• 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.
Stack ADT
• In Stack ADT Implementation instead of data being stored in each
node, the pointer to data is stored.
• The program allocates memory for the data and address is passed to
the stack ADT.
• The head node and the data nodes are encapsulated in the
ADT. The calling function can only see the pointer to the stack.
• The stack head structure also contains a pointer to top and count of number of
entries currently in stack.
• A Stack contains elements of the same type arranged in sequential order. All
operations take place at a single end that is top of the stack and following
operations can be performed:
• push() – Insert an element at one end of the stack called top.
• pop() – Remove and return the element at the top of the stack, if it is not empty.
• peek() – Return the element at the top of the stack without removing it, if the stack is not
empty.
• size() – Return the number of elements in the stack.
• isEmpty() – Return true if the stack is empty, otherwise return false.
• isFull() – Return true if the stack is full, otherwise return false.
Push Operation
• The process of putting a new data element onto stack is
known as a Push Operation. Push operation involves a
series of steps −
• Step 1 − Checks if the stack is full.
• Step 2 − If the stack is full, produces an error and exit.
• Step 3 − If the stack is not full, increments top to point
next empty space.
• Step 4 − Adds data element to the stack location, where
top is pointing.
• Step 5 − Returns success.
Algorithm for PUSH Operation
A simple algorithm for Push operation can be derived as
follows −
begin procedure push: stack, data
if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
Pop Operation
• Accessing the content while removing it from the stack, is known as a Pop Operation.
In an array implementation of pop() operation, the data element is not actually
removed, instead top is decremented to a lower position in the stack to point to the next
value. But in linked-list implementation, pop() actually removes data element and
deallocates memory space.
• A Pop operation may involve the following steps −
• Step 1 − Checks if the stack is empty.
• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.
Algorithm for Pop Operation

A simple algorithm for Pop operation can be derived as


follows −

begin procedure pop: stack


if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
Implement a stack using singly linked list
Implement a stack using singly linked list
Real time examples of stack:
Stacks are used in various real-world scenarios where a last-in, first-out (LIFO)
data structure is required. Here are some examples of real-time applications of
stacks:

• Function call stack


• Undo/Redo operations
• Browser history
• Expression evaluation
• Call stack in recursion
Application of Stack Data Structure:

• Function calls and recursion

• Undo/Redo operations

• Expression evaluation

• Browser history

• Balanced Parentheses

• Backtracking Algorithms
Expression Evaluation
Infix Expressions

The usual expressions which we encounter are infix expressions.

For example, (A + B) * C / D - E

Postfix Expressions

In postfix expressions, the operators are written after the operands as shown below:

ABC+*D/

Prefix Expressions

Here, the operators are written before the operands. An example is,

/*A+BCD
To evaluate post/prefix Expression 4+5*6
What is Queue Data Structure?

A Queue is defined as a linear data structure that is open at both


ends and the operations are performed in First In First Out
(FIFO) order.
FIFO Principle of Queue
Queue ADT
• The queue abstract data type (ADT) follows the basic design of the stack
abstract data type.
• Each node contains a void pointer to the data and the link
pointer to the next element in the queue. The program’s responsibility is to
allocate memory for storing the data.
• A Queue contains elements of the same type arranged in sequential order.
Operations take place at both ends, insertion is done at the end and deletion is
done at the front. Following operations can be performed:
• enqueue() – Insert an element at the end of the queue.
• dequeue() – Remove and return the first element of the queue, if the queue is
not empty.
• peek() – Return the element of the queue without removing it, if the queue is not
empty.
• size() – Return the number of elements in the queue.
• isEmpty() – Return true if the queue is empty, otherwise return false.
• isFull() – Return true if the queue is full, otherwise return false.
Insertion in a Queue( Enqueue)
• Queues maintain two data pointers, front and rear.
Therefore, its operations are comparatively difficult to implement than that of
stacks.
• The following steps should be taken to enqueue (insert) data into a queue −
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the next
empty space.
• Step 4 − Add data element to the queue location, where the rear is pointing.
• Step 5 − return success.
Algorithm for enqueue operation

procedure enqueue(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
Dequeue Operation
• Accessing data from the queue is a process of two tasks − access the data
where front is pointing and remove the data after access. The following steps
are taken to perform dequeue operation −
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and exit.
• Step 3 − If the queue is not empty, access the data where front is pointing.
• Step 4 − Increment front pointer to point to the next available data element.
• Step 5 − Return success.
Algorithm for dequeue operation

procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
Linked List implementation of Queue
• The array implementation can not be used for the large scale applications where the queues are implemented.

• One of the alternative of array implementation is linked list implementation of queue.

• The storage requirement of linked representation of a queue with n elements is o(n) while the time requirement

for operations is o(1).

• In a linked queue, each node of the queue consists of two parts i.e. data part and the link part.

• Each element of the queue points to its immediate next element in the memory.

• In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear pointer.

• The front pointer contains the address of the starting element of the queue while the rear pointer contains the

address of the last element of the queue.


Insertion and deletions are performed at rear and front end respectively.
If front and rear both are NULL, it indicates that the queue is empty.
The linked representation of queue is shown in the following figure.
Algorithm for insertion

•Step 1 Allocate the space for the new node PTR

•Step 2 SET PTR  DATA  VAL

•Step 3 IF FRONT  NULL

SET FRONT  REAR  PTR

SET FRONT  NEXT  REAR  NEXT  NULL

ELSE

SET REAR  NEXT  PTR

SET REAR  PTR

SET REAR  NEXT  NULL

END OF IF

•Step 4 END
Deletion

•Step 1 IF FRONT  NULL

Write " Underflow "

Go to Step 5

END OF IF

•Step 2 SET PTR  FRONT

•Step 3 SET FRONT  FRONT  NEXT

•Step 4 FREE PTR

•Step 5 END
Priority Queue
• A priority queue is an abstract data-type similar to a regular queue or stack data structure.

Each element in a priority queue has an associated priority.

• In a priority queue, elements with high priority are served before elements with low priority.

• In some implementations, if two elements have the same priority, they are served in the same order in which

they were enqueued. In other implementations, the order of elements with the same priority is undefined.
Applications:

• Dijkstra’s Shortest Path Algorithm using priority queue

• Prim’s algorithm

• Data compression

• Optimization problems

• Robotics

• Event-driven simulations

• Medical systems
Hash tables
• Hash Table is a data structure which stores data in an associative manner.
• In a hash table, data is stored in an array format, where each data value has its
own unique index value.
• Access of data becomes very fast if we know the index of the desired data.
• Thus, it becomes a data structure in which insertion and search operations are
very fast irrespective of the size of the data.
• Hash Table uses an array as a storage medium and uses hash technique to
generate an index where an element is to be inserted or is to be located from.
Hash
Table
A Hash table is a data structure that is used to store the data in
key-value pairs.
•Key- unique integer that is used for indexing the values
•Value - data that are associated with keys.
Hashing
• Hashing is a technique to convert a range of key values into a range of indexes
of an array.
Structure of Hash Table
Key : Key a unique value and input of hash
function.
Value : The value that is finally stored in a
bucket and it must be paired with a key.
Hash function : Compute the key to hash
code.
Hash or Hash code : The result of a hash
function and stored in a bucket paired with a
value.
Bucket or Slot - Where the values are
stored.
Basic Operations for Hash Table
•Search - Search a value with a key.

•Insert - Compute key to hash through hash function and insert data.

•Delete - Delete a hash and value that matches a key.


Hash Function
•A hash function is used to determine the index of the
key-value pair.

•It is always recommended that we should choose a good


hash function for creating a good hash table.

•Besides a good hash function, it should be a one-way


function, i.e. we should be able to get the hash value from
the key and not vice versa.

•Also, it should avoid producing the same hash for different


keys.
HASH COLLISION
Problem with Hash Function :
Hash Collision : When different keys become the same hash code through
a hash function, it is called Hash Collision.
Collision :
•When the hash function generates the same index for multiple keys, there will be a conflict
(what value to be stored in that index). This is called a hash collision.
•When the two different values have the same value, then the problem occurs between the two
values, known as a collision.
Collision Resolution Strategies
1. Chaining
•In the separate chaining, each bucket is independent.
•Each bucket has some sort of list of entries with the same index.
•When a collision occurs in a bucket, it links new data next to the existing data.
Example 1 : If we have some elements like {15, 47, 23, 34, 85, 97, 65, 89, 70}.
Hash function is h(x) = x mod 7.

The hash values will be


The Hashing with chaining will be like −
Example 2 : Let us consider a simple hash function as “key mod 7” and
a sequence of keys as 50, 700, 76, 85, 92, 73, 101
Advantages & Disadvantages of Chaining
Open Addressing
•In Open Addressing, all elements are stored in the hash table itself.
•So at any point, the size of the table must be greater than or equal to the total
number of keys (Note that we can increase table size by copying old data if
needed).
•This approach is also known as closed hashing.
•This entire procedure is based upon probing.
Types of probing
•Insert(k): Keep probing until an empty slot is found. Once
an empty slot is found, insert k.
•Search(k): Keep probing until the slot’s key doesn’t become
equal to k or an empty slot is reached.
•Delete(k): Delete operation is interesting. If we simply
delete a key, then the search may fail. So slots of deleted
keys are marked specially as “deleted”.
The insert can insert an item in a deleted slot, but the search
doesn’t stop at a deleted slot.
Different ways of Open Addressing

1. Linear Probing:
In linear probing, the hash table is searched sequentially that starts from
the original location of the hash. If in case the location that we get is
already occupied, then we check for the next location.

Let hash(x) be the slot index computed using a hash function and S be the
table size
If slot hash(x) % S is full, then we try (hash(x) + 1) % S
If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S
If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S
Example : Let us consider a simple hash function as “key mod 7” and a sequence of keys as 50, 700, 76, 85,
92, 73, 101,
which means hash(key)= key% S, here S=size of the table =7,indexed from 0 to 6.We can define the hash
function as per our choice if we want to create a hash table, although it is fixed internally with a pre-defined
formula.
2. Quadratic Probing:
•Quadratic probing is a method with the help of which we can solve the
problem of clustering that was discussed above.
•This method is also known as the mid-square method. In this method, we
look for the i2‘th slot in the ith iteration.
•We always start from the original hash location. If only the location is
occupied then we check the other slots.
let hash(x) be the slot index computed using hash function.
If slot hash(x) % S is full, then we try (hash(x) + 1*1) % S
If (hash(x) + 1*1) % S is also full, then we try (hash(x) + 2*2) % S
If (hash(x) + 2*2) % S is also full, then we try (hash(x) + 3*3) % S
Example: Let us consider table Size = 7, hash function as Hash(x) =
x % 7 and collision resolution strategy to be f(i) = i2 . Insert = 22, 30,
and 50.
Step 1: Create a table of size 7.

Hash table
Step 2 – Insert 22 and 30
Hash(22) = 22 % 7 = 1, Since the cell at index 1 is
empty, we can easily insert 22 at slot 1.
Hash(30) = 30 % 7 = 2, Since the cell at index 2 is
empty, we can easily insert 30 at slot 2.

Insert keys 22 and 30 in the hash table


Step 3: Inserting 50
Hash(50) = 50 % 7 = 1
In our hash table slot 1 is already occupied. So, we will search
for slot 1+12, i.e. 1+1 = 2,
Again slot 2 is found occupied, so we will search for cell 1+22,
i.e.1+4 = 5,
Now, cell 5 is not occupied so we will place 50 in slot 5.
3. Double Hashing

•The intervals that lie between probes are computed by another hash function.
•Double hashing is a technique that reduces clustering in an optimized way.
•In this technique, the increments for the probing sequence are computed by
using another hash function.
•We use another hash function hash2(x) and look for the i*hash2(x) slot in
the ith rotation.

let hash(x) be the slot index computed using hash function.


If slot hash(x) % S is full, then we try (hash(x) + 1*hash2(x)) % S
If (hash(x) + 1*hash2(x)) % S is also full, then we try (hash(x) + 2*hash2(x)) % S
If (hash(x) + 2*hash2(x)) % S is also full, then we try (hash(x) + 3*hash2(x)) % S
Example: Insert the keys 27, 43, 692, 72 into the Hash Table of size
7. where first hash-function is h1(k) = k mod 7 and second
hash-function is h2(k) = 1 + (k mod 5)
Step 1: Insert 27
27 % 7 = 6, location 6 is empty so insert 27 into 6 slot.

Insert key 27 in the hash table


Step 2: Insert 43
43 % 7 = 1, location 1 is empty so insert 43 into 1 slot.

Insert key 43 in the hash table


•Step 3: Insert 692
•692 % 7 = 6, but location 6 is already being occupied and this is a collision
•So we need to resolve this collision using double hashing.

hnew = [h1(692) + i * (h2(692)] % 7


= [6 + 1 * (1 + 692 % 5)] % 7
= 9% 7
=2
Now, as 2 is an empty slot,
so we can insert 692 into 2nd slot.
•Step 4: Insert 72
•72 % 7 = 2, but location 2 is already being occupied and
this is a collision.
•So we need to resolve this collision using double
hashing.
hnew = [h1(72) + i * (h2(72)] % 7
= [2 + 1 * (1 + 72 % 5)] % 7
=5%7
= 5,
Now, as 5 is an empty slot,
so we can insert 72 into 5th slot.
Advantage of Open Addressing

•Because no additional storage is needed, no additional work is required.


•By not using a pointer, serialization is easy.
Disadvantage of Open Addressing
•The performance of a hash table depends on the performance of a hash function.
•The number of stored nodes cannot exceed the number of slots in the bucket array.
CASE STUDY OF STACK

Use of Stack in a Text Editor Application


What is Stack?

● A pile of books.
● It follows the "Last-In, First-Out" (LIFO) principle
● commonly used in computer programming for managing data and function
calls.
How to create Stack?
1.Choose a data structure like an array or linked list.
2.Define push (add), pop (remove), peek (view top item), and isEmpty
operations.
3.Implement these operations in your chosen data structure.
4.Test your stack with various operations to ensure it works correctly.
Optionally, refine and extend your stack as needed for your specific
requirements
Uses of Stack

1.Managing function calls and returns.


2.Evaluating mathematical expressions.
3.Implementing undo mechanisms.
4.Backtracking algorithms.
5.Syntax parsing in programming languages.
6.Memory management, particularly in call stacks.
7.Converting between expression notations.
8.Implementing various algorithms such as depth-first search and tree
traversals.
Advantages of Stacks Disadvantages of Stacks

Simple Implementation Limited Access (LIFO)

Efficient Memory Usage No Random Access

Easily Reversible Operations Fixed Size

Recursive Function Execution Inefficient for Certain Operations

Used in Function Calls Requires Extra Overhead for Dynamic

Sizing
Use of Stack in a Text Editor Application

•Stacks play a crucial role in the implementation of various features in a


text editor application.
1.Undo/Redo Functionality:
2.Text Selection and Clipboard:
3.Indentation Levels
4.Parentheses Matching:
5.Find and Replace History:
6.Navigation History:
CODE EXPLANATION

1.Structures: It defines structures for commands and stacks. Commands represent text
insertion actions, while stacks are used to store the commands.
•Stack Operations: Functions are provided to initialize a stack, check if it is empty or full, push
commands onto it, and pop commands from it.
2.Main Function:
-It prompts the user to enter the size of the stack.
-It enters a loop to repeatedly prompt the user for actions until they choose to quit.
-Supported actions include inserting text and performing undo operations.
-If the stack is full, the program notifies the user of a "Stack Overflow."
-If the user tries to undo when the stack is empty, it notifies of a "Stack Underflow.“
- The program dynamically allocates memory for the stack based on user input and deallocates
it before exiting
-
•Printing Stack Contents: It provides a function to print the current contents of the stack after
each insert or undo operation.
Queue Usage in a Ticketing
System for an Amusement
Park
Queue Usage in a Ticketing System for an Amusement Park
Introduction
An amusement park ticketing system is a critical component of park operations, managing the sale and
distribution of tickets to visitors. Queues play a vital role in this system, ensuring fair and efficient ticket
processing for guests.

Use of Queue in an Amusement Park Ticketing System


1.Fairness: Ensures that tickets are sold and processed in the order guests arrive.
2.Efficiency: Prevents long waiting times by managing ticket sales in a structured manner.
3.Resource Management: Helps allocate resources, such as ticket counters and staff, effectively based on queue
length.
Array Usage in a Stock
Management System
USE OF ARRAY
Due to their ability to store multiple elements of the same type in a contiguous
memory block arrays are widely used in programming for various purposes.
Data storage and retrieval
Sequential Access
Matrices and Multi-dimensional Arrays
Sorting and Searching
Buffers and Caches
Stacks and Queues
Image and Signal Processing
ETC
Stock Management System
A stock management system, also known as inventory management system, is
a set of processes, tools, and technologies designed to efficiently track,
manage, and control a company's inventory or stock of goods.
The primary goal of a stock management system is to ensure that products are
available when needed, minimize excess stock, and streamline the overall
supply chain.
Inventory Tracking
Order Processing
Demand Forecasting
Supplier Management
Multi-Channel Integration
Scalability
In the context of stock management, arrays are
valuable for organizing and managing data
efficiently.
Fixed Size :- In stock management, this fixed size can represent a predetermined capacity
for storing product information or stock levels.
Direct Access with Indices :- This feature is beneficial in stock management for quick
access to specific product details or retrieving stock levels by referencing the corresponding
indices.
Contiguous Memory Allocation :- This contiguous memory allocation is advantageous in
stock management systems, allowing for efficient access to and manipulation of inventory
data.
Homogeneous Elements : In stock management, this means that information related to
products or stock items is uniform, making it easier to process and manage.
Efficient Data Retrieval :- This is crucial in stock management for quickly accessing
product details, stock levels, and other relevant information.
Orderly Storage :- In stock management, this orderly storage is essential for organizing
product information, tracking stock levels, and managing inventory effectively.
Optimized for Iteration :- In stock management, this is beneficial for tasks like iterating through the
product list, updating stock levels, or processing orders in a systematic manner.
Memory Efficiency:- In stock management systems, efficient memory usage is crucial for handling
large amounts of product information and stock data.
Scalability Considerations :- While arrays have a fixed size, their use prompts considerations for
scalability in stock management systems. The fixed size nature may require careful planning for
potential growth in the number of products or stock items.
Using arrays in stock management systems has
several advantages:
1.Efficient Access: Arrays provide quick access to specific product details and stock
levels.
2.Ordered Storage: Products are stored in a structured and organized manner.
3.Simplified Processing: Homogeneous elements simplify data processing tasks.
4.Optimized for Iteration: Well-suited for efficient iteration through lists of products.
5.Memory Efficiency: Arrays are memory-efficient for handling large datasets.
6.Predictable Performance: Offers consistent and predictable performance for basic
operations.
7.Simplicity and Readability: Provides a simple and readable way to represent and
manage inventory data.

You might also like