forked from dale48/levawc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.h
147 lines (131 loc) · 4.15 KB
/
stack.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* _____
* ANSI / ___/
* / /__
* \___/
*
* Filename: stack.h
* Author : Kyle Loudon/Dan Levin
* Date : Fri Mar 22 12:40:45 GMT 2013
* Version : 0.40
* ---
* Description: A C interface for a generic stack ADT.
*
* Revision history - coming up below:
*
* Date Revision message
* 2012-12-20 Created this file
* 2013-02-19 Made some revision to the Doxygen documentation. Enhanced the description of
* in/out parameters - i.e. double-pointers.
*
*/
/**
* @file stack.h
**/
#ifndef _STACK_H_
#define _STACK_H_
#include <stdio.h>
#include <stdlib.h>
#include "slist.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef Slist Stack;
/* INTERFACE FUNCTION DECLARATIONS */
/**
*
* Initiate the stack.
*
* @param[in] destroy - A reference to a user-made function, reponsible
* for freeing element data, when the stack is deleted. If @a destroy is
* NULL - then element data will be left untouched when the list is
* destroyed.
* @return A reference - to a new, empty stack - if dynamic memory
* allocation for the ADT was successful - or NULL otherwise. Take really
* good care of this return value, since it will be needed as a parameter in
* subsequent calls - to the majority of other stack handling functions
* in this stack function interface - i.e. a sort of "handle" to the stack.
* @see STACKdestroy()
**/
Stack STACKinit(void (*destroy)(void *data));
/**
* Destroy the stack.
*
* The stack is destroyed - that is, all memory occupied by the
* elements is deallocated. The user-defined callback function
* @a destroy, given as an argument to @b STACKinit(), is responsible
* for freeing dynamically allocated element data, when this function
* is called. When all elements and data have been deallocated - the
* stack header is deallocated, too.
*
* @param[in] stk - a reference to current stack.
* @return Nothing.
* @see STACKinit()
**/
void STACKdestroy(Stack stk);
/**
* Insert(=push) a new element - at the top of the stack.
*
* This function inserts an new element - with a reference to
* its corresponding data, given by parameter @a data - at the
* top of the stack.
*
* @param[in] stk - reference to current stack
* @param[in] data - reference to data to be stored in the new
* element, which is to be inserted at the top of the stack.
*
* @return Value 0 - if everything went OK - or value -1 otherwise.
**/
int STACKpush(Stack stk, const void *data);
/**
* Remove(=pop) the top element.
*
* When called, the 2nd parameter of this function, @a data,
* should reference an (external, user-defined) pointer.
* After the call - this referenced, external pointer has been
* redirected, to point to the data of the removed element -
* if the call was succesful. The caller is responsible for
* the future of this memory - deallocating it, if needed,
* for example.
*
* @param[in] stk - reference to current stack.
* @param[out] data - reference to a pointer. After the call,
* this referenced pointer has been redirected to point to
* the data of the removed element - if the call was
* successful. The caller is responsible for the future of
* this memory - deallocating it, for example.
*
* @return Value 0 - if the call was OK - or
* value -1 otherwise.
**/
int STACKpop(Stack stk, void **data);
/**
* Peek at the top of the stack.
*
* @param[in] stk - reference to the current stack.
*
* @return NULL if the stack is empty - or a reference
* to data of the top element, otherwise.
*
**/
void *STACKpeek(Stack stk);
/**
* Determine if the stack is empty - or not.
*
* @param[in] stk - a reference to the current stack.
* @return Value 1 - if the stack is indeed empty -
* or 0 otherwise.
**/
int STACKisempty(Stack stk);
/**
* Get the stack size.
*
* @param[in] stk - a reference to the current stack.
*
* @return The size, that is, the number of elements in the stack.
**/
int STACKsize(Stack stk);
#ifdef __cplusplus
}
#endif
#endif /* _STACK_H_ */