libdatatypes 0.3.2
Abstract datatypes for C.
Loading...
Searching...
No Matches
stack.c
Go to the documentation of this file.
1/***************************************************************************
2 begin........: June 2012
3 copyright....: Sebastian Fedrau
4 email........: sebastian.fedrau@gmail.com
5 ***************************************************************************/
6
7/***************************************************************************
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License v3 as published by
10 the Free Software Foundation.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License v3 for more details.
16 ***************************************************************************/
22#include <stdlib.h>
23#include <assert.h>
24
25#include "stack.h"
26
27bool
28stack_pop(Stack *stack, void **data)
29{
30 assert(stack != NULL);
31 assert(data != NULL);
32
33 bool success = false;
34
35 if(slist_count(stack))
36 {
37 void *ptr = slist_pop(stack);
38
39 if(data)
40 {
41 *data = ptr;
42 }
43
44 success = true;
45 }
46 else if(data)
47 {
48 *data = NULL;
49 }
50
51 return success;
52}
53
54bool
55stack_head(const Stack *stack, void **data)
56{
57 assert(stack != NULL);
58 assert(data != NULL);
59
60 SListItem *iter = slist_head(stack);
61 bool success = false;
62
63 if(iter)
64 {
65 *data = iter->data;
66 success = true;
67 }
68
69 return success;
70}
71
size_t slist_count(const SList *list)
Definition slist.c:410
SListItem * slist_head(const SList *list)
Definition slist.c:418
void * slist_pop(SList *list)
Definition slist.c:325
void * data
Definition slist.h:39
Singly-linked list.
Definition slist.h:49
Structure holding a list item.
Definition slist.h:37
bool stack_head(const Stack *stack, void **data)
Definition stack.c:55
bool stack_pop(Stack *stack, void **data)
Definition stack.c:28
Generic stack.