#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100
int enqueue();
int dequeue();
int peek();
void display();
int main()
{
char name[max][80], data[80];
int front, rear, value;
int ch;
front = rear = -1;
printf("------------------------------\n");
printf("\tMenu");
printf("\n------------------------------");
printf("\n [1] ENQUEUE");
printf("\n [2] DEQUEUE");
printf("\n [3] PEEK");
printf("\n [4] DISPLAY");
printf("\n------------------------------\n");
while(1)
{
printf("Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1 : // insert
printf("\nEnter the Name : ");
scanf("%s",data);
value = enqueue(name, &rear, data);
if(value == -1 )
printf("\n QUEUE is Full \n");
else
printf("\n'%s' is inserted in QUEUE.\n\n",data);
break;
case 2 : // delete
value = dequeue(name, &front, &rear, data);
if( value == -1 )
printf("\n QUEUE is Empty \n");
else
printf("\n Deleted Name from QUEUE is : %s\n", data);
printf("\n");
break;
case 3:
value = peek(name, &front, &rear, data);
if(value != -1)
{
printf("\n The front is: %s", value);
}
break;
case 5 : exit(0);
default: printf("Invalid Choice \n");
}
}
return 0;
}
int enqueue(char name[max][80], int *rear, char data[80])
{
if(*rear == max -1)
return(-1);
else
{
*rear = *rear 1;
strcpy(name[*rear], data);
return(1);
}
}
int dequeue(char name[max][80], int *front, int *rear, char data[80])
{
if(*front == *rear)
return(-1);
else
{
(*front) ;
strcpy(data, name[*front]);
return(1);
}
}
int peek(char name[max][80], int *front, int *rear, char data[80])
{
if(*front == -1 || *front > *rear)
{
printf(" QUEUE IS EMPTY\n");
return -1;
}
else
{
return data[*front];
}
}
My Peek operation has a problem it does not print the first element.
For example, the user input one name, and when the user selects the peek operation, the program should print the first element, but my program always prints QUEUE IS EMPTY even though my queue is not empty.
The peek operation should print the first element of the queue.
CodePudding user response:
If you enqueue()
then peek()
the variable front
is still -1
and it incorrectly says the queue is empty.
- I suggest your treat front to rear as a half open interval with both initialized to 0, so it's empty if
*front == *rear
. peek()
need to populate data and return an integer (notdata[*front]
which is nonsense).main()
afterpeek()
you need to printdata
notvalue
.- Your prototypes are wrong. If you move the functions before
main()
then you can remove them.
int enqueue(char name[max][80], int *rear, const char data[80]) {
if(*rear 1 == max)
return -1;
strcpy(name[*rear], data);
(*rear) ;
return 1;
}
int peek(char name[max][80], int *front, int *rear, char data[80]) {
if(*front == *rear) {
printf(" QUEUE IS EMPTY\n");
return -1;
}
strcpy(data, name[*front]);
return 1;
}
// ...
int main() {
char name[max][80], data[80];
int front = 0;
int rear = 0;
int value;
int ch;
printf("------------------------------\n");
printf("\tMenu");
printf("\n------------------------------");
printf("\n [1] ENQUEUE");
printf("\n [2] DEQUEUE");
printf("\n [3] PEEK");
printf("\n [4] DISPLAY");
printf("\n------------------------------\n");
while(1)
{
printf("Choice : ");
scanf("%d", &ch);
switch(ch) {
case 1 : // insert
printf("\nEnter the Name : ");
scanf("%s",data);
value = enqueue(name, &rear, data);
if(value == -1 )
printf("\n QUEUE is Full \n");
else
printf("\n'%s' is inserted in QUEUE.\n\n",data);
break;
case 2 : // delete
value = dequeue(name, &front, &rear, data);
if( value == -1 )
printf("\n QUEUE is Empty \n");
else
printf("\n Deleted Name from QUEUE is : %s\n", data);
printf("\n");
break;
case 3:
value = peek(name, &front, &rear, data);
if(value != -1)
{
printf("\n The front is: %s\n", data);
}
break;
case 5 : exit(0);
default: printf("Invalid Choice \n");
}
}
return 0;
}
example session:
------------------------------
Menu
------------------------------
[1] ENQUEUE
[2] DEQUEUE
[3] PEEK
[4] DISPLAY
------------------------------
Choice : 1
Enter the Name : test
'test' is inserted in QUEUE.
Choice : 3
The front is: test