Stack vs Queue: Data Structure
Difference Between Stack and Queue
What is Stack?
The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first. The insertion and deletion operations take place from the one side of the stack known as top. In stack, we can insert the elements which have same data types or it can contain homogeneous elements only.
There are some operations perform which are given below:
- push(data) adds a new data item into the stack. We give data item as parameter and it returns nothing.
- pop() removes the data item from the stack, returns the data item and it will be modified.
- top() function returns top element present in the stack.
- is_Empty() function checks whether the stack is empty or not and it returns a boolean value.
- is_Full() function checks whether the stack is full or not and it returns a boolean value.
- size() returns the number of items in the stack and return an integer.
Queue: -7 8 /cwb,7e
A queue is linear data structure which works on first in first out principle. We can say a queue is homogeneous collection of data items where new data items are added at one end of queue called rear and the deletion of the data item is done from other end called front of the queue.
There are some operations perform which are given below:
- enqueue(data) inserts the data item into the queue.
- dequeue() deletes the data item from the queue called dequeue operation.
- is_Empty() function checks whether the queue is empty or not and it returns a boolean value.
- is_Full() function checks whether the queue is full or not and it returns a boolean value.
- size() returns the number of items in the queue and return an integer.
Difference Between Stack and Queue
Parameters | Stack | Queue |
Definition | A stack is a kind of linear data structure. We can say it is container that has only one end is opened and it follows a principle which is called Last in First out (LIFO). | A queue is linear data structure which works on first in first out (FIFO) concept. We can say a queue is homogeneous collection of data items |
Insertion and Deletion | In stack, there is only one end opened so insertion and deletion operations are done from one end called top of the stack | In queue, we can insert the data item from the end of the queue and deletion can be done from the front of the queue. |
Full condition | For checking whether the stack is full or not. If (top== max-1) | For checking whether the queue is full or not. If (rear==max-1) |
Empty condition | For checking whether the stack is empty or not. If (top==-1) | For checking whether the queue is empty or not. If (front== -1 or front = rear+1) |
Types | Stack doesn’t have any kind of types | Queue has three other types like circular queue, priority queue and double ended queue. |
Visualization | A stack is visualized as a vertical collection. | A queue is visualized as a horizontal collection. |