mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-09-24 16:05:02 +00:00
Fix filenames for DIRECTORY.md
This commit is contained in:
12
data_structures/stack/stack_linked_list/Makefile
Normal file
12
data_structures/stack/stack_linked_list/Makefile
Normal file
@@ -0,0 +1,12 @@
|
||||
CC = gcc
|
||||
CFLAGS = -c -Wall
|
||||
|
||||
all: main
|
||||
main: main.o stack.o
|
||||
$(CC) main.o stack.o -o main
|
||||
|
||||
stack.o: stack.c
|
||||
$(CC) $(CFLAGS) stack.c
|
||||
|
||||
clean:
|
||||
rm *o main
|
||||
22
data_structures/stack/stack_linked_list/main.c
Normal file
22
data_structures/stack/stack_linked_list/main.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include "stack.h"
|
||||
|
||||
int main() {
|
||||
Stack_T stk;
|
||||
stk = Stack_init();
|
||||
Stack_push(stk, (int *) 1);
|
||||
Stack_push(stk, (int *) 2);
|
||||
Stack_push(stk, (int *) 3);
|
||||
Stack_push(stk, (int *) 4);
|
||||
printf("Size: %d\n", Stack_size(stk));
|
||||
Stack_print(stk);
|
||||
Stack_pop(stk);
|
||||
printf("Stack after popping: \n");
|
||||
Stack_print(stk);
|
||||
Stack_pop(stk);
|
||||
printf("Stack after popping: \n");
|
||||
Stack_print(stk);
|
||||
return 0;
|
||||
}
|
||||
79
data_structures/stack/stack_linked_list/stack.c
Normal file
79
data_structures/stack/stack_linked_list/stack.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "stack.h"
|
||||
|
||||
#define T Stack_T
|
||||
|
||||
typedef struct elem {
|
||||
void *val;
|
||||
struct elem *next;
|
||||
} elem_t;
|
||||
|
||||
struct T {
|
||||
int count;
|
||||
elem_t *head;
|
||||
};
|
||||
|
||||
/* Initial stack */
|
||||
T Stack_init (void) {
|
||||
T stack;
|
||||
stack = (T) malloc(sizeof(T));
|
||||
stack->count = 0;
|
||||
stack->head = NULL;
|
||||
return stack;
|
||||
}
|
||||
|
||||
/* Check empty stack*/
|
||||
int Stack_empty(T stack) {
|
||||
assert(stack);
|
||||
return stack->count == 0;
|
||||
}
|
||||
|
||||
/* Return size of the stack */
|
||||
int Stack_size(T stack) {
|
||||
assert(stack);
|
||||
return stack->count;
|
||||
}
|
||||
|
||||
/* Push an element into the stack */
|
||||
void Stack_push(T stack, void *val) {
|
||||
elem_t *t;
|
||||
|
||||
assert(stack);
|
||||
t = (elem_t *) malloc(sizeof(elem_t));
|
||||
t->val = val;
|
||||
t->next = stack->head;
|
||||
stack->head = t;
|
||||
stack->count++;
|
||||
}
|
||||
|
||||
/* Pop an element out of the stack */
|
||||
void *Stack_pop(T stack) {
|
||||
void *val;
|
||||
elem_t *t;
|
||||
|
||||
assert(stack);
|
||||
assert(stack->count > 0);
|
||||
t = stack->head;
|
||||
stack->head = t->next;
|
||||
stack->count--;
|
||||
val = t->val;
|
||||
free(t);
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Print all elements in the stack */
|
||||
void Stack_print(Stack_T stack) {
|
||||
assert(stack);
|
||||
|
||||
int i, size = Stack_size(stack);
|
||||
elem_t *current_elem = stack->head;
|
||||
printf("Stack [Top --- Bottom]: ");
|
||||
for(i = 0; i < size; ++i) {
|
||||
printf("%p ", (int *)current_elem->val);
|
||||
current_elem = current_elem->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
15
data_structures/stack/stack_linked_list/stack.h
Normal file
15
data_structures/stack/stack_linked_list/stack.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef __STACK__
|
||||
#define __STACK__
|
||||
|
||||
#define T Stack_T
|
||||
typedef struct T *T;
|
||||
|
||||
extern T Stack_init (void);
|
||||
extern int Stack_size (T stack);
|
||||
extern int Stack_empty (T stack);
|
||||
extern void Stack_push (T stack, void *val);
|
||||
extern void *Stack_pop (T stack);
|
||||
extern void Stack_print (T stack);
|
||||
|
||||
#undef T
|
||||
#endif
|
||||
Reference in New Issue
Block a user