#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100
int enqueue();
int dequeue();
int peek();
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;
}
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 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 == *rear) {
printf(" QUEUE IS EMPTY\n");
return -1;
}
strcpy(data, name[*front]);
return 1;
}
Student here.
My dequeue is not working correctly. The dequeue function is not deleting the first element but the second element. For example, The user, first inputs the name "Jennie" and then the second is "Lisa", when the user selects the dequeue function, "Jennie" should be deleted, but my program deletes the second element which is "Lisa". How to fix this?
You need to increment the front after you dequeue the value:
int dequeue(char name[max][80], int *front, int *rear, char data[80])
{
if(*front == *rear)
return -1;
strcpy(data, name[(*front)++]);
return 1;
}
Related
Can you tell me if this is correct way to do?
This is my sample code in student information in queue. my problem is that I want to insert a student number in the struct but it doesn't repeat when I input the same student number.
#include <stdio.h>
#include <stdlib.h>
struct node
{
char No[12],Name[24],crsysr[10];
float gwa;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void insert(char *data, char *name,char *yearsec,float gwa);
void del();
void empty();
void display();
void create();
void queuesize();
void removeduplicate();
int count = 0;
void main()
{
int ch, pos,i,e;
char no[12], name[24],crsyr[10];
float gwa,tgwa;
create();
while (1)
{
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf ("how Many student do you want to enter?: ");
int n;
scanf("%d",&n);
for(int i=0; i<n; ++i)
{
fflush(stdin);
printf("\n Enter Student number : ");
gets(no);
printf("\n Enter Student name : ");
gets(name);
printf("\n Enter Student Year and Sec : ");
gets(crsyr);
fflush(stdin);
printf("\n Enter Student gwa : ");
scanf("%f", &gwa);
insert(no, name, crsyr, gwa);
count++;
}
printf("Press any key to contiue...");
getch();
system("cls");
break;
case 2:
del();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
system("cls");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
system("cls");
removeduplicate(temp->ptr);
display();
printf("Press any key to contiue...");
getch();
system("cls");
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("\n Queue size : %d", count);
}
void insert(char *data, char *name,char *yearsec, float gwa)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
strcpy(rear->No, data);
strcpy(rear->Name, name);
strcpy(rear->crsysr, yearsec);
rear->gwa=gwa;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
strcpy(temp->No, data);
strcpy(temp->Name, name);
strcpy(temp->crsysr, yearsec);
temp->gwa=gwa;
temp->ptr = NULL;
rear = temp;
}
}
void display()
{
front1 = front;
int i;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
printf("Student Number\t\tName\t\tSection\t\tGwa\n\n");
while (front1 != rear)
{
printf("%s \t",front1->No);
printf("%s \t",front1->Name);
printf("%s \t",front1->crsysr);
printf("%f \t", front1->gwa);
front1 = front1->ptr;
printf("\n");
}
if (front1 == rear)
printf("%s \t",front1->No);
printf("%s \t",front1->Name);
printf("%s \t",front1->crsysr);
printf("%f\t", front1->gwa);
printf("\n");
}
void del()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed Student num : " );
puts(front->No);
printf("\n Dequed Name : ");
puts(front->Name);
printf("\n Dequed Year and section : ");
puts(front->crsysr);
printf("\n Dequed GWA : %f", front->gwa);
free(front);
front = front1;
}
else
{
printf("\n Dequed Student Num : ", front->No);
printf("\n Dequed Name : ", front->Name);
printf("\n Dequed Year and section :", front->crsysr);
printf("\n Dequed GWA : %f", front->gwa);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->No,front->Name,front->crsysr,front->gwa);
else
return 0;
}
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
void removeduplicate(struct node *front) {
struct node *ptr1, *ptr2, *duplicate;
ptr1 = front;
while (ptr1 != NULL && ptr1->ptr != NULL)
{
ptr2 = ptr1;
/* Compare the current element with rest of the elements */
while (ptr2->ptr != NULL)
{
if (strcmp(ptr1->No == ptr2->ptr->No && ptr1->Name == ptr2->ptr->Name && ptr1->crsysr == ptr2->ptr->crsysr && ptr1->gwa == ptr2->ptr->gwa)==0)
{
duplicate = ptr2->ptr;
ptr2->ptr = ptr2->ptr->ptr;
free(duplicate);
} else
{
ptr2 = ptr2->ptr;
}
ptr1 = ptr1->ptr;
}
}
}
When i display I am getting different answer but search is working. It could be a semantic error.Please help to correct it. It could have an error somewhere. I don't know where is it.Please help me to find it.The insert does not work in the program.It could be an error somewhere.I need this trie data structure to work.
#include<stdio.h>
#include<stdlib.h>
#define maxlength 10
typedef struct node
{
int isend;
struct node *branch[27];
}trinode;
int len,count;
trinode *createnode()
{
trinode *new=(trinode *)malloc(sizeof(trinode));
int ch;
for(ch=0;ch<27;ch++)
{
new->branch[ch]=NULL;
}
new->isend=0;
return new;
}
trinode *insert_trie(trinode *root,char *newenty)
{int ind;
trinode *proot;
if(root==NULL)
root=createnode();
proot=root;
for(int i=0;i<maxlength;i++)
{
ind=newenty[i]-'a';
if(newenty[i]=='\0')
break;
else
{
if(root->branch[ind]==NULL)
root->branch[ind]=createnode();
root=root->branch[ind];
}
}
if(root->isend!=0)
printf("trying to insert duplicate");
else
root->isend=1;
return proot;
}
void print_trie(trinode *cur)
{
int count;
char word[40];
for(int i=0;i<26;i++)
{
if(cur->branch[i]!=NULL)
{
word[count++]=(i+'a');
if((cur->branch[i]->isend)==1)
{
printf("\n");
for(int j=0;j<count;j++)
{
printf("%c",word[j]);
}
}
print_trie(cur->branch[i]);
}
}
count--;
}
int search_trie(trinode *root,char *target)
{
int ind;
for(int i=0;i<maxlength &&root;i++)
{
ind=target[i]-'a';
if(target[i]=='\0')
break;
else
root=root->branch[ind];
}
if(root && root->isend==1)
return 1;
else
return 0;
}
int main()
{
int ch;
trinode *root=NULL;
char *newenty;
char *target;
int check;
newenty = (char *)malloc(maxlength);
target= (char *)malloc(maxlength);
while(1)
{
printf("\n enter option 1.insert_trie 2.display 3.search 4.exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter word");
scanf("%s",newenty);
root=insert_trie(root,newenty);
break;
case 2:
count =0;
print_trie(root);
break;
case 3:
printf("enter elem you want to search");
scanf("%s",target);
check=search_trie(root,target);
if(check==0)
printf("word not found");
else
printf("found");
break;
case 4:
exit(0);
break;
}
}
}
So, I have to read a linked list from a binary file and then add new elements to it. It reads from the binary as expected, but when I try to add new elements to the list, it adds only the last one, which I enter.
Here is the code in main() responsible for reading, creating and inserting a new element into the List
int main()
{
int choice=0;
int confirmation=0;
t_Node *temp_node1, *temp_node2, *temphead;
unsigned int ID1, ID2;
FILE *fp;
fp = fopen("Data.bin","rb");
if(fp==NULL)
{
printf("File error");
exit(6);
}
while(1)
{
if(feof(fp))
{
break;
}
if (head == NULL)
{
head = readNode(fp);
temphead = head;
}else
{
temphead = readlist(temphead, fp);
}
}
fclose(fp);
while(1)
{
printf("\n\n");
printf("Hello world!\n\n 1-Enter a new element/s.\n 2-Copy data from one element to another (by ID).\n 3-Switch data between two elements (by ID).\n 4-Delete an element (by ID)\n 5-Print all elements\n 6-Delete all elements\n 0-Exit\n");
scanf("%d",&choice);
if(choice==0)
{
system("cls");
delete_List(head);
printf("EXIT BY USER");
break;
}
switch(choice)
{
case 1:
system("cls");
int temp=0;
t_Node *temphead1, *searched1, *searched2;
do
{
temphead1=makelist(temphead);
printf("press 0 to stop, any other key to go on\n");
fflush(stdin);
scanf("%d",&temp);
}while (temp!=0);
break;
case 2:
system("cls");
confirmation=0;
ID1=0;
ID2=0;
do
{
do
{
printf("Enter ID of the element you want to copy\n");
scanf("%u",&ID1);
temp_node1=Find_By_Id(ID1);
}while( temp_node1==NULL);
printNode(temp_node1);
printf("\nAre you sure?\n 1 for yes, 0 for no\n");
scanf("%d",&confirmation);
}while (confirmation==0);
confirmation=0;
do{
do{
printf("Enter ID of the element you want to copy to\n");
scanf("%u",&ID2);
temp_node2=Find_By_Id(ID2);
}while(temp_node2==NULL);
printNode(temp_node2);
printf("\nAre you sure?\n 1 for yes, 0 for no\n");
scanf("%d",&confirmation);
}while (confirmation==0);
confirmation=0;
Copy_first_to_second(temp_node1, temp_node2);
break;
case 3:
system("cls");
ID1=0;
ID2=0;
do{
do{
printf("Enter ID of the first element you want to switch data with\n");
scanf("%u",&ID1);
temp_node1=Find_By_Id(ID1);
}while( temp_node1==NULL);
printNode(temp_node1);
printf("\nAre you sure?\n 1 for Yes, 0 for No\n");
scanf("%d",&confirmation);
} while (confirmation==0);
confirmation=0;
do
{
do
{
printf("Enter ID of the second element you want to switch data with\n");
scanf("%u",&ID2);
temp_node2=Find_By_Id(ID2);
} while( temp_node2==NULL);
printNode(temp_node2);
printf("\nAre you sure?\n 1 for yes, 0 for no\n");
scanf("%d",&confirmation);
}while(confirmation==0);
confirmation=0;
Copy_first_to_second(temp_node1, temp_node2);
break;
case 4:
system("cls");
ID1=0;
do
{
do
{
printf("Enter ID of the element you want to delete\n");
scanf("%u",&ID1);
temp_node1=Find_By_Id(ID1);
} while( temp_node1==NULL);
printNode(temp_node1);
printf("\nAre you sure?\n 1 for yes, 0 for no\n");
scanf("%d",&confirmation);
}while (confirmation==0);
confirmation=0;
Delete_Element(temp_node1);
break;
case 5:
system("cls");
if (head==NULL)
{
printf("\nEnter elements first");
break;
}
printlist(head);
break;
case 6:
system("cls");
delete_List(head);
printf("List deleted");
break;
default:
delete_List(head);
exit(1);
}
}
return 0;
}
The structures:
typedef struct something
{
char name[50];
double value;
unsigned int ID;
}t_Something;
typedef struct node
{
t_Something smth;
struct node *next;
}t_Node;
And the functions:
t_Something readElement (FILE *file)
{
t_Something t_Struct;
int check=0;
unsigned int t_ID;
fread(&t_Struct,sizeof(t_Struct),1,file);
do{
t_ID=t_Struct.ID;
check=checkID(t_ID);
if (check==1)
{
break;
}
}while(1);
return t_Struct;
}
t_Something addElement()
{
t_Something t_Struct;
int check=0;
unsigned int t_ID;
printf("\nInput name\t");
fflush(stdin);
gets(t_Struct.name);
printf("\nInput value\t");
fflush(stdin);
scanf("%0.2lf",&t_Struct.value);
printf("\nInput ID, must be unique\t");
fflush(stdin);
do
{
scanf("%u",&t_Struct.ID);
t_ID=t_Struct.ID;
check=checkID(t_ID);
if (check==1)
{
break;
}
}while (1);
return t_Struct;
}
t_Node* addNode (t_Node *temphead)
{
t_Node *pNode;
pNode = (t_Node*)malloc(sizeof(t_Node));
if (pNode==NULL)
{
printf("\nMemory error\n");
delete_List(head);
}
pNode->smth=addElement();
pNode->next=NULL;
return pNode;
}
t_Node* makelist(t_Node *temphead)
{
t_Node *temp2=addNode(temphead);
temphead->next=temp2;
return temp2;
}
t_Node* readNode (FILE *file)
{
t_Node *pNode;
pNode = (t_Node*)malloc(sizeof(t_Node));
if (pNode==NULL)
{
printf("\nMemory error\n");
delete_List(head);
}
pNode->smth=readElement(file);
pNode->next=NULL;
return pNode;
}
t_Node* readlist (t_Node *temphead, FILE *file)
{
t_Node *temp2=readNode(file);
temphead->next=temp2;
return temphead->next;
}
void printlist (t_Node* current){
for (;;){
printNode(current);
current=current->next;
if (current==NULL) break;
}
}
void printNode (t_Node* current)
{
printf("\n--------\n");
printf("name: %s\n", current->smth.name);
printf("value: %lf", current->smth.value);
printf("\nID: %u\n", current->smth.ID);
}
void delete_List (t_Node* current)
{
t_Node *temp;
for (;;){
temp=current->next;
free(current);
current=temp;
if (current==NULL)
{
break;
}
}
}
int checkID (unsigned int number)
{
t_Node *current=head;
int pass=1;
if (head!=NULL)
{
for(;;)
{
if(current->smth.ID==number)
{
pass=0;
return pass;
}
current=current->next;
if (current==NULL)
{
break;
}
}
}
return pass;
}
t_Node* Find_By_Id (unsigned int searched)
{
t_Node* current=head;
for (;;)
{
if (searched==current->smth.ID)
{
return current;
}
current=current->next;
if (current==NULL)
{
break;
}
}
printf("\nThere is no element with this ID\n");
return NULL;
}
void Change_two_elements (t_Node* first, t_Node* second)
{
t_Something temp;
temp=first->smth;
first->smth=second->smth;
second->smth=temp;
}
void Copy_first_to_second (t_Node* first, t_Node* second)
{
second->smth=first->smth;
}
void Delete_Element (t_Node* element)
{
t_Node* temp;
if (element==head)
{
head=element->next;
free(element);
}else
{
for(temp=head;;)
{
if (temp->next==element)
{
break;
}
temp=temp->next;
}
temp->next=element->next;
free(element);
}
}
Sorry for the large amount of code, but otherwise it wouldn't be possible to see the full picture
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
void insert_by_priority(int);
void delete_by_priority(int);
void create();
void check(int);
void display_pqueue();
int pri_que[MAX];
int front, rear;
void priority()
{
int n, ch;
char v[20];
printf("\n1 - Insert");
printf("\n2 - Delete");
printf("\n3 - Display");
printf("\n4 - Exit");
create();
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter age to be inserted : ");
scanf("%d",&n);
printf("\nEnter name:");
scanf("%s",&v);
insert_b
y_priority(n);
printf("%s");
break;
case 2:
printf("\nEnter age to delete : ");
scanf("%d",&n);
delete_by_priority(n);
break;
case 3:
display_pqueue();
break;
case 4:
exit(0);
default:
printf("\nChoice is incorrect, Enter a correct choice");
}
}
}
void create()
{
front = rear = -1;
}
void insert_by_priority(int data)
{
if (rear >= MAX - 1)
{
printf("\nQueue overflow no more elements can be inserted");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pri_que[rear] = data;
return;
}
else
check(data);
rear++;
}
void check(int data)
{
int i,j;
for (i = 0; i <= rear; i++)
{
if (data >= pri_que[i])
{
for (j = rear + 1; j > i; j--)
{
pri_que[j] = pri_que[j - 1];
}
pri_que[i] = data;
return;
}
}
pri_que[i] = data;
}
void delete_by_priority(int data)
{
int i;
if ((front==-1) && (rear==-1))
{
printf("\nQueue is empty no elements to delete");
return;
}
for (i = 0; i <= rear; i++)
{
if (data == pri_que[i])
{
for (; i < rear; i++)
{
pri_que[i] = pri_que[i + 1];
}
pri_que[i] = -99;
rear--;
if (rear == -1)
front = -1;
return;
}
}
printf("\n%d not found in queue to delete", data);
}
void display_pqueue()
{
if ((front == -1) && (rear == -1))
{
printf("\nQueue is empty");
return;
}
for (; front <= rear; front++)
{
printf(" %d ", pri_que[front]);
}
front = 0;
}
I have this code on priority queue but i am unable to insert a string alongside the integer like a sorted record i.e
AGE | NAME
78 | abcd
75 | bcdfe
.............etc
I tried using scanf statements but to no avail it prints it seperately or do i have to use emplace?what will be the code changes? but how to implement using user input?Or do i have to implement another data structure? if yes then how?
I have created a program to implement a Circular Queue with insert, delete and display. The insertion is working fine and the deletion too but once I try to enter numbers after deletion, nothing is displayed. Here is my 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 into queue.\n");
printf("\n2.Dequeue from queue.\n");
printf("\n3.display quesue elements\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the 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 you want to 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 is 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 to display");
else
{
printf("\n The queue elements are:\n ");
for(i=front;i<=rear;i++)
{
printf("\t %d",queue[i]);
}
}
}
Check this code its working fine.
Few suggestions don't use conio.h [clrscr(), and getch()] these are not standards.
Try the code below it worked for 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 into queue.\n");
printf("\n2.Dequeue from queue.\n");
printf("\n3.display quesue elements\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the 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 you want to 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 is 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 to display");
else
{
printf("\n The queue elements are:\n ");
for(i=front;i<=rear;i++)
{
printf("\t %d",queue[i]);
}
}
}
OUTPUT
Do you want to continue (1/0): 1
1.Enqueue into queue.
2.Dequeue from queue.
3.display quesue elements
Enter your choice: 3
The queue elements are:
5 5
Do you want to continue (1/0): 1
1.Enqueue into queue.
2.Dequeue from queue.
3.display quesue elements
Enter your choice: 1
Enter the value of item: 43
Do you want to continue (1/0): 1
1.Enqueue into queue.
2.Dequeue from queue.
3.display quesue elements
Enter your choice: 3
The queue elements are:
5 5 43
Do you want to continue (1/0): 1
1.Enqueue into queue.
2.Dequeue from queue.
3.display quesue elements
Enter your choice: 2
Item dequeued: 5
Do you want to continue (1/0): 1
1.Enqueue into queue.
2.Dequeue from queue.
3.display quesue elements
Enter your choice: 3
The queue elements are:
5 43
Do you want to continue (1/0):
Thats a rather confusing code. Since the idea is for the queue to be circular, trying to determine if the queue is full by checking the position of front and rear can be really tricky, aswell as trying to make decisions based on front being higher than rear etc. You can avoid all that. A third variable keeping track of the length of the queue will make your life a whole lot easier.
Try this implementation instead:
#define SIZE 5
int queue[SIZE];
int read = 0, write = 0, size = 0;
void enqueue(int item)
{
if (size >= SIZE)
{
printf("Queue is full");
return;
}
queue[write] = item;
write = (write + 1) % SIZE;
size++;
}
int dequeue()
{
if (size == 0)
{
printf("Queue is empty");
return 0;
}
read %= SIZE;
size--;
return queue[read++];
}
Here is your Fully Functional updated code. I pasted code on Ideone.com Link is link to code
If you don't understand anything just ask. I tried to explain it using comments. And now it is not having any problem that you were facing .
#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 into queue.\n");
printf("\n2.Dequeue from queue.\n");
printf("\n3.display quesue elements\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the 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 you want to continue (1/0): ");
scanf("%d",&cont);
}
getch();
}
void enqueue(int item)
{
int temp = (rear+1)%SIZE; //EDIT HERE
if(temp == front){
printf("\n Queue OverFlow Occured");
return;
}
else if(front==-1 )
{
front=rear=0;
queue[rear]=item;
return;
}
else{
rear = (rear+1)%SIZE; // EDIT HERE
queue[rear%SIZE]=item;
}
}
int dequeue()
{
int item = NULL;
if(front == rear) // modified condition
{
printf("\nQueue is empty. Dequeue not possible.\n");
}
else
{
item = queue[front];
queue[front] = NULL;
front++; // front must be incremented
if(front > rear)
{
front = -1;
rear = -1;
}
}
return(item);
}
void display()
{
int i;
if(front==-1)
printf("\n No elements to display");
else
{
printf("\n The queue elements are:\n ");
for(i=front;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}
}