/* Implementation of a circular queue of Array containg names.. */
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
# define QSIZE 5
typedefstruct
{
int count;
int head,tail;
char names[QSIZE][30];
} QUEUE;
char* enqueue(char *);
char* dequeue();
void display();
void init();
QUEUE *pq;
int main()
{
int choice;
char str[30];
QUEUE q;
pq=&q;
init();
do
{
system("cls");
printf("\n\n\t\tEnter your Choice:");
printf("\n\t\t:1 for Add into the Queue.. ");
printf("\n\t\t:2 for Delete from the Queue.. ");
printf("\n\t\t:3 Display Elements of the Queue.. ");
printf("\n\t\t:4 For Exit..\n\t\t\t :: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n\n\t\t Enter a Name:");
fflush(stdin);
gets(str);
puts(enqueue(str));
break;
case 2:
puts(dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\n\t\t Plz press 1,2,3 or 4 key..");
}
printf("\n\n\n\t Press Any Key to continue...");
fflush(stdin);
while(!kbhit());
}
while(1);
return 0;
}
void init()
{
pq->head = pq->tail = pq->count= 0;
}
char* enqueue(char *p)
{
if(pq->count==QSIZE)
return"\n\n\t\t Error: Queue Overflow..!!";
pq->tail= (pq->tail)%QSIZE;
strcpy(pq->names[(pq->tail)++],p);
pq->count++;
return"\n\n\t\t Element Successfully Inserted";
}
char* dequeue()
{
if(pq->count==0)
return"\n\n\t\t Error: Queue Underflow..!!";
pq->head= (pq->head)%QSIZE;
pq->count--;
printf("\n\n\t\t Deleted Queue Element is :");
return pq->names[(pq->head)++];
}
void display()
{
int i=pq->head;
int x=0;
if(pq->count==0)
printf("\n\n\t Queue is Empty..");
else
{
while(x<pq->count)
{
if(i==QSIZE)
i%=QSIZE;
printf("\n\t\t :%s",pq->names[i]);
i++;
x++;
}
}
}
i changed my code. but whenever i typed in the ILoveBacolod it takes it as a whole, and if i deleted it deletes the string not the letter. for example:
Enter String: ILoveBacolod
Enter a command: Delete (D)
Output: LoveBacolod
Enter a command: Delete (D)
Output: oveBacolod
Enter a command: Add (A)
Enter a character: z
Output: oveBacolodz
Isn't it just a matter of replacing the definition and initialisation of Queue from int* to std::string*?
I think you're expected to do it using a template. But first, do a string, then maybe a double. A pattern will emerge and once you get your head around templates, the solution will be obvious.