-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit - move project to github
- Loading branch information
0 parents
commit 62ffc2a
Showing
4 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Prerequisites | ||
*.d | ||
|
||
# Object files | ||
*.o | ||
*.ko | ||
*.obj | ||
*.elf | ||
|
||
# Linker output | ||
*.ilk | ||
*.map | ||
*.exp | ||
|
||
# Precompiled Headers | ||
*.gch | ||
*.pch | ||
|
||
# Libraries | ||
*.lib | ||
*.a | ||
*.la | ||
*.lo | ||
|
||
# Shared objects (inc. Windows DLLs) | ||
*.dll | ||
*.so | ||
*.so.* | ||
*.dylib | ||
|
||
# Executables | ||
*.exe | ||
*.out | ||
*.app | ||
*.i*86 | ||
*.x86_64 | ||
*.hex | ||
|
||
# Debug files | ||
*.dSYM/ | ||
*.su | ||
*.idb | ||
*.pdb | ||
|
||
# Kernel Module Compile Results | ||
*.mod* | ||
*.cmd | ||
.tmp_versions/ | ||
modules.order | ||
Module.symvers | ||
Mkfile.old | ||
dkms.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <stdio.h> | ||
#include "stack.h" | ||
|
||
|
||
/* - Program Start - */ | ||
|
||
int main(int argc, char *argv[]) { | ||
printf("Making stack: "); | ||
Node *stack = make_stack(); | ||
if (!stack) { | ||
printf("fail\nProgram terminated.\n"); | ||
return 0; | ||
} | ||
printf("success\n\n"); | ||
|
||
printf("Peaking at empty stack behaves as expected: %s\n", peak(stack) == '\0' ? "success" : "fail"); | ||
printf("Popping from empty stack behaves as expected: %s\n", pop(stack) == '\0' ? "success" : "fail"); | ||
|
||
printf("\nPushing alphabet to stack...\n"); | ||
for (char c = 'a'; c <= 'z'; c++) { | ||
printf("Pushing %c to stack: %s\n", c, push(stack,c) ? "success" : "fail"); | ||
} | ||
|
||
printf("\nTesting peak and pop...\n"); | ||
printf("peak: '%c'\n", peak(stack)); | ||
printf("pop: '%c'\n", pop(stack)); | ||
printf("pop: '%c'\n\n", pop(stack)); | ||
|
||
printf("Destroying stack: %s\n", free_stack(stack) ? "success" : "fail"); | ||
printf("Setting stack pointer to NULL: %s\n\n", (stack = NULL) ? "fail" : "success"); | ||
|
||
printf("Pushing to NULL stack behaves as expected: %s\n", push(stack, 'a') ? "fail" : "success"); | ||
printf("Peaking at NULL stack behaves as expected: %s\n", peak(stack) == '\0' ? "success" : "fail"); | ||
printf("Popping from NULL stack behaves as expected: %s\n", pop(stack) == '\0' ? "success" : "fail"); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include "stack.h" | ||
|
||
|
||
/* - STRUCTS - */ | ||
|
||
|
||
// Nodes contain stored data and a pointer to the next node. | ||
struct node { | ||
char character; | ||
struct node *next; | ||
}; | ||
|
||
|
||
/* - FUNCTIONS - */ | ||
|
||
|
||
// Creates a blank node that acts as the accessor for the stack structure. The | ||
// returned pointer will be NULL if malloc fails to allocate this node. | ||
Node *make_stack() { | ||
Node *new_stack = (Node *)malloc(sizeof(Node)); | ||
if (new_stack) { | ||
new_stack->character = '\0'; | ||
new_stack->next = NULL; | ||
} | ||
return new_stack; | ||
} | ||
|
||
|
||
// Adds a node to the top of the stack containing the value c. Returns false if | ||
// the stack is NULL. | ||
bool push(Node *stack, char c) { | ||
if (!stack) return false; | ||
Node *node = (Node *)malloc(sizeof(Node)); | ||
if (!node) return false; | ||
node->next = stack->next; | ||
node->character = c; | ||
stack->next = node; | ||
return true; | ||
} | ||
|
||
|
||
// Removes node at top of stack and returns its stored value | ||
char pop(Node *stack) { | ||
if (stack && stack->next) { | ||
Node *temp = stack->next; | ||
stack->next = temp->next; | ||
char return_value = temp->character; | ||
free(temp); | ||
return return_value; | ||
} | ||
return '\0'; | ||
} | ||
|
||
|
||
// Returns the value at the top of the stack without removing the node. | ||
char peak(Node *stack) { | ||
if (stack && stack->next) { | ||
return stack->next->character; | ||
} | ||
return '\0'; | ||
} | ||
|
||
|
||
// Frees every node in the stack structure. Returns false if the stack is NULL. | ||
bool free_stack(Node *stack) { | ||
if (!stack) return false; | ||
Node *temp = stack; | ||
while (stack) { | ||
temp = stack->next; | ||
free(stack); | ||
stack = temp; | ||
// printf("free node\n"); | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef STACK_H | ||
#define STACK_H | ||
|
||
#include <stdbool.h> | ||
|
||
typedef struct node Node; | ||
Node *make_stack(void); | ||
bool push(Node *, char); | ||
char pop(Node *); | ||
char peak(Node *); | ||
bool free_stack(Node *stack); | ||
|
||
#endif | ||
|