c - My Circular Queue implementation is not working properly -
i have created program implement circular queue insert, delete , display. insertion working fine , deletion once try enter numbers after deletion, nothing displayed. here source code:
#include<stdio.h> #include<conio.h> #define size 5 int front = -1; int rear = -1; int queue[size]; void enqueue(int item); int dequeue(); void display(); void main() { int item, choice, cont = 1; clrscr(); while(cont == 1) { printf("\n1.enqueue queue.\n"); printf("\n2.dequeue queue.\n"); printf("\n3.display quesue elements\n"); printf("\nenter choice: "); scanf("%d",&choice); switch(choice) { case 1: printf("\nenter value of item: "); scanf("%d",&item); enqueue(item); break; case 2: item = dequeue(); if(item != null) { printf("\nitem dequeued: %d\n",item); } break; case 3: display(); break; default: printf("\ninvalid choice.\n"); break; } printf("\ndo want continue (1/0): "); scanf("%d",&cont); } getch(); } void enqueue(int item) { if(front==0 && rear==size-1) printf("\n queue overflow occured"); else if(front==-1 && rear==-1) { front=rear=0; queue[rear]=item; } else if(rear==size-1 && front!=0) { rear=0; queue[rear]=item; } else { rear++; queue[rear]=item; } } int dequeue() { int item = null; if(front == -1 && rear == -1) { printf("\nqueue empty. dequeue not possible.\n"); } else { item = queue[front]; queue[front] = null; if(front == rear) { front = -1; rear = -1; } else { front = front + 1; } } return(item); } void display() { int i; if(front==-1) printf("\n no elements display"); else { printf("\n queue elements are:\n "); for(i=front;i<=rear;i++) { printf("\t %d",queue[i]); } } }
check code working fine.
few suggestions don't use conio.h [clrscr(), , getch()] these not standards.
try code below worked me fine.
code
#include<stdio.h> #define size 5 int front = -1; int rear = -1; int queue[size]; void enqueue(int item); int dequeue(); void display(); int main() { int item, choice, cont = 1; while(cont == 1) { printf("\n1.enqueue queue.\n"); printf("\n2.dequeue queue.\n"); printf("\n3.display quesue elements\n"); printf("\nenter choice: "); scanf("%d",&choice); switch(choice) { case 1: printf("\nenter value of item: "); scanf("%d",&item); enqueue(item); break; case 2: item = dequeue(); if(item != null) { printf("\nitem dequeued: %d\n",item); } break; case 3: display(); break; default: printf("\ninvalid choice.\n"); break; } printf("\ndo want continue (1/0): "); scanf("%d",&cont); } printf(""); return 0; } void enqueue(int item) { if(front==0 && rear==size-1) printf("\n queue overflow occured"); else if(front==-1 && rear==-1) { front=rear=0; queue[rear]=item; } else if(rear==size-1 && front!=0) { rear=0; queue[rear]=item; } else { rear++; queue[rear]=item; } } int dequeue() { int item = null; if(front == -1 && rear == -1) { printf("\nqueue empty. dequeue not possible.\n"); } else { item = queue[front]; queue[front] = null; if(front == rear) { front = -1; rear = -1; } else { front = front + 1; } } return(item); } void display() { int i; if(front==-1) printf("\n no elements display"); else { printf("\n queue elements are:\n "); for(i=front;i<=rear;i++) { printf("\t %d",queue[i]); } } }
output
want continue (1/0): 1 1.enqueue queue. 2.dequeue queue. 3.display quesue elements enter choice: 3 queue elements are: 5 5 want continue (1/0): 1 1.enqueue queue. 2.dequeue queue. 3.display quesue elements enter choice: 1 enter value of item: 43 want continue (1/0): 1 1.enqueue queue. 2.dequeue queue. 3.display quesue elements enter choice: 3 queue elements are: 5 5 43 want continue (1/0): 1 1.enqueue queue. 2.dequeue queue. 3.display quesue elements enter choice: 2 item dequeued: 5 want continue (1/0): 1 1.enqueue queue. 2.dequeue queue. 3.display quesue elements enter choice: 3 queue elements are: 5 43 want continue (1/0):
Comments
Post a Comment