Queue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Implementation of a circular queue of Array containg names.. */
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
# define QSIZE 5
typedef struct
{
    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

could you please tell me how? thanks.
Last edited on
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.
Last edited on
i'm not a very good coder. i'm really really a newbie. forgive me but i don't really understand that std stuff. simple codes only.
Topic archived. No new replies allowed.