Skip to content

Instantly share code, notes, and snippets.

@gallirohik
Created March 19, 2018 19:11
Show Gist options
  • Select an option

  • Save gallirohik/eba0e931e4e23f47c64048fb2f9781a1 to your computer and use it in GitHub Desktop.

Select an option

Save gallirohik/eba0e931e4e23f47c64048fb2f9781a1 to your computer and use it in GitHub Desktop.
implemented queue using stack
#include <stdio.h>
#include <stdlib.h>
struct qnode
{
int data;
struct qnode *next;
};
typedef struct qnode* qptr;
qptr front=NULL,rear=NULL;
int isempty(front)
{
if(front==NULL)
return 0;
return 1;
}
qptr push(qptr rear,int data)
{
qptr p;
p=(qptr)malloc(sizeof(struct qnode));
p->data=data;
p->next=NULL;
if(rear==NULL)
{
rear=p;
front=p;
return p;
}
rear->next=p;
rear=p;
return rear;
}
qptr pop()
{
qptr t;
if(isempty(front))
{
t=front;
front=front->next;
return t;
}
printf("queue is empty...! ");
exit(1);
}
int main()
{
qptr t=NULL;
int c,data;
while(1)
{
scanf("%d",&c);
if(c==1)
{
scanf("%d",&data);
rear=push(rear,data);
}
else if(c==0)
{
t=pop();
printf("%d\n",t->data);
}
else{
exit(1);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment