#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct stack
{
int data;
struct stack *next;
};
struct stack* push(struct stack *head,int num)
{
struct stack* temp=(struct stack*)malloc(sizeof(struct stack));
temp->data=num;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
return head;
}
int pop(struct stack **head)
{
int temp=(*head)->data;
*head=(*head)->next;
return temp;
}
int main()
{
int choice,temp,q,num,i,a;
struct stack* stack_ins=NULL;
struct stack* stack_del=NULL;
scanf("%d",& q);
for(i=0;i<q;i++)
{
scanf("%d",&choice);
if(choice==1)
{
scanf("%d",&num);
stack_ins=push(stack_ins,num);
}
if(choice==2)
{
if(stack_del==NULL)
{
while(stack_ins!=NULL)
{
temp=pop(&stack_ins);
stack_del=push(stack_del,temp);
}
}
a=pop(&stack_del);
}
if(choice==3)
{
if(stack_del==NULL)
{
while(stack_ins!=NULL)
{
temp=pop(&stack_ins);
stack_del=push(stack_del,temp);
}
}
printf("%d\n",stack_del->data);
}
}
return 0;
}
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct stack
{
int data;
struct stack *next;
};
struct stack* push(struct stack *head,int num)
{
struct stack* temp=(struct stack*)malloc(sizeof(struct stack));
temp->data=num;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
return head;
}
int pop(struct stack **head)
{
int temp=(*head)->data;
*head=(*head)->next;
return temp;
}
int main()
{
int choice,temp,q,num,i,a;
struct stack* stack_ins=NULL;
struct stack* stack_del=NULL;
scanf("%d",& q);
for(i=0;i<q;i++)
{
scanf("%d",&choice);
if(choice==1)
{
scanf("%d",&num);
stack_ins=push(stack_ins,num);
}
if(choice==2)
{
if(stack_del==NULL)
{
while(stack_ins!=NULL)
{
temp=pop(&stack_ins);
stack_del=push(stack_del,temp);
}
}
a=pop(&stack_del);
}
if(choice==3)
{
if(stack_del==NULL)
{
while(stack_ins!=NULL)
{
temp=pop(&stack_ins);
stack_del=push(stack_del,temp);
}
}
printf("%d\n",stack_del->data);
}
}
return 0;
}
No comments:
Post a Comment