Added stack files, with push().
This commit is contained in:
parent
be38f63822
commit
4bb10a54cc
45
lib/stack.c
Normal file
45
lib/stack.c
Normal file
@ -0,0 +1,45 @@
|
||||
#include <malloc.h>
|
||||
#include <stdbool.h>
|
||||
#include "card.h"
|
||||
#include "stack.h"
|
||||
|
||||
struct stack *initialize_stack() {
|
||||
struct stack *stack = NULL;
|
||||
|
||||
stack = malloc(sizeof(stack));
|
||||
|
||||
stack->card = NULL;
|
||||
stack->next = NULL;
|
||||
|
||||
return(stack);
|
||||
}
|
||||
|
||||
bool empty(struct stack *stack) {
|
||||
return(stack->card == NULL);
|
||||
}
|
||||
|
||||
int length(struct stack *stack) {
|
||||
int length = 0;
|
||||
|
||||
if (!empty(stack)) {
|
||||
length = 1;
|
||||
while (stack->next != NULL) {
|
||||
length++;
|
||||
}
|
||||
}
|
||||
|
||||
return(length);
|
||||
}
|
||||
|
||||
void push(struct stack *stack, struct card *card) {
|
||||
struct stack *new_stack = NULL;
|
||||
|
||||
if (empty(stack)) {
|
||||
stack->card = card;
|
||||
} else {
|
||||
new_stack = malloc(sizeof(new_stack));
|
||||
new_stack->card = card;
|
||||
new_stack->next = stack;
|
||||
stack = new_stack;
|
||||
}
|
||||
}
|
9
lib/stack.h
Normal file
9
lib/stack.h
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
|
||||
struct stack {
|
||||
struct card *card;
|
||||
struct stack *next;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user