Data Structure Unit I
Data Structure Unit I
3.push() function is used to insert new elements into the Stack and pop()
function is used to remove an element from the stack.
Both insertion and removal are allowed at only one end of Stack called
Top.
4.Stack is said to be in Overflow state when it is completely full
and is said to be in Underflow state if it is completely empty.
Algorithm for PUSH operation
• Check if the stack is full or not.If the stack is full, then print error of
overflow and exit the program. If the stack is not full, then increment
the top and add the element.
• 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
Algorithm for POP operation
• Check if the stack is empty or not. If the stack is empty, then print
error of underflow and exit the program. If the stack is not empty,
then print the element at the top and decrement the top.
• 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
Position of the TOP
Postfix Notation
• This notation style is known as Reversed Polish Notation. In this
notation style, the operator is postfixed to the operands i.e., the
operator is written after the operands. For example, ab+. This is
equivalent to its infix notation a + b.
Some examples of infix, prefix and
postfix
• Like stack, queue is also an ordered list of elements of similar data types.
• Queue is a FIFO( First in First Out ) structure. Once a new element is inserted
into the Queue, all the elements inserted before the new element in the
queue must be removed, to remove the new element.
• peek( ) function is oftenly used to return the value of first element without
dequeuing it.
Applications of Queue
• Queue, as the name suggests is used whenever we need to manage
any group of objects in an order in which the first one coming in, also
gets out first while the others wait for their turn, like in the following
scenarios:
1. Serving requests on a single shared resource, like a printer, CPU task
scheduling etc.
2. In real life scenario, Call Center phone systems uses Queues to hold
people calling them in an order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are
handled in the same order as they arrive i.e First come first served.
Implementation of Queue Data
Structure
• Queue can be implemented using an Array, Stack or Linked List. The easiest
way of implementing a queue is by using an Array.
• Initially the head(FRONT) and the tail(REAR) of the queue points at the first
index of the array (starting the index of array from index 0.
• As we add elements to the queue, the REAR keeps on moving ahead, always
pointing to the position where the next element will be inserted, while the
FRONT remains at the first index.
Implementation of Queue Data
Structure
Implementation of Queue Data
Structure
• When we remove an element from Queue, we can follow two
possible approaches (mentioned [A] and [B] in above diagram). In [A]
approach, we remove the element at head position, and then one by
one shift all the other elements in forward position.
• In approach [B] we remove the element from head position and then
move head to the next position.
Algorithm for ENQUEUE
operation
Check if the queue is full or not. If the queue is full, then print overflow error
and exit the program. If the queue is not full, then increment the tail and add
the element.
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 DEQUEUE
operation
Check if the queue is empty or not. If the queue is empty, then print
underflow error and exit the program. If the queue is not empty, then print
the element at the head and increment the head.
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.
Additional Functions can be
done on Queues
• peek() − Gets the element at the front of the queue without removing
it.
1. Simple Queue
2. Circular Queue
3. Priority Queue
4. Dequeue (Double Ended Queue)
Simple Queue
• Simple queue defines the simple operation of queue in which
insertion occurs at the rear of the list and deletion occurs at the front
of the list.
Dequeue (Double Ended Queue)
• In Double Ended Queue, insert and delete operation can be occur at
both ends that is front and rear of the queue.
• Two types of Dequeue:
• Input restricted Dequeue
• Output restricted Dequeue
Priority Queue
• Priority queue contains data items which have some preset priority.
While removing an element from a priority queue, the data item with
the highest priority is removed first.