-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.h
99 lines (87 loc) · 2.75 KB
/
queue.h
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
/***********************************************************************
* Homework: Maze Solving
***********************************************************************/
/* Queue data structure for storing partial solutions to the maze problem */
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <stdbool.h>
#include "path.h"
/* List-backed Queue consists of nodes, each of which has a reference to a
* solution (path), as well as the next node inthe queue.
*/
typedef struct node {
list_t * solution;
struct node * next;
} node_t;
/* Queue structure contains references to the front and rear nodes for easy
* insertion at the end and deletion from the front */
typedef struct queue {
node_t * front;
node_t * rear;
} queue_t;
/* Initialize a queue data structure
*
* Preconditions:
* queue != NULL
* queue points to a valid queue structure
* Postconditions:
* queue_empty(queue) == true
*/
void
queue_initialize (queue_t * queue);
/* Determine whether a queue is empty
*
* Preconditions:
* queue != NULL
* queue points to a valid queue structure
* queue_initialize(queue) has been called at some point
* Postconditions:
* Returns true when queue_initialize has been called on the given queue and
* both enqueue and dequeue have been called the same number of times
*/
bool
queue_empty (const queue_t * queue);
/* Insert a solution at the rear of a queue
*
* Preconditions:
* queue != NULL
* queue points to a valid queue structure
* initialize(queue) has been called at some point
* Postconditions:
* Returns whether the item was successfully inserted.
* When true, queue_empty(queue)==false and path appears at the end of
* the queue
* Only the reference path is stored; no deep copy is made and the queue is not
* responsible for managing any memory associated with path
*/
bool
enqueue (queue_t * queue, list_t* path);
/* Remove and return the item at the front of the queue
*
* Preconditions:
* queue != NULL
* queue points to a valid queue structure
* queue_initialize(queue) has been called at some point
* queue_empty(queue) is false [verified]
* Postconditions:
* Returns the item at the front of the queue (i.e., the remaining item least
* recently enqueued)
* The item has been removed from the queue
*/
list_t*
dequeue (queue_t * queue);
/* Return the solution at the front of the queue
*
* Preconditions:
* queue != NULL
* queue points to a valid queue structure
* queue_initialize(queue) has been called at some point
* queue_empty(queue) is false [verified]
* Postconditions:
* Returns the item at the front of the queue (i.e., the remaining item least
* recently enqueued)
* queue is unaltered
*/
list_t*
queue_front (const queue_t * queue);
#endif