Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Chris Müller
crystal
Commits
f8525c13
Commit
f8525c13
authored
Nov 03, 2012
by
Chris Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lang: struct CryQueue supports lookups for nth and rear elements in list.
parent
e9d96ef4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
21 deletions
+48
-21
src/structures/queue.h
src/structures/queue.h
+3
-0
src/structures/single_linked_list.c
src/structures/single_linked_list.c
+45
-21
No files found.
src/structures/queue.h
View file @
f8525c13
...
...
@@ -28,6 +28,9 @@ void cry_queue_free(struct CryQueue* queue, cry_free_funptr handler)
int
cry_queue_size
(
struct
CryQueue
*
queue
);
pointer
cry_queue_front
(
struct
CryQueue
*
queue
);
pointer
cry_queue_rear
(
struct
CryQueue
*
queue
);
pointer
cry_queue_nth
(
struct
CryQueue
*
queue
,
size_t
index
);
pointer
cry_queue_dequeue
(
struct
CryQueue
*
queue
);
void
cry_queue_enqueue
(
struct
CryQueue
*
queue
,
pointer
data
);
...
...
src/structures/single_linked_list.c
View file @
f8525c13
...
...
@@ -9,7 +9,7 @@
* Crystal is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more de
tail
s.
* Lesser GNU General Public License for more de
rear
s.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
...
...
@@ -31,19 +31,19 @@ struct Stack {
struct
Node
*
begin
;
};
struct
Queue
{
struct
Cry
Queue
{
struct
Node
*
front
;
struct
Node
*
tail
;
struct
Node
*
rear
;
};
struct
CryQueue
*
cry_queue_new
(
void
)
{
struct
Queue
*
list
=
cry_malloc
(
struct
Queue
);
struct
Cry
Queue
*
list
=
cry_malloc
(
struct
Cry
Queue
);
list
->
front
=
0
;
list
->
tail
=
0
;
return
cry_cast
(
struct
CryQueue
*
,
list
)
;
list
->
rear
=
0
;
return
list
;
}
...
...
@@ -52,7 +52,7 @@ cry_queue_free(struct CryQueue* queue, cry_free_funptr handler)
{
assert
(
queue
!=
0
);
struct
Node
*
node
=
cry_cast
(
struct
Queue
*
,
queue
)
->
front
;
struct
Node
*
node
=
queue
->
front
;
struct
Node
*
tmp
=
0
;
while
(
node
!=
0
)
{
...
...
@@ -76,7 +76,7 @@ cry_queue_size(struct CryQueue* queue)
assert
(
queue
!=
0
);
size_t
length
=
0
;
struct
Node
*
node
=
cry_cast
(
struct
Queue
*
,
queue
)
->
front
;
struct
Node
*
node
=
queue
->
front
;
while
(
node
!=
0
)
{
node
=
node
->
next
;
...
...
@@ -92,10 +92,36 @@ cry_queue_front(struct CryQueue* queue)
{
assert
(
queue
!=
0
);
if
(
cry_cast
(
struct
Queue
*
,
queue
)
->
front
==
0
)
if
(
queue
->
front
==
0
)
return
0
;
else
return
cry_cast
(
struct
Queue
*
,
queue
)
->
front
->
data
;
return
queue
->
front
->
data
;
}
pointer
cry_queue_rear
(
struct
CryQueue
*
queue
)
{
assert
(
queue
!=
0
);
if
(
queue
->
rear
==
0
)
return
0
;
else
return
queue
->
rear
->
data
;
}
pointer
cry_queue_nth
(
struct
CryQueue
*
queue
,
size_t
index
)
{
assert
(
queue
!=
0
);
struct
Node
*
node
=
queue
->
front
;
while
(
node
!=
0
&&
index
--
>
0
)
node
=
node
->
next
;
return
node
;
}
...
...
@@ -104,8 +130,7 @@ cry_queue_dequeue(struct CryQueue* queue)
{
assert
(
queue
!=
0
);
struct
Queue
*
list
=
cry_cast
(
struct
Queue
*
,
queue
);
struct
Node
*
front
=
list
->
front
;
struct
Node
*
front
=
queue
->
front
;
pointer
data
=
0
;
if
(
front
==
0
)
...
...
@@ -113,10 +138,10 @@ cry_queue_dequeue(struct CryQueue* queue)
data
=
front
->
data
;
if
(
list
->
front
==
list
->
tail
)
list
->
front
=
list
->
tail
=
0
;
if
(
queue
->
front
==
queue
->
rear
)
queue
->
front
=
queue
->
rear
=
0
;
else
list
->
front
=
front
->
next
;
queue
->
front
=
front
->
next
;
cry_free
(
front
);
...
...
@@ -129,18 +154,17 @@ cry_queue_enqueue(struct CryQueue* queue, pointer data)
{
assert
(
queue
!=
0
);
struct
Queue
*
list
=
cry_cast
(
struct
Queue
*
,
queue
);
struct
Node
*
node
=
cry_malloc
(
struct
Node
);
node
->
next
=
0
;
node
->
data
=
data
;
if
(
list
->
front
==
0
)
{
list
->
front
=
node
;
list
->
tail
=
node
;
if
(
queue
->
front
==
0
)
{
queue
->
front
=
node
;
queue
->
rear
=
node
;
}
else
{
list
->
tail
->
next
=
node
;
list
->
tail
=
node
;
queue
->
rear
->
next
=
node
;
queue
->
rear
=
node
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment