komal deep singh chahal
komal deep singh chahal

Reputation: 1259

Implement queue using 2 stacks

Can anyone tell me. how to implement queue using 2 stacks. Specifically, implement the enqueue and dequeuer methods.

it will helpful if you guys tell me in php or JavaScript programming

Upvotes: 1

Views: 399

Answers (2)

Atul Sharma
Atul Sharma

Reputation: 1

Here is my solution, but it's in C lang.

#include<stdio.h>
#include<stdlib.h>
struct Stack{
    int size;
    int top;
    int *S;
}S1,S2;

void create(struct Stack *st){
    st->size=10;
    st->top=-1;
    st->S=(int *)malloc(st->size*sizeof(int));

}

void push(struct Stack *st, int x){
    if(st->top==st->size-1)
        printf("\nStack overflow\n");
    else
    {
        st->top++;

        st->S[st->top]=x;
    }    
}

int pop(struct Stack *st){
    int x=-1;
    if(st->top==-1)
        printf("\nStack Underflow\n");
    else
    {
        x=st->S[st->top--];  // post decrement of top
    }
    return x;
}

int isEmpty(struct Stack *st){
    if(st->top==-1)
        return 1;
    return 0;
}

void enqueue(int x){
    push(&S1,x);
}

int dequeue(){
    int x=-1;
    if(isEmpty(&S2)){
        if(isEmpty(&S1)){
            printf("Empty Queue!");
            return x;
        }
        else
        {
            while(!isEmpty(&S1))
                push(&S2,pop(&S1));
        }       
    }
    return pop(&S2);
}
void display(struct Stack *st){
    
    for(int i=st->top;i>=0;i--)
        printf("%d ",st->S[i]);
    printf("\n");
    
}

int main(){

    create(&S1);
    create(&S2);

    enqueue(10);
    enqueue(20);
    enqueue(30);
    enqueue(40);

    int deleted_element=dequeue();
    printf("Dequeued Element is = %d\n",deleted_element);
    display(&S2);



    return 0;
}

Upvotes: 0

Xelad1
Xelad1

Reputation: 175

Here is a personal example, I'm sure it could be more optimized, but it allows for queue dequeue and peek functionality in JS.

function processData(input) {
    let stackOne = [];
    let stackTwo = [];
    let parsableInput = input.split('\n');

    for(let i = 1; i < parsableInput.length; i++) {
        // handle 1 push
        if (parsableInput[i][0] === '1') {
            enqueue(stackOne, stackTwo, parsableInput[i].slice(2));
        }
        // handle 2
        if (parsableInput[i] === '2') {
            dequeue(stackTwo);
        }
        // handle 3
        if (parsableInput[i] === '3') {
            console.log(peek(stackTwo));
        }
    }
} 

function enqueue(stackOne, stackTwo, queuedValue) {
    while(stackTwo.length !== 0) {
        stackOne.push(stackTwo.pop());
    }

    stackOne.push(queuedValue);

    while(stackOne.length !== 0) {
        stackTwo.push(stackOne.pop());
       }
}

function dequeue(stackTwo) {
    return stackTwo.pop();
}

function peek(stackTwo) {

    let stringToBeParsed = stackTwo[stackTwo.length - 1];
    let parsedString = stringToBeParsed.slice(0, stringToBeParsed.length);

    if (parsedString) {
        return parsedString;
    } else {
        console.log('Error: there is nothing to peek at!');
    }
}

Upvotes: 1

Related Questions