A stack is simply a list of elements with insertions and deletions permitted at one endcalled the stack top. That means that it is possible to remove elements from a stack in reverse order from the insertion of elements into the stack. Thus, a stack data structure exhibits the LIFO (last in first out) property. Push and pop are the operations that are provided for insertion of an element into the stack and the removal of an element from the stack, respectively.
Operations on stack:
The insertion of elements into stack is called PUSH operation.
The deletion of elements from stack is called POP operation.
Following actions taken place in POP:
Check the stack empty or not.
Remove the top element from the stack.
Return this element to the calling function or program.
A queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle. An excellent example of a queue is a line of students in the food court of the UC. New additions to a line made to the back of the queue, while removal (or serving) happens in the front. In the queue only two operations are allowed enqueue and dequeue. Enqueue means to insert an item into the back of the queue, dequeue means removing the front item. The picture demonstrates the FIFO access.
The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the itemthe least recently added.
Queue operations are also called First-in first-out Operations
Enqueue: insert at the end of queue
Dequeue: delete from the beginning of queue
Code: similar to previous code on linked lists
Queue Application: Executing processes by operating system
Operating System puts new processes at the end of a queue.System executes processes at the beginning of the queue
The last element of a linked list points to the first element.
A reference pointer is required to access the list: head
The list pointer can have the address of the last element.The tail/last element can be accessed by the list pointer.The head/first element can be accessed from the tail/lastelement (by list->next)
Provides flexibility in accessing first and last elements
Circular lists can be used for queues.
Useful in enqueue/dequeue operations without needing to
traverse the list.