Dynamic Implementation of Circular Queue
CODE
👇
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int info;
- struct node *next;
- }NODE;
- NODE *front,*rear;
- void InitQ()
- {
- rear = NULL;
- }
- void AddQ(int num)
- {
- NODE *newnode;
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->info=num;
- if(rear==NULL)
- {
- rear=newnode;
- rear->next=rear;
- }
- else
- {
- newnode->next=rear->next;
- rear->next=newnode;
- rear=newnode;
- }
- }
- int DelQ()
- {
- NODE *front=rear->next;
- int num=front->info;
- if(rear->next==rear)
- free(rear);
- else
- {
- rear->next=front->next;
- free(front);
- }
- return(num);
- }
- int isempty()
- {
- return(rear == NULL);
- }
- void main()
- {
- int choice,num;
- InitQ();
- printf(">>Dynamic Implementation of Circular Queue<<\n");
- do
- {
- printf("\n1: Add \n");
- printf("2: Delete \n");
- printf("3: Exit \n");
- printf("Enter your choice:\t");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1: printf("Enter Element To Be Added:\t");
- scanf("%d",&num);
- AddQ(num);
- break;
- case 2: if(isempty())
- printf(">>Queue Is Empty<< \n");
- else
- printf("The Deleted Element Is:\t%d \n",DelQ());
- break;
- case 3: printf("\t>>END<<");
- break;
- default: printf("\t>>Enter Right Choice<<");
- }
- }
- while(choice!=3);
- }
👉Execute👈
//Output
/*
>>Dynamic Implementation of Circular Queue<<
1: Add
2: Delete
3: Exit
Enter your choice: 1
Enter Element To Be Added: 5
1: Add
2: Delete
3: Exit
Enter your choice: 2
The Deleted Element Is: 5
1: Add
2: Delete
3: Exit
Enter your choice: 4
>>Enter Right Choice<<
1: Add
2: Delete
3: Exit
Enter your choice: 3
>>END<<
*/
//ThE ProFessoR
Comments
Post a Comment