
Stack as a data structure is not related to the stack memory area we learned about last weeks, and they are completely different things.

Implementing Queue using concrete array.Biomedical Application: Queues of Biological Signals.Finally, let’s add a variable indicating the top element.Implementing a Stack using Static Array.Before starting: Stack as Data Structure != Stack Memory.In stack1, the oldest element would be at the top of the stack, so time taken to perform a dequeue operation would be O(1). Here, the time complexity of enqueue operation would be O(n). After adding the new element in the stack1, all the element are moved back from stack1 to stack2. Once all the elements from the stack1 are pushed into the stack2, then the new element is added in the stack1. In case of enqueue operation, first all the elements will be popped from the stack1 and push it into the stack2. If we implement the Queue using Stack by making a enqueue operation costly means that time complexity in enqueue operation would be O(n) and the time complexity in dequeue operation would be O(1).įirst, we will consider two stacks named as stack1 and stack2. Second Approach: Making an enqueue operation costly. Printf("\nThe dequeued element is %d", b) Int element = stack2 // assigning the topmost value to element Stack2=x // assigning the 'x' value to the Stack2 Top2++ // incrementing the value of top2.
ENQUEUE DEQUEUE C LINKED LIST STACK FULL
Condition to check whether the stack2 is full or not Int a=stack1 // Assigning the topmost value of stack1 to 'a' variable. Condition to check whether the stack1 is empty or not. Removing the elements from the stack1. Stack1=data // pushing the data into stack1 Condition to check whether the stack1 is full or not. Int stack1, stack2 // declaration of two stacks Program to implement Queue using Stack in C. Here, dequeue operation is performed two times on the data so time complexity is O(n). The topmost element would be popped out from the Stack2 and then all the elements from the Stack2 are moved back to Stack1. Once all the elements are popped from the Stack1 then they are added in the Stack2. First, we insert the elements in the Stack1 and then we remove all the elements from the Stack1. In case of dequeue operation, we need to consider two stacks named as Stack1 and Stack2. In this case, when we insert the elements in the stack then the elements are added on the top of the stack so it takes O(1) time. If we implement the Queue using Stack by making a dequeue operation costly means that time complexity in enqueue operation would be O(1) and the time complexity in dequeue operation would be O(n). There are two approaches to implement Queue using Stack:įirst approach: Making a dequeue operation costly Once the topmost element is popped out from the Stack2, all the elements are moved back from Stack2 to Stack 1 shown as below: Once the elements are inserted into the Stack2, the topmost element is 5 so it would be popped out from the Stack 2 shown as below:


Now we will pop the elements from the Stack1 one by one and push them into the Stack2 as shown as below: First, we will push 5, then 2 and finally we will push element 3 shown as below:

Now, we will perform push operations on the Stack1. Suppose we have two stacks named as Stack1 and Stack2 shown as below:Īs we can observe that above stacks are empty. In order to implement the Queue using Stack, we need to consider two stacks. On the other hand, the deletion in Queue is performed from the front end and the front element is 5. If we perform the delete operation in the above stack, then the element 3 would be deleted from the stack. In the above stack, we can observe that the topmost element is 3. Now we will perform three enqueue operations shown as below:Īfter performing the above three enqueue operations, the queue would look like: Let's understand the implementation of Queue using stacks. Stack is a linear data structure that follows LIFO (Last In First Out) principle in which both insertion and deletion are performed from the top of the stack. Queue is a linear data structure that follows FIFO ( First In First Out) principle in which insertion is performed from the rear end and the deletion is done from the front end. Next → ← prev Implementation of Queue using Stacks
