Creating a Stack of Strings in C

I want to have a stack that accepts strings. I want to be able to push and pop lines as well as clear the entire stack. I think C ++ has some methods for this. How about C?

+4
source share
3 answers

A quick and dirty untested example. Uses a single-linked list structure; items are clicked and pushed out of the list header.

#include <stdlib.h> #include <string.h> /** * Type for individual stack entry */ struct stack_entry { char *data; struct stack_entry *next; } /** * Type for stack instance */ struct stack_t { struct stack_entry *head; size_t stackSize; // not strictly necessary, but // useful for logging } /** * Create a new stack instance */ struct stack_t *newStack(void) { struct stack_t *stack = malloc(sizeof *stack); if (stack) { stack->head = NULL; stack->stackSize = 0; } return stack; } /** * Make a copy of the string to be stored (assumes * strdup() or similar functionality is not * available */ char *copyString(char *str) { char *tmp = malloc(strlen(str) + 1); if (tmp) strcpy(tmp, str); return tmp; } /** * Push a value onto the stack */ void push(struct stack_t *theStack, char *value) { struct stack_entry *entry = malloc(sizeof *entry); if (entry) { entry->data = copyString(value); entry->next = theStack->head; theStack->head = entry; theStack->stackSize++; } else { // handle error here } } /** * Get the value at the top of the stack */ char *top(struct stack_t *theStack) { if (theStack && theStack->head) return theStack->head->data; else return NULL; } /** * Pop the top element from the stack; this deletes both * the stack entry and the string it points to */ void pop(struct stack_t *theStack) { if (theStack->head != NULL) { struct stack_entry *tmp = theStack->head; theStack->head = theStack->head->next; free(tmp->data); free(tmp); theStack->stackSize--; } } /** * Clear all elements from the stack */ void clear (struct stack_t *theStack) { while (theStack->head != NULL) pop(theStack); } /** * Destroy a stack instance */ void destroyStack(struct stack_t **theStack) { clear(*theStack); free(*theStack); *theStack = NULL; } 

Edit

This would help to have an example of how to use it:

 int main(void) { struct stack_t *theStack = newStack(); char *data; push(theStack, "foo"); push(theStack, "bar"); ... data = top(theStack); pop(theStack); ... clear(theStack); destroyStack(&theStack); ... } 

You can declare stacks as automatic variables, rather than using newStack () and destroyStack (), you just need to make sure that they are loaded correctly, as in

 int main(void) { struct stack_t myStack = {NULL, 0}; push (&myStack, "this is a test"); push (&myStack, "this is another test"); ... clear(&myStack); } 

I'm just used to creating pseudo-constructors / destructors for everything.

+6
source

Try GNU Obstacks .

From Wikipedia:

In the C programming language, Obstack is a GNU extension for managing memory in the C standard library. "Obstack" is the "stack" of "objects" (data elements) that is dynamically managed.

Wikipedia sample code:

 char *x; void *(*funcp)(); x = (char *) obstack_alloc(obptr, size); /* Use the macro. */ x = (char *) (obstack_alloc) (obptr, size); /* Call the function. */ funcp = obstack_alloc; /* Take the address of the function. */ 

IMO, which makes special locks: It does not need malloc() and free() , but memory can still be allocated "dynamically". This is similar to alloca() on steroids. It is also available on many platforms, as it is part of the GNU C library. Especially on embedded systems, it may make more sense to use Obstacks instead of malloc() .

+3
source
+2
source

All Articles