Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed circular queue using array.cpp #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Queue_using_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//structure of Queue
struct queue
{
int front,rear,size;
int front=-1,rear=-1,size=0;
unsigned capacity;
int *array;
};
Expand All @@ -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);
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
64 changes: 33 additions & 31 deletions circular_queue_using_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define MAXQ 100
typedef struct
{
int front ,rear;
int front=-1 ,rear=-1;
int items[MAXQ];

}queue;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -66,60 +67,61 @@ 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");
printf("\n 3 - Display");
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;
}