Stack and Queue

From my last post, I’ve talked about single linked list and double linked list, now we’re going to talk about STACK AND QUEUE. Both stack and queue is a linear data structure. Queues and stacks are also complementary in the actual use scenarios.

So what is the difference between stack and queue? 
In STACK the elements can be inserted and deleted only from one side of the list called the TOP and a stack follows the LIFO (Last In, First Out) principle, so the element inserted at the last is the first element to come out. In stack if you want to add a new node you can do push like in a single linked list and do pop if you want to remove a node. But remember that the added new node will be put on top or say at the end of the list and if you do pop or remove a node the one that will be removed will be the top or the recently added node. One of the applications for stack is an undo mechanism in a text editor.


Here is a visualization of stack.


For QUEUE the elements can be inserted only from one side of the list called REAR(the last inserted element), and the elements can be deleted only from the other side called the FRONT(the first and still element of the list) and a queue follows the FIFO (First In, First Out) principles, so the elements inserted at first in the list will be the first to be removed from the list. In queue adding a new node--to the end of the list is called enqueue and removing the front node is called dequeue. Queue will be very useful when doing parsing or converting arithmetic expressions from infix to postfix.


Here is a visualization of queue.


That’s it for my brief summary for stack and queue, I hope we all learn something from this. 

Have a nice day!! Bitter or sweet J

Comments

Popular posts from this blog

Even Semester Full Summary

Linked List and Stack and Queue

First Part of Even Semester