-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
107 lines (98 loc) · 3 KB
/
stack.c
File metadata and controls
107 lines (98 loc) · 3 KB
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
/*
* This file is where you should implement your stack. It already contains
* skeletons of the functions you need to implement (along with documentation
* for each function). Feel free to implement any additional functions you
* might need. Also, don't forget to include your name and @oregonstate.edu
* email address below.
*
* Name:
* Email:
*/
#include <stdlib.h>
#include "stack.h"
#include "list.h"
/*
* This is the structure that will be used to represent a stack. This
* structure specifically contains a single field representing a linked list
* that should be used as the underlying data storage for the stack.
*
* You should not modify this structure.
*/
struct stack {
struct list* list;
};
/*
* This function should allocate and initialize a new, empty stack and return
* a pointer to it.
*/
struct stack* stack_create() {
struct stack* stack = malloc(sizeof(struct stack));
stack->list = list_create();
return stack;
}
/*
* This function should free the memory associated with a stack. While this
* function should up all memory used in the stack itself, it should not free
* any memory allocated to the pointer values stored in the stack. This is the
* responsibility of the caller.
*
* Params:
* stack - the stack to be destroyed. May not be NULL.
*/
void stack_free(struct stack* stack) {
list_free(stack->list);
free(stack);
return;
}
/*
* This function should indicate whether a given stack is currently empty.
* Specifically, it should return 1 if the specified stack is empty (i.e.
* contains no elements) and 0 otherwise.
*
* Params:
* stack - the stack whose emptiness is being questioned. May not be NULL.
*/
int stack_isempty(struct stack* stack) {
int val = isEmpty(stack->list);
return val;
}
/*
* This function should push a new value onto a given stack. The value to be
* pushed is specified as a void pointer. This function must have O(1)
* average runtime complexity.
*
* Params:
* stack - the stack onto which a value is to be pushed. May not be NULL.
* val - the value to be pushed. Note that this parameter has type void*,
* which means that a pointer of any type can be passed.
*/
void stack_push(struct stack* stack, void* val) {
list_insert(stack->list, val);
return;
}
/*
* This function should return the value stored at the top of a given stack
* *without* removing that value. This function must have O(1) average runtime
* complexity.
*
* Params:
* stack - the stack from which to query the top value. May not be NULL.
*/
void* stack_top(struct stack* stack) {
void* val = return_top_val(stack->list);
return val;
}
/*
* This function should pop a value from a given stack and return the popped
* value. This function must have O(1) average runtime complexity.
*
* Params:
* stack - the stack from which a value is to be popped. May not be NULL.
*
* Return:
* This function should return the value that was popped.
*/
void* stack_pop(struct stack* stack) {
void* val = pop_from_stack(stack->list);
return val;
}