A Queue can be defined as a list of items.Queue follows the method of first in first out .
- First element inserted is first one retrieved
- Can’t retrieve elements from middle of queue or end of queue
- Can’t insert elements into middle or start of queue
Examples: Implement Enqueue And Dequeue Process By Using LinkedList
#include <stdio.h> #include <stdlib.h> #include <conio.h> #define size 3 struct node { int data ; struct node *pnext; }; struct node *phead; struct node *ptail; int count; struct node *createnode(int data); int equeue(int data); int dequeue (); struct node *createnode (int data){ struct node *ptr=NULL; if(count<size){ ptr=(struct node*) malloc(sizeof(struct node)); if(ptr){ count++; ptr->data=data; ptr->pnext=NULL; } } return ptr; } int enqueue(int data){ int flag=0; struct node *ptr=createnode(data); if(ptr){ flag=1; if (ptail==NULL){ ptail=phead=ptr; } else ptail->pnext=ptr; ptail=ptr; } return flag; } int dequeue (){ struct node *pcur=phead; int data; if(pcur){ phead=phead->pnext; data=pcur->data; free(pcur); count--; } return data; } void printall (){ struct node *pointer; pointer=phead; int i=0; for (i=0;i!=size;i++){ printf("%d\n",pointer->data); pointer=pointer->pnext; } } int main() { int input,output ; int stat; do{ printf("You Want To\n 1-Enqueue\n 2-Dequeue\n 3-Print All\n"); scanf("%d",&stat); switch(stat){ case 1 : printf("Please Enter Input Integer Number:\n"); scanf("%d",&input); enqueue(input); break; case 2 : output=dequeue(); printf("%d\n",output); break; case 3 : printall(); break; } printf("Do you want do something else ?(y/n)\n"); }while(getch()=='y'); return 0; }
0 Comment to "Queue"
Post a Comment