Stack and Queue in Data Structures
Stack
In Data Structure,
a stack is defined as a linear structure in which certain order of operations are
performed.
The aforementioned
operators will most likely include:
- Push: The addition of an item to a stack. If the stack is full, an Overflow condition is ruled should you use the push operator on it.
- Pop: The removal of an item from a stack. If the stack is empty, an Underflow condition is ruled should you use the pop operator on it.
- Peek/Top: Returning the top element of a stack.
- isEmpty: Checking whether or not a stack is empty.
In a stack, this order can either be LIFO (Last In
First Out) or FILO (First In Last Out) because they are basically the same
things. LIFO or FILO basically means that, in a list of items, the last one to
be added will be taken out first. An example of this would be a stack of
plates you have just washed and placed in a dryer. To put it back to the
cupboard, you would use the LIFO method in which the latest washed plate gets
taken first out of the stack.
Queue
Similar to a Stack, a Queue is also
defined as a linear structure with a certain order of operations performed on
them. The difference between a Stack and a Queue are the operations
done to each of them and the order in which they are performed.
Operations in a
queue usually include:
·
Enqueue: The addition of an item to a queue. Similar to a
stack, an Overflow condition will be ruled if you use enqueue while said
queue is full.
·
Dequeue: The removal of an item from a queue. Also similar to
a stack, an Underflow condition will be ruled if dequeue is used with an
empty queue.
·
Front: Returning the front (first) item from a queue.
·
Rear: Returning the last item from a queue.
The Above operators, although also done in a certain
order, the order in which a queue is done is FIFO (First in First Out). Example
of such a phenomena is, like its name, a real life queue in which the first in
line will be served first followed by the second, third, and so on.
Comments
Post a Comment