Created
March 19, 2018 19:11
-
-
Save gallirohik/eba0e931e4e23f47c64048fb2f9781a1 to your computer and use it in GitHub Desktop.
implemented queue using stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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