mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 23:09:36 +03:00
Fix indenting
This commit is contained in:
parent
5160b2f8ee
commit
ab3173f581
@ -5,18 +5,18 @@
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Stack_T stk;
|
Stack_T stk;
|
||||||
stk = Stack_init();
|
stk = Stack_init();
|
||||||
Stack_push(stk, (int *) 1);
|
Stack_push(stk, (int *) 1);
|
||||||
Stack_push(stk, (int *) 2);
|
Stack_push(stk, (int *) 2);
|
||||||
Stack_push(stk, (int *) 3);
|
Stack_push(stk, (int *) 3);
|
||||||
Stack_push(stk, (int *) 4);
|
Stack_push(stk, (int *) 4);
|
||||||
printf("Size: %d\n", Stack_size(stk));
|
printf("Size: %d\n", Stack_size(stk));
|
||||||
Stack_print(stk);
|
Stack_print(stk);
|
||||||
Stack_pop(stk);
|
Stack_pop(stk);
|
||||||
printf("Stack after popping: \n");
|
printf("Stack after popping: \n");
|
||||||
Stack_print(stk);
|
Stack_print(stk);
|
||||||
Stack_pop(stk);
|
Stack_pop(stk);
|
||||||
printf("Stack after popping: \n");
|
printf("Stack after popping: \n");
|
||||||
Stack_print(stk);
|
Stack_print(stk);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -8,60 +8,60 @@
|
|||||||
|
|
||||||
typedef struct elem {
|
typedef struct elem {
|
||||||
void *val;
|
void *val;
|
||||||
struct elem *next;
|
struct elem *next;
|
||||||
} elem_t;
|
} elem_t;
|
||||||
|
|
||||||
struct T {
|
struct T {
|
||||||
int count;
|
int count;
|
||||||
elem_t *head;
|
elem_t *head;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Initial stack */
|
/* Initial stack */
|
||||||
T Stack_init (void) {
|
T Stack_init (void) {
|
||||||
T stack;
|
T stack;
|
||||||
stack = (T) malloc(sizeof(T));
|
stack = (T) malloc(sizeof(T));
|
||||||
stack->count = 0;
|
stack->count = 0;
|
||||||
stack->head = NULL;
|
stack->head = NULL;
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check empty stack*/
|
/* Check empty stack*/
|
||||||
int Stack_empty(T stack) {
|
int Stack_empty(T stack) {
|
||||||
assert(stack);
|
assert(stack);
|
||||||
return stack->count == 0;
|
return stack->count == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return size of the stack */
|
/* Return size of the stack */
|
||||||
int Stack_size(T stack) {
|
int Stack_size(T stack) {
|
||||||
assert(stack);
|
assert(stack);
|
||||||
return stack->count;
|
return stack->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Push an element into the stack */
|
/* Push an element into the stack */
|
||||||
void Stack_push(T stack, void *val) {
|
void Stack_push(T stack, void *val) {
|
||||||
elem_t *t;
|
elem_t *t;
|
||||||
|
|
||||||
assert(stack);
|
assert(stack);
|
||||||
t = (elem_t *) malloc(sizeof(elem_t));
|
t = (elem_t *) malloc(sizeof(elem_t));
|
||||||
t->val = val;
|
t->val = val;
|
||||||
t->next = stack->head;
|
t->next = stack->head;
|
||||||
stack->head = t;
|
stack->head = t;
|
||||||
stack->count++;
|
stack->count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pop an element out of the stack */
|
/* Pop an element out of the stack */
|
||||||
void *Stack_pop(T stack) {
|
void *Stack_pop(T stack) {
|
||||||
void *val;
|
void *val;
|
||||||
elem_t *t;
|
elem_t *t;
|
||||||
|
|
||||||
assert(stack);
|
assert(stack);
|
||||||
assert(stack->count > 0);
|
assert(stack->count > 0);
|
||||||
t = stack->head;
|
t = stack->head;
|
||||||
stack->head = t->next;
|
stack->head = t->next;
|
||||||
stack->count--;
|
stack->count--;
|
||||||
val = t->val;
|
val = t->val;
|
||||||
free(t);
|
free(t);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Print all elements in the stack */
|
/* Print all elements in the stack */
|
||||||
@ -69,11 +69,11 @@ void Stack_print(Stack_T stack) {
|
|||||||
assert(stack);
|
assert(stack);
|
||||||
|
|
||||||
int i, size = Stack_size(stack);
|
int i, size = Stack_size(stack);
|
||||||
elem_t *current_elem = stack->head;
|
elem_t *current_elem = stack->head;
|
||||||
printf("Stack [Top --- Bottom]: ");
|
printf("Stack [Top --- Bottom]: ");
|
||||||
for(i = 0; i < size; ++i) {
|
for(i = 0; i < size; ++i) {
|
||||||
printf("%p ", (int *)current_elem->val);
|
printf("%p ", (int *)current_elem->val);
|
||||||
current_elem = current_elem->next;
|
current_elem = current_elem->next;
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user