IDS - Unit-4
IDS - Unit-4
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
• Undo/Redo operations
• Expression evaluation
• Browser history
• Balanced Parentheses
• Backtracking Algorithms
Expression Evaluation
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?
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.
• The storage requirement of linked representation of a queue with n elements is o(n) while the time requirement
• 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
ELSE
END OF IF
•Step 4 END
Deletion
Go to Step 5
END OF IF
•Step 5 END
Priority Queue
• A priority queue is an abstract data-type similar to a regular queue or stack data structure.
• 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:
• 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.
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.
•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.
● 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
Sizing
Use of Stack in a Text Editor Application
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.