I'm relatively new to C as a language, but I wouldn't call myself a beginner. That being said, I don't believe my C "coding style," so to speak, is particularly developed. Also, I'm new to error handling in C as well.
stack.c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node* next;
} node_t;
typedef struct stack {
struct node* head;
} stack_t;
stack_t* create_list(void)
{
stack_t* stack = malloc(sizeof *stack);
if (stack != NULL) {
stack->head = NULL;
}
return stack;
}
void push(stack_t* list, int val)
{
node_t* newnode = malloc(sizeof *newnode);
if (newnode != NULL) {
newnode->val = val;
newnode->next = list->head;
}
list->head = newnode;
}
int pop(stack_t* list)
{
int val = list->head->val;
node_t* next = list->head->next;
free(list->head);
list->head = next;
return val;
}
void destroy_list(stack_t* list)
{
do {
node_t* head = list->head;
node_t* next = head->next;
free(head);
list->head = next;
} while (list->head);
free(list);
}
void foreach(stack_t* list, int (*func)(const int))
{
node_t* curr = list->head;
do {
curr->val = func(curr->val);
curr = curr->next;
} while(curr);
}
int print_val(const int val)
{
printf("value: %d\n", val);
return val;
}
int main(int argc, char* argv[])
{
printf("creating list\n");
stack_t* list = create_list();
printf("pushing value 2\n");
push(list, 2);
printf("top value: %d\n", list->head->val);
printf("pushing value 5\n");
push(list, 5);
printf("top value: %d\n", list->head->val);
printf("iterating top-down\n");
foreach(list, print_val);
printf("popping top value\n");
int popped = pop(list);
printf("popped: %d\n", popped);
printf("top value: %d\n", list->head->val);
destroy_list(list);
return 0;
}
queue.c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node* next;
} node_t;
typedef struct {
node_t *head, *tail;
} queue_t;
queue_t* create_list(void)
{
queue_t* queue = malloc(sizeof *queue);
if (queue != NULL) {
queue->head = queue->tail = NULL;
}
return queue;
}
void enqueue(queue_t* queue, int val)
{
node_t* newnode = malloc(sizeof *newnode);
if (newnode != NULL) {
newnode->val = val;
newnode->next = NULL;
}
if (queue->tail) {
queue->tail->next = newnode;
} else {
queue->head = newnode;
}
queue->tail = newnode;
}
int dequeue(queue_t* queue)
{
int val = queue->head->val;
node_t* next = queue->head->next;
free(queue->head);
queue->head = next;
return val;
}
void destroy_list(queue_t* queue)
{
do {
node_t* head = queue->head;
node_t* next = head->next;
free(head);
queue->head = next;
} while (queue->head);
queue->head = queue->tail = NULL;
free(queue);
}
void foreach(queue_t* list, int (*func)(const int))
{
node_t* curr = list->head;
do {
curr->val = func(curr->val);
curr = curr->next;
} while(curr);
}
int print_val(const int val)
{
printf("value: %d\n", val);
return val;
}
int main(int argc, char* argv[])
{
printf("creating list\n");
queue_t* list = create_list();
printf("enqueuing value 2\n");
enqueue(list, 2);
printf("top value: %d\n", list->head->val);
printf("enqueueing value 5\n");
enqueue(list, 5);
printf("top value: %d\n", list->head->val);
printf("iterating top-down\n");
foreach(list, print_val);
printf("dequeueing top value\n");
int dequeued = dequeue(list);
printf("dequeued: %d\n", dequeued);
printf("top value: %d\n", list->head->val);
destroy_list(list);
return 0;
}