Question description Rajinikanth organized a technical round interview in an Animation company for the set of computer science candidates. the task is to implement stack operations for two stacks and merge the stacks into one. Get two sets of stack elements and reverse them, then merge them into one stack. Rajinikanth has given the deadline of only 15 minutes to complete the problem. Can you Help the candidates to complete the problem within the specified time limit? Function Description a) push(): Adds the new item at the beginning of the linked list using the first pointer. b) pop(): Removes an item from the beginning using the first pointer. c) merge(): Links the first pointer second stack as next of the last pointer of the first list.
C Program


#include<stdio.h>

#include<stdlib.h>

typedef struct node{

    int data;

    struct node *next;

}node;

typedef struct mystack{

    node *top;

}mystack;

void push(int data, mystack* ms)

{

    node *temp=(node*)malloc(sizeof(node));

    temp->data=data;

    temp->next=ms->top;

    ms->top=temp;

}

int pop(mystack* ms)

{

    if(ms->top==NULL)

        return -1;

    node *t=ms->top;

    int x=t->data;

    ms->top=t->next;

    free(t);

    return x;

}

void merge(mystack* ms1, mystack* ms2)

{

    node *p=ms1->top;

    if(!p)

    {

        ms1->top=ms2->top;

        return;

    }

    while(p->next)

        p=p->next;

    p->next=ms2->top;

}

void display(mystack* ms)

{

    node *p=ms->top;

    while(p)

    {

        printf("%d ",p->data);

        p=p->next;

    }

}

int main()

{

    int n,m,i,x;

    mystack s1,s2;

    s1.top=NULL;

    s2.top=NULL;

    scanf("%d %d",&n,&m);

    for(i=0;i<n;i++)

    {

        scanf("%d",&x);

        push(x,&s1);

    }

    for(i=0;i<m;i++)

    {

        scanf("%d",&x);

        push(x,&s2);

    }

    merge(&s1,&s2);

    display(&s1);

    return 0;

}