queue

Hi,

When I try to run the below code, there's an error. The compiler just hanged on me. Any idea what is wrong?

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
#include <iostream>
using namespace std;

const int MAXQUEUE = 20;

struct myqueue{
    int items[MAXQUEUE];
    int front, rear;
};

int empty(struct myqueue *pq){
    return ((pq->front == pq->rear) ? 1:0);
}/*empty queue*/

int dequeue(struct myqueue *pq){
    if (empty(pq)){
        cout << "queue underflow" << endl;
        exit(1);
    }/* end if */
    if (pq->front == MAXQUEUE-1)
        pq->front = 0;
    else
        (pq->front)++;
    return (pq->items[pq->front]);
}/* end dequeue */

int enqueue(struct myqueue *pq, int x){
    /*make room for new element*/
    if (pq->rear == MAXQUEUE-1)
        pq->rear = 0;

    else{
        (pq->rear)++;
        /* check for overflow */
        if (pq->rear == pq->front){
            cout << "queue overflow" << endl;
            exit(1);
        }/* end if */

        pq->items[pq->rear] = x;
        return 0;
    }
}/* end enqueue*/

int front(struct myqueue *pq){
    if (empty(pq)){
        cout << "queue underflow" << endl;
        exit(1);

    }/*end if*/
    return (pq->items[pq->front]);

}/* end front */




int main(){
    myqueue *q;
    q->front = 0;
    q->rear = 0;
    enqueue(q,5);

    return 0;
}
What does line 33 do?
front 0
rear -1
No it doesn't. Anyway, what doe4s rear <= -1 mean?

This is the beginning of the problem.
rear or tail start from -1 not from zero,right?
Not quite. A pointer either points to something or nothing, indicated by null or zero. -1 is meaningless in this context.
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
#include<iostream>
using namespace std;

const int MAXQUEUE = 20;

struct myqueue{
	 int items[MAXQUEUE];
	 int front, rear;
};

int empty(myqueue *pq){
	 pq->front=0;
    pq->rear=-1;
}/*empty queue*/

int destroy(myqueue *pq,int x){
    if (pq->front>pq->rear){
        cout << "queue underflow" << endl;
		  exit(1);
    }/* end if */
	 else
		{
      	pq->items[pq->front]=x;
		 	(pq->front)++;
      }

}/* end dequeue */

int insert(struct myqueue *pq, int x){
    /*make room for new element*/
	 if (pq->rear == MAXQUEUE-1)
       cout << "queue overflow" << endl;
         /* check for overflow */
	 else{
		  (pq->rear)++;
		  pq->items[pq->rear] = x;
		  return 0;
	 }
}/* end enqueue*/


int main(){
   int place;
   myqueue queue;
   	empty(&queue);
      place=10;   //place is value to put into the queue
      insert(&queue,place);
      cout<<queue.items[queue.rear];
      cin>>place;
    return 0;
}


how about now mr.kbw
Im beginner in queue
Topic archived. No new replies allowed.