diff --git a/Queue_using_array.c b/Queue_using_array.c index 7d9646b..764535f 100644 --- a/Queue_using_array.c +++ b/Queue_using_array.c @@ -4,7 +4,7 @@ //structure of Queue struct queue { - int front,rear,size; + int front=-1,rear=-1,size=0; unsigned capacity; int *array; }; @@ -24,7 +24,7 @@ int isEmpty(struct queue *Queue) return(Queue->size==0); } -int isFULL(struct queue *Queue) +int isFull(struct queue *Queue) { return(Queue->size==Queue->capacity); } @@ -36,7 +36,7 @@ void enqueue(struct queue *Queue,int n) Queue->rear=(Queue->rear+1)%Queue->capacity; Queue->array[Queue->rear]=n; Queue->size=Queue->size+1; - printf("Enqueued Element%d",n); + printf("\nEnqueued Element%d",n); } int dequeue(struct queue *Queue) @@ -71,7 +71,7 @@ int main() enqueue(Queue,16); dequeue(Queue); - printf("Element At front %d\n\n",front(Queue)); - printf("Element At Rear%d",rear(Queue)); + printf("\nElement At front %d",front(Queue)); + printf("\nElement At Rear %d",rear(Queue)); return 0; } diff --git a/circular_queue_using_array.cpp b/circular_queue_using_array.cpp index 29ae8b4..79cd837 100644 --- a/circular_queue_using_array.cpp +++ b/circular_queue_using_array.cpp @@ -2,7 +2,7 @@ #define MAXQ 100 typedef struct { - int front ,rear; + int front=-1 ,rear=-1; int items[MAXQ]; }queue; @@ -35,8 +35,9 @@ int insert(queue *q,int x) q->rear=0; } else - q->rear =(q->rear+1)%MAXQ; + q->rear =(q->rear+1)%MAXQ; q->items[q->rear] = x; + return 0; } int Delete(queue *q) @@ -66,29 +67,30 @@ void display(queue *q) printf("\nqueue empty"); return; } - else - printf("\nElements In the Queue Are:\n"); - if(q->rear>=q->front) - { - for(i=q->front;i<=q->rear;i++) - printf("%d\n",q->items[i]); + else{ + printf("\nElements In the Queue Are:\n"); + if(q->rear>=q->front) + { + for(i=q->front;i<=q->rear;i++) + printf("%d\n",q->items[i]); + } + else{ + for(i=q->front;i<=MAXQ;i++) + printf("%d\n",q->items[i]); + for(i=0;i<=q->rear;i++) + printf("%d\n",q->items[i]); + } } - else - for(i=q->front;i<=MAXQ;i++) - printf("%d\n",q->items[i]); - for(i=0;i<=q->rear;i++) - printf("%d\n",q->items[i]); - } int main() { queue q; int x; - char ch='1'; + int ch; q.front=-1; q.rear=-1; - while(ch!='4') + do { printf("\n 1 - Insert"); printf("\n 2 - Delete"); @@ -96,30 +98,30 @@ int main() printf("\n 4 - Quit"); printf("\nEnter your Choice :"); fflush(stdin); - scanf("%c",&ch); + scanf("%d",&ch); switch(ch) { - case '1': + case 1: printf("\nEnter The Element To Be inserted :"); scanf("%d",&x); insert(&q,x); break; - case '2': - x=Delete(&q); - printf("\nDeleted Element Is %d\n:",x); - break; - case '3': - display(&q); - break ; - case '4': - break; - default: - printf("\nYou Entered Wrong Choice.Try again!"); + case 2: + x=Delete(&q); + printf("\nDeleted Element Is %d\n:",x); + break; + case 3: + display(&q); + break ; + case 4: + break; + default: + printf("\nYou Entered Wrong Choice.Try again!"); + break; } - } + }while(ch!=4); return 0; } -