Skip to content

Commit 3ed1024

Browse files
authored
Merge pull request #9 from ascii-dev/implement_linked_list_based_stack
implement linked list based stack
2 parents 32ee566 + e7f1f63 commit 3ed1024

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

c/includes/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
exports_files(["greet.h"])
22
exports_files(["vector.h"])
33
exports_files(["slinkedlist.h"])
4+
exports_files(["llstack.h"])

c/includes/llstack.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef C_LLSTACK_H
2+
#define C_LLSTACK_H
3+
#include <stddef.h>
4+
5+
typedef struct LLStack LLStack;
6+
7+
LLStack* llstack_new();
8+
void llstack_push(LLStack* stack, const void* item);
9+
void* llstack_pop(LLStack* stack);
10+
short llstack_empty(LLStack* stack);
11+
void llstack_free(LLStack* stack);
12+
13+
#endif // C_LLSTACK_H

c/src/stack/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_library")
2+
3+
cc_library(
4+
name = "llstack",
5+
srcs = ["llstack.c"],
6+
hdrs = ["//c/includes:llstack.h", "//c/includes:slinkedlist.h"],
7+
strip_include_prefix = "//c",
8+
deps = ["//c/src/linkedlist:slinkedlist"],
9+
visibility = ["//visibility:public"],
10+
)

c/src/stack/llstack.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "includes/llstack.h"
2+
3+
#include <stdlib.h>
4+
5+
#include "includes/slinkedlist.h"
6+
7+
struct LLStack {
8+
SLinkedList* list;
9+
};
10+
11+
LLStack* llstack_new() {
12+
LLStack* stack = malloc(sizeof(LLStack));
13+
if (stack == NULL) return NULL;
14+
15+
stack->list = slinkedlist_new();
16+
return stack;
17+
}
18+
19+
void llstack_push(LLStack* stack, const void* item) { slinkedlist_push_front(stack->list, item); }
20+
21+
void* llstack_pop(LLStack* stack) { return slinkedlist_pop_front(stack->list); }
22+
23+
short llstack_empty(LLStack* stack) { return slinkedlist_empty(stack->list); }
24+
25+
void llstack_free(LLStack* stack) {
26+
slinkedlist_free(stack->list);
27+
free(stack);
28+
}

c/tests/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ cc_test(
2626
"@googletest//:gtest_main",
2727
],
2828
)
29+
30+
cc_test(
31+
name = "llstack_test",
32+
srcs = ["llstack_test.cc"],
33+
deps = [
34+
"//c/src/stack:llstack",
35+
"@googletest//:gtest_main",
36+
],
37+
)

c/tests/llstack_test.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "gtest/gtest.h"
2+
3+
extern "C" {
4+
#include "includes/llstack.h"
5+
}
6+
7+
TEST(LLStackTest, NewLLStack) {
8+
LLStack* stack = llstack_new();
9+
ASSERT_NE(stack, nullptr);
10+
ASSERT_EQ(llstack_empty(stack), 1);
11+
llstack_free(stack);
12+
}
13+
14+
TEST(LLStackTest, PushAndPop) {
15+
LLStack* stack = llstack_new();
16+
ASSERT_NE(stack, nullptr);
17+
ASSERT_EQ(llstack_empty(stack), 1);
18+
int item1 = 10;
19+
int item2 = 20;
20+
int item3 = 30;
21+
llstack_push(stack, &item1);
22+
llstack_push(stack, &item2);
23+
llstack_push(stack, &item3);
24+
25+
ASSERT_EQ(llstack_empty(stack), 0);
26+
ASSERT_EQ(llstack_pop(stack), &item3);
27+
ASSERT_EQ(llstack_empty(stack), 0);
28+
ASSERT_EQ(llstack_pop(stack), &item2);
29+
ASSERT_EQ(llstack_empty(stack), 0);
30+
ASSERT_EQ(llstack_pop(stack), &item1);
31+
ASSERT_EQ(llstack_empty(stack), 1);
32+
33+
llstack_free(stack);
34+
}

0 commit comments

Comments
 (0)