Note that there are some explanatory texts on larger screens.

plurals
  1. POC Typed Stack: Linked-List Implementation
    text
    copied!<p>I have a computer science exam coming up and a large portion is going to be on programming in ansi C. I'm new to C and as a review I tried to implement a stack using a linked-list. My code compiles but it doesn't run as expected.</p> <p>I believe there is an error with my push() function. I'm assuming that my stack reference isn't being updated properly.</p> <p>I wrote the following code from scratch to study / practice. Any advice on how to fix my code or improve my programming style would be greatly appreciated.</p> <p>Thanks guys!</p> <hr> <p><strong>stack.h</strong></p> <pre><code>#ifndef __STACK__ #define __STACK__ #include &lt;stdlib.h&gt; #include "bool.h" #define EMPTY -1 typedef struct Node { int index; enum { INT = 0, CHAR, STRING } type; union { int i; char c; char* s; } value; struct Node* prev; } Node; typedef Node* Stack; Stack init(); void push(Stack stack, int type, void* value); void pop(Stack stack, Node* node); void empty(Stack stack); Bool isempty(Stack stack); #endif </code></pre> <p><strong>stack.c</strong></p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include "stack.h" Stack init() { Stack stack = (Node*) malloc(sizeof(Node)); stack-&gt;index = EMPTY; stack-&gt;type = INT; stack-&gt;value.i = 0; stack-&gt;prev = 0; return stack; } void push(Stack stack, int type, void* value) { int length; Node* node = (Node*) malloc(sizeof(Node)); node-&gt;index = stack-&gt;index + 1; node-&gt;type = type; switch(node-&gt;type) { case INT: node-&gt;value.i = *((int*) value); break; case CHAR: node-&gt;value.c = *((char*) value); break; case STRING: length = strlen((char*) value) + 1; node-&gt;value.s = (char*) malloc(length * sizeof(char)); strcpy(node-&gt;value.s, value); break; } node-&gt;prev = stack; stack = node; } void pop(Stack stack, Node* node) { int length; Node* temp = stack; if (!isempty(stack)) { node-&gt;index = stack-&gt;index; node-&gt;type = stack-&gt;type; switch(stack-&gt;type) { case INT: node-&gt;value.i = stack-&gt;value.i; break; case CHAR: node-&gt;value.c = stack-&gt;value.c; break; case STRING: length = strlen(stack-&gt;value.s) + 1; node-&gt;value.s = (char*) malloc(length * sizeof(char)); strcpy(node-&gt;value.s, stack-&gt;value.s); free(stack-&gt;value.s); break; } node-&gt;prev = 0; stack = stack-&gt;prev; free(temp); } else { /*TODO: handle empty case */ puts("Stack empty!"); } } void empty(Stack stack) { while (!isempty(stack)) { Node* temp = malloc(sizeof(Node)); pop(stack, temp); free(temp); } } Bool isempty(Stack stack) { return stack-&gt;index == EMPTY ? TRUE : FALSE; } </code></pre> <p><strong>bool.h</strong></p> <pre><code>#ifndef __BOOL__ #define __BOOL__ typedef int Bool; #define FALSE 0 #define TRUE 1 #endif </code></pre> <p><strong>main.c</strong></p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include "stack.h" int main() { Stack stack = init(); Node* node = malloc(sizeof(Node)); int i = 5; push(stack, 0, &amp;i); pop(stack, node); printf("Node value: %d\n", node-&gt;value.i); free(node); empty(stack); puts("done."); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload