summaryrefslogtreecommitdiff
path: root/src/02/05/stack.c
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-08-05 21:35:29 -0600
committermo khan <mo.khan@gmail.com>2020-08-05 21:35:29 -0600
commit8ebf2af9526cbc66dfdb1a305cb5d594c19d45b7 (patch)
treeac16b8a393ab8f856841c64fd9b88f4b3523d42f /src/02/05/stack.c
parent97628ebac511e3cb9d19921471bbedf180ba4025 (diff)
Implement stack peek
Diffstat (limited to 'src/02/05/stack.c')
-rw-r--r--src/02/05/stack.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/02/05/stack.c b/src/02/05/stack.c
index 18a344a..26ef956 100644
--- a/src/02/05/stack.c
+++ b/src/02/05/stack.c
@@ -8,6 +8,16 @@ Node *node_init(int data) {
return node;
}
+Node *node_tail(Node *self) {
+ Node *current = self;
+ while (current) {
+ if (current->next == NULL)
+ return current;
+ current = current->next;
+ }
+ return NULL;
+}
+
Stack *stack_init(int data) {
Stack *stack = malloc(sizeof(Stack));
stack->head = node_init(data);
@@ -27,3 +37,10 @@ int stack_size(Stack *self) {
return count;
}
+
+int stack_peek(Stack *self) {
+ Node *tail = node_tail(self->head);
+ if (tail)
+ return tail->data;
+ return -1;
+}