You are on page 1of 2

Data Structure

Data Structure is a way of collecting and organizing data in such a way that we
can perform operations on these data in an effective way.
Stack
A stack is a data structure whose elements are accessed according to the Last-In
First-Out (LIFO) principle. This is because in a stack, insertion and deletion of
elements can only take place at one end, called top of the stack.
Consider the following examples of stacks:
1. A pile of books
2. A stack of coins
3. Ten glass plates placed one above another.

Operations on stack
 Push operation: It means inserting element at the top of the stack. This can be
done with the help of append() method of the list as: st.append(element) where ‘st’
is a list.

We can add elements to a stack until it is full. Trying to add an element to a full
stack results in an exception called ‘overflow’.

 Pop operation: It means removing the topmost element from the stack. This can
be performed using pop() method of the list as: st.pop() where ‘st’ is a list. This
method returns the removed element that can be displayed.

We can delete elements from a stack until it is empty i.e. there is no element in it.
Trying to delete an element from an empty stack results in an exception called
‘underflow

Implementation of stack

Write the program given during class

Queue
A queue is a data structure where the element that is added first will be deleted
first. This is called FIFO (First In First Out) order. A queue has two ends, called the
front end and rear end. Items are always added from the rear end and removed
from the front end. The process of adding items to queue is called insert operation
or enqueue and removal of items from the queue is called delete operation or
dequeue.
Consider the following examples of queues:
1. The consumer who comes first to a shop will be served first.
2. People standing in queue in front of train ticket counter

Operations on queue:
ENQUEUE: is used to insert a new element to the queue at the rear end.
This can be done using append() method of list as: Q.append(element), where Q is
a list
We can insert elements in the queue till there is space in the queue for
adding more elements. Inserting elements beyond capacity of the queue will result
in an exception - known as Overflow.
DEQUEUE: is used to remove one element at a time from the front of the queue.
This can be done using pop() method of list as: Q.pop(0), where Q is a list.
We can delete elements from a queue until it is empty, trying to delete an
element from an empty queue will result in exception - known as Underflow.

To perform enqueue and dequeue efficiently on a queue, following operations are


also required:
IS EMPTY : used to check whether the queue has any element or not, so as
to avoid Underflow exception while performing dequeue operation.
PEEK : used to view elements at the front of the queue, without removing it
from the queue.
IS FULL : used to check whether any more elements can be added to the
queue or not, to avoid Overflow exceptions while performing enqueue operation

Implementation of queue
Write the program discussed in class

You might also like