Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 6dac95c

Browse files
committed
Merge pull request #50 from r-deleon/master
Stack solution in C
2 parents 8f6f69e + ed5682a commit 6dac95c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

stack/stack.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdlib.h>
2+
#include "stack.h"
3+
4+
5+
void _add(Stack *head,int value){
6+
Stack node = (Stack)malloc(sizeof(Stack));
7+
if(node !=NULL){
8+
node->value = value;
9+
node->next = *head;
10+
*head = node;
11+
}
12+
}
13+
14+
int _remove(Stack *head){
15+
int value = -1;
16+
if(head!=NULL){
17+
Stack top = *head;
18+
value = (*head)->value;
19+
*head = (*head)->next;
20+
free(top);
21+
22+
}
23+
return value;
24+
}

stack/stack.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef STACK_H_INCLUDED
2+
#define STACK_H_INCLUDED
3+
4+
typedef struct Nodestack {
5+
int value;
6+
struct Nodestack *next;
7+
}NodeStack;
8+
9+
typedef NodeStack *Stack;
10+
11+
// add the element onto the stack
12+
void _add(Stack*,int);
13+
14+
//returns the element on top of the stack
15+
int _remove(Stack*);
16+
17+
18+
#endif // STACK_H_INCLUDED

0 commit comments

Comments
 (0)