Simple Queue (Non-circular) implementation using stack through arrays in c

Queue can be implemented by using two stacks, either making (enqueue operation expensive O(n) and dequeue  operation simple O(1)) or (enqueue operation simple O(1) and dequeue operation expensive O(n)).

I've done it by Enqueue operation simple O(1) 
and Dequeue operation expensive O(n).

Note: A stack can also be implemented using Queue by??

#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

void enqueue(int stack[], int *top, int size, int data)
{

    *top=*top+1;
    stack[*top]=data;
}
void dequeue(int s1[], int *t1, int size)
{
    int i,top;
    int stack2[size], top2=-1;
if(*t1==-1)
    printf("Queue is already empty!");
else
    {
       for(i=*t1;i>=0;i--)
        enqueue(stack2, &top2, size, s1[i]);
        //I've pasted the elements of first(actual Queue) stack into second stack, but did not deleted any value from it. But what i'll do is that, after pasting the
        //elements into second stack, considering that stack1 is becomeing empty, but actuall not.
        //I'll set stack pointer of first stack to -1 to make it empty and overwrite it with new values from second stack.
        printf("Dequeued element is: %d", stack2[top2]);// Dequeued element is same as s1[0];
        //now, for pop operation i've shifted top2 pointer of second stack down by one value.
        top2=top2-1;
        //Now, make stack pointer of first stack -1, to consider it empty;
        *t1=-1;top=-1;
        // *t1 and top here are having same value. And are the stack pointers of same s1 or stack1 stack.
        //But, You cannot pass here t1 as parameter, bcz it t1 is a pointer, but according to definition it should be variable whose address should be passed.
        // So, I declared top variable to pass as parameter. And wrote *t1=top, so that the actual top1 of stack1 from main function can get there value updated.
        for(i=top2;i>=0;i--){
        enqueue(s1, &top, size, stack2[i]);*t1=top;}
    }
}
void display(int stack[], int top)
{
    int i;
    if(top==-1)
        printf("Queue is empty.");
    else{
        printf("Queue is\nFront\n");
        for(i=0;i<=top;i++)
            printf("%d\n",stack[i]);
            printf("End");
    }
}
void peek(int stack[], int top)
{
    int i;
    if(top==-1)
        printf("Queue is empty.");
    else
        printf("Element at Front is: %d",stack[0]);
}
int main()
{
    int n,ch,choice=1,data;
    printf("Enter the size of Queue: ");
    scanf("%d",&n);
    int stack1[n];
    int top1=-1;
    while(choice==1){
    printf("\nEnter 1. to enqueue(insert in queue) \n");
    printf("Enter 2. to dequeue (to delete from queue)\n");
    printf("Enter 3. to display \n");
    printf("Enter 4. to peek (to show the element at front) \n choice: ");
    scanf("%d",&ch);
    if(ch==1)
    {
        if(top1!=n-1){
        printf("enter the data to be entered: ");
        scanf("%d",&data);
        enqueue(stack1,&top1,n,data);}
        else printf("Queue is full");
    }
    else if(ch==2)
        dequeue(stack1, &top1, n);
    else if(ch==3)
        display(stack1, top1);
    else if(ch==4)
        peek(stack1,top1);
    else
        printf("invalid choice:");
    printf("\nDo you want to continue(0,1): ");
    scanf("%d",&choice);
    }
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree