What is stack in data structure? What is importance of using stack over queue? I created stack in C but cannot understand it's principle and benefits.
-
2these are two ways to arrange data. – Aug 30 '21 at 13:59
-
@Mr_Robot Well, to be fair, they are two ways to choose the order in which to store and retrieve data; the way data is arranged in memory might or might not be the same :) – Stef Aug 30 '21 at 14:01
-
Queue: "process items in the order they appear"; Stack: "process most recent items first". – Stef Aug 30 '21 at 14:02
-
3stack work on principle LIFO(Last IN First Out) and Queue on FIFO (First In First Out). – Aug 30 '21 at 15:09
1 Answers
Stack : A stack is a linear data structure in which elements can be inserted and deleted only from one side of the list, called the top. A stack follows the LIFO (Last In First Out) principle, i.e., the element inserted at the last is the first element to come out. The insertion of an element into stack is called push operation, and deletion of an element from the stack is called pop operation. In stack we always keep track of the last element present in the list with a pointer called top.
Queue: A queue is a linear data structure in which elements can be inserted only from one side of the list called rear, and the elements can be deleted only from the other side called the front. The queue data structure follows the FIFO (First In First Out) principle, i.e. the element inserted at first in the list, is the first element to be removed from the list. The insertion of an element in a queue is called an enqueue operation and the deletion of an element is called a dequeue operation. In queue we always maintain two pointers, one pointing to the element which was inserted at the first and still present in the list with the front pointer and the second pointer pointing to the element inserted at the last with the rear pointer.