-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.c
125 lines (92 loc) · 2.14 KB
/
queue.c
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdlib.h>
#include <stdio.h>
#include "queue.h"
void append ( int row, int col, queue *l )
{
elem *el = malloc ( sizeof ( elem ) );
el->point.row = row;
el->point.col = col;
el->next = NULL;
// set the first point if queue is empty, otherwise
// update the link from the last element to new point
if ( l->first == NULL )
l->first = el;
else
l->last->next = el;
// update the last in the queue
l->last = el;
// update the length attribute
l->length += 1;
}
elem *pop ( queue *l )
{
//
// return the first element of a queue and remove from the queue
// if the queue is empty return NULL
elem *el = l->first;
if ( el != NULL )
{
l->first = el->next;
l->length -= 1;
}
return el;
}
void array_append ( int indx, int row, int col, queue **l )
{
append ( row, col, l[indx] );
}
void populate ( int nrows, queue *l )
{
for ( int row = 0; row < nrows; row++ )
append ( row, 0, l );
}
queue **create_empty_array_of_queue( int nrows )
{
queue **a = malloc ((long unsigned int)nrows * sizeof ( queue * ) );
for(int i = 0; i < nrows; i++)
a[i] = create_empty_queue();
return a;
}
queue * create_empty_queue()
{
queue *l = malloc ( sizeof ( queue ) );
l->length = 0;
l->first = l->last = NULL;
return l;
}
void print_array_of_queue ( queue **l, int nrows )
{
elem *e;
for ( int i = 0; i < nrows; i++ )
{
printf("\nrow: %d, length: %d\n", i, l[i]->length);
if (l[i]->length != 0)
{
e = l[i]->first;
for ( int j = 0; j < l[i]->length; j++ )
{
printf ( "%d ", e->point.col );
e = e->next;
}
}
}
}
void print_queue ( queue *l )
{
elem *e = l->first;
for ( int i = 0; i < l->length; i++ )
{
printf ( "%d) row=%d col=%d\n", i, e->point.row, e->point.col );
e = e->next;
}
}
void free_queue ( queue *l )
{
elem *e = l->first, *p;
for ( int i = 0; i < l->length; i++ )
{
p = e;
e = e->next;
free ( p );
}
}