Program in C to implement queue Using Structure.
#include<stdio.h> #include<conio.h> #include<stdlib.h> struct Queue{ int data; struct Queue *next; }; typedef struct Queue Queue; Queue *front=NULL,*rear=NULL; void insert(int d); void del(); void show(); int length(); void insert(int d) { Queue *newN; newN=(Queue*)malloc(sizeof(Queue)); newN->data=d; newN->next=NULL; if(front==NULL) { front=newN; rear=newN; } else { rear->next=newN; rear=newN; } } void del() { Queue *temp; int d; if(front==NULL){ printf("Queue is empty!"); } else { temp=front; d=temp->data; printf("\nDelete data = %d",d); front=temp->next; free(temp); } } void show() { Queue *temp; temp=front; while(temp!=NULL) { printf("%4d",temp->data); temp=temp->next; } } int length() { Queue *temp; int count=0; temp=front; while(temp!=NULL) { count++; temp=temp->next; } return count; } int main() { insert(23); insert(89); insert(56); insert(23); insert(12); insert(17); printf("\nElements of queue "); show(); printf("\nSize of queue = %d",length()); del(); return 0; }
Output :-
Related Programs
- Program in C and C++ to implement queue Using Array second method.
- Program in C++ to implement queue Using Class.
- Program in C and C++ to implement queue Using Array and Function.
- Program in C++ to implement queue Using Template feature.
0 Comments