1

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

2
  • Rather than asking someone to provide you solution, describe what you’ve already tried and the results of any research. Commented Jun 9, 2016 at 3:23
  • i know how to di it in c language.. i saw many examples by searching on google... but dont know how to do this in php.... thats why i asked man Commented Jun 9, 2016 at 3:26

2 Answers 2

1

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!');
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.