From cfaf438fd42d9f0b780a9909dd4044a2f7f08b32 Mon Sep 17 00:00:00 2001 From: ergz Date: Fri, 30 Jun 2023 07:12:43 -0700 Subject: [PATCH] last pop in stack not working yet --- c/a.out | Bin 16904 -> 16944 bytes c/queue.c | 49 +++++++++++++++++++++-- c/stack.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 c/stack.c diff --git a/c/a.out b/c/a.out index 887012c9a48a307aad39bef4efec07801ee340b8..5d3aed0992800724758d5b3ec86041e3470d7733 100755 GIT binary patch delta 3456 zcmaJ^eQZ@d-23i( zi5c6Cp3lecoZr3o+!?I{3#fv`F4X_j5-U`#*oC{UqDo;@7D737 z24e<+2}uM2MK_DgY*X=!KVSB1d2ou_tm7hTTttn-75ag-+pgy55)?z&@xK_m1!Ff@ zjNvtjUl0CazVTei%|kvsQ@i=vEZm>W<#OrI$~Jt3zzE`HHM{yIf$|=*QPSnp{ zk6Uv0%4%L?W4<9I##?xNlL2O>_4Fo~tsClZVmwJ~3CXAZZ!=X^{t_>dQ~t|nWWW4( zE|)!ob9m467CdL&)Ny*wuAH2kU>h|1DT1=nz9^^K7gM1*MPkbPH`we#$ReMflhnYQ zD*|s}CLu6TzAlMevT`zuS=kghdp)sgoI$f8=8#hXv*1SX^de2H=I&bozEHEgFFCc( z%)JVii?VVnyNUJ18AtYUMUlqYR?Qz5_Pdyz#cAT`G3f}-km+m~$xqVh6~P!ND~Wjx zO+3MKQlWWSbebX~`zUiN((D!$4_p7b9PX*?oAXk zt5>zM6D7C2n7*wsEfY;*q0~aU@`m=(|kUWo@;kP~m6`piJ>GYveypYRvqx7O2 zLir}jH&FV%z?q`VpyZbifgjj6J~58{;M1??mzMF{=stk> z&@!GZEi@eC5sY9CpLhOG2PHVeUm+e@Ccymf==oa_Uqbu>yMDC+8ix%m0Iw*r7xAIb zME;)D(s={?!?DYV9&mUnezC?=Y|$>?0KB`+Wi_v5JrPo^3X*{@x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 0) { + a->head = n; + a->tail = n; + a->data[a->length] = n; + a->length++; + } else { + a->tail = n; + a->data[a->length] = n; + a->length++; + } +} + +// not a very good idea, this will require a O(n) walk through the array to +// shift everything to the left +bool arr_dequeue(ArrayQueue* a) { + if (a->length == 0) { + return false; + } else { + } +} void enqueue(Queue* q, Node* n) { if (q->length == 0) { @@ -54,6 +91,12 @@ Node* new_node(int value) { return n; } +void free_node(Node* n) { + if (n != NULL) { + free(n); + } +} + Node* peek(Queue* q) { return q->head; } int main() { diff --git a/c/stack.c b/c/stack.c new file mode 100644 index 0000000..9c5baac --- /dev/null +++ b/c/stack.c @@ -0,0 +1,117 @@ +#include +#include +#include +/* +how a stack grows + +len = 0 ______ +push: len = 1 [A] +push: len = 2 [A] <-- [B] new elements point to the previous top +push: len = 3 [A] <-- [B] <-- [C] +pop: len = 2 [A] <-- [B] <-/- [C] need to free C here and set new head of Stack to be B +pop: len = 1 [A] <-/- [B] +pop: len = 0 /[A]/ + + + */ + +typedef struct Node { + int value; + struct Node* next; +} Node; + +typedef struct Stack { + int length; + Node* head; +} Stack; + +Node* new_node(int value) { + Node* n = malloc(sizeof(Node)); + n->value = value; + + return (n); +} + +Stack* new_stack() { + Stack* s = malloc(sizeof(Stack)); + s->length = 0; + s->head = NULL; + return (s); +} + +bool pop(Stack* s) { + if (s->length == 0) { + return false; + } else { + Node* n = s->head; + if (s->length == 1) { + s->head = NULL; + } else { + s->head = n->next; + } + free(n); + s->length--; + return true; + } +} + +void delete_stack(Stack* s) { + while (s->length > 0) { + pop(s); + } +} + +void push(Stack* s, Node* n) { + if (s->length == 0) { + s->head = n; + s->length++; + } else { + // make the old head point to what will be the new head + n->next = s->head; + // make the head point to n now + s->head = n; + s->length++; + } +} + +int main() { + Stack* s = new_stack(); + + Node* a = new_node(10); + Node* b = new_node(11); + Node* c = new_node(12); + Node* d = new_node(13); + + push(s, a); + printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the length of the stack is: %d\n", s->length); + push(s, b); + printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the length of the stack is: %d\n", s->length); + push(s, c); + printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the length of the stack is: %d\n", s->length); + push(s, d); + printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the length of the stack is: %d\n", s->length); + + bool res = pop(s); + printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the length of the stack is: %d\n", s->length); + + res = pop(s); + printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the length of the stack is: %d\n", s->length); + + res = pop(s); + printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the length of the stack is: %d\n", s->length); + + res = pop(s); + printf("the value at the top of the stack is: %d\n", s->head->value); + printf("the length of the stack is: %d\n", s->length); + + // delete_stack(s); + + return (0); +} \ No newline at end of file