Stack & Queue in Data Structure
Submitted to,
Associate Professor Md. Ashek-Al-Aziz
Chairman of CSE Department
University Of Development
Alternative
Submitted by,
Name: Samsul Arafin Shanto
Student ID: 234023
Department of CSE, Batch 63th
University Of Development
Alternative
Data Structure (Stack)
Introduct
ion
A Stack is linear data structure.
A stack is a list of elements in which
an element may be inserted or
deleted only at one end, called the
top of the stack.
Stack principle is LIFO (last in, first
out). According to this, which element
inserted last on to the stack that
element deleted first from the stack.
As the items can be added or
removed only from the top i.e. the last
item to be added to a stack is the first
item to be removed.
Operations on
Stack
The two basic operations associated with stacks
are:
1. Push
2. Pop
While performing push and pop operations the
following test must be conducted on the stack.
a) Stack is empty or not b) stack is full or not
Push: Push operation is used to add new
elements in to the stack. At the time of addition
first check the stack is full or not. If the stack is
full it generates an error message "stack
overflow".
Pop: Pop operation is used to delete elements
from the stack. At the time of deletion first check
the stack is empty or not. If the stack is empty it
generates an error message "stack underflow".
Insertion and Deletion of
Stack
All insertions and deletions take place at the same
end, so the last element added to the stack will be
the first element removed from the stack. When a
stack is created, the stack base remains fixed while
the stack top changes as elements are added and
removed. The most accessible element is the top and
the least accessible element is the bottom of the
stack.
Advantages and Disadvantages of Stack
The advantages of using stack are The disadvantages of using
listed below: stack are listed below:
Efficient data management: Stack helps Limited memory size: Stack
you manage the data in a LIFO (last in, first memory is very limited.
out) method, which is not possible with a Chances of stack overflow:
Linked list and array. Creating too many objects on the
Efficient management of functions: stack can increase the risk of stack
When a function is called, the local variables overflow.
are stored in a stack, and it is automatically Random access is not possible: In
destroyed once returned. a stack, random accessing the data is
Control over memory: Stack allows you to not possible.
control how memory is allocated and Unreliable: When variable storage
deallocated. will get overwritten, it will sometimes
Smart memory management: Stack leads to undefined behaviour of the
automatically cleans up the object. function or program.
Undesired termination: The stack
Not easily corrupted: Stack does not get
corrupted easily; hence it is more secure and will fall outside of the memory area,
reliable. which might lead to an abnormal
termination.
Data Structure
(Queue)
A queue is linear data structure and
collection of elements.
A queue is another special kind of list, where
items are inserted at one end called the rear
and deleted at the other end called the front.
The principle of queue is a “FIFO” or “First-in-
first-out”.
Queue is an abstract data structure. A queue
is a useful data structure in programming. It
is similar to the ticket queue outside a
cinema hall, where the first person entering
the queue is the first person who gets the
ticket.
A real-world example of queue can be a
Operations on Queue
The operations for a queue are analogues
to those for a stack; the difference is that
the insertions go at the end of the list,
rather than the beginning.
A queue is an object or more specifically
an abstract data structure (ADT) that
allows the following operations:
Enqueue or insertion: which inserts an
element at the end of the queue.
Dequeue or deletion: which deletes an
element at the start of the queue.
Queue operations work as follows:
Two pointers called FRONT and REAR are used to keep track of the first and last elements in the
queue.
When initializing the queue, we set the value of FRONT and REAR to 0.
On enqueing an element, we increase the value of REAR index and place the new element in
the position pointed to by REAR.
On dequeueing an element, we return the value pointed to by FRONT and increase the FRONT
index.
Before enqueing, we check if queue is already full.
Before dequeuing, we check if queue is already empty.
When enqueing the first element, we set the value of FRONT to 1.
When dequeing the last element, we reset the values of FRONT and REAR to 0.
Advantages and Disadvantages of
Queue
Advantages of Queue: Disadvantages of Queue:
A large amount of data can be managed The operations such as insertion and
efficiently with ease. deletion of elements from the middle are
Operations such as insertion and time consuming.
deletion can be performed with ease as In a classical queue, a new element can
it follows the first in first out rule. only be inserted when the existing
Queues are useful when a particular elements are deleted from the queue.
service is used by multiple consumers. Searching an element takes O(N) time.
Maximum size of a queue must be
Queues are fast in speed for data inter-
process communication. defined prior in case of array
implementation.
Queues can be used in the
implementation of other data structures.
Difference between Stack
Stacks and Queue Queues
1. A stack is a linear list of elements in which the element 1. A Queue is a linerar list of elements in which the
may be inserted or deleted at one end. elements are added at one end and deletes the elements
at another end.
2. In stacks, elements which are inserted last is the first
2. In Queue the element which is inserted first is the
element to be deleted. element deleted first.
3. Stacks are called LIFO (Last In First Out) list 3. Queues are called FIFO (First In First Out) list.
4. In stack elements are removed in reverse order in 4. In Queue elements are removed in the same order in
which thy are inserted. which thy are inserted.
5. Suppose the elements a,b,c,d,e are inserted in the 5. Suppose the elements a,b,c,d,e are inserted in the Queue,
stack, the deletion of elements will be e,d,c,b,a. the deletion of elements will be in the same order in which
thy are inserted.
6. In stack there is only one pointer to insert and delete 6. In Queue there are two pointers one for insertion called
called “Top”. “Rear” and another for deletion called “Front”.
7. Initially top=-1 indicates a stack is empty. 7. Initially Rear=Front=-1 indicates a Queue is empty.
8. Stack is full represented by the condition TOP=MAX 8.Queue is full represented by the condition
(if array index starts from ‘0’). Rear=Max-1.
9. To push an element into a stack, Top is incremented by 9. To insert an element into Queue, Rear is
one incremented by one.
10. To POP an element from stack, top is decremented by 10. To delete an element from Queue, Front is incremented
one by one
Thank
you