Last active
December 4, 2019 19:27
-
-
Save thinkphp/b1cb6511680f313f28a88cf0ff33f545 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Author: Adrian Statescu <mergesortv@gmail.com>,<http://adrianstatescu.com> | |
* Description: Efficient Data Structure for storing and retrieving data in a Last-In, first-out; | |
* This allow us to retrieve data in opposite order as it was stored. | |
*/ | |
#include <stdio.h> | |
#define STACK_MAX 100 | |
#define FIN "scarface.in" | |
struct Stack { | |
int size; | |
char data[ STACK_MAX ]; | |
}; | |
typedef struct Stack Stack; | |
void Stack_Init(Stack *S) { | |
S->size = 0; | |
} | |
void Stack_Push(Stack *S, int val) { | |
if( S->size < STACK_MAX ) { | |
S->data[S->size++] = val; | |
} else fprintf(stderr, "Err: Stack Full!\n"); | |
} | |
void Stack_Pop(Stack *S) { | |
if( S->size == 0 ) fprintf(stderr, "Error: Stack Empty!"); | |
else S->size--; | |
} | |
int Stack_Top(Stack *S) { | |
if(S->size == 0) { | |
fprintf(stderr, "Error: Stack Empty!"); | |
return -1; | |
} | |
return S->data[ S->size - 1]; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
int i; | |
char *arr = "Likelihood"; | |
int n = sizeof(arr)/sizeof(arr[0]); | |
for(i = 0; i < n; ++i) printf("%c", arr[i]); | |
printf("\n"); | |
Stack S; | |
Stack_Init(&S); | |
for(i = 0; i < n; ++i) Stack_Push(&S, arr[i]); | |
for(i = 0; i < n; ++i) printf("%c", Stack_Top(&S)), Stack_Pop(&S); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment