mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-07-30 12:52:57 +00:00
formatting source-code for b388e4a309
This commit is contained in:
@@ -1,48 +1,54 @@
|
||||
#include "stack.h"
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "stack.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#define T Stack_T
|
||||
|
||||
typedef struct elem {
|
||||
typedef struct elem
|
||||
{
|
||||
void *val;
|
||||
struct elem *next;
|
||||
} elem_t;
|
||||
|
||||
struct T {
|
||||
struct T
|
||||
{
|
||||
int count;
|
||||
elem_t *head;
|
||||
};
|
||||
|
||||
/* Initial stack */
|
||||
T Stack_init (void) {
|
||||
T Stack_init(void)
|
||||
{
|
||||
T stack;
|
||||
stack = (T) malloc(sizeof(T));
|
||||
stack = (T)malloc(sizeof(T));
|
||||
stack->count = 0;
|
||||
stack->head = NULL;
|
||||
return stack;
|
||||
}
|
||||
|
||||
/* Check empty stack*/
|
||||
int Stack_empty(T stack) {
|
||||
int Stack_empty(T stack)
|
||||
{
|
||||
assert(stack);
|
||||
return stack->count == 0;
|
||||
}
|
||||
|
||||
/* Return size of the stack */
|
||||
int Stack_size(T stack) {
|
||||
int Stack_size(T stack)
|
||||
{
|
||||
assert(stack);
|
||||
return stack->count;
|
||||
}
|
||||
|
||||
/* Push an element into the stack */
|
||||
void Stack_push(T stack, void *val) {
|
||||
void Stack_push(T stack, void *val)
|
||||
{
|
||||
elem_t *t;
|
||||
|
||||
assert(stack);
|
||||
t = (elem_t *) malloc(sizeof(elem_t));
|
||||
t = (elem_t *)malloc(sizeof(elem_t));
|
||||
t->val = val;
|
||||
t->next = stack->head;
|
||||
stack->head = t;
|
||||
@@ -50,7 +56,8 @@ void Stack_push(T stack, void *val) {
|
||||
}
|
||||
|
||||
/* Pop an element out of the stack */
|
||||
void *Stack_pop(T stack) {
|
||||
void *Stack_pop(T stack)
|
||||
{
|
||||
void *val;
|
||||
elem_t *t;
|
||||
|
||||
@@ -65,13 +72,15 @@ void *Stack_pop(T stack) {
|
||||
}
|
||||
|
||||
/* Print all elements in the stack */
|
||||
void Stack_print(Stack_T 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) {
|
||||
for (i = 0; i < size; ++i)
|
||||
{
|
||||
printf("%p ", (int *)current_elem->val);
|
||||
current_elem = current_elem->next;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user