Created
March 9, 2018 22:26
-
-
Save OffS3c/aef27a77e4b6070d5a84e749ef04544a to your computer and use it in GitHub Desktop.
Basic Example Code for C Beginners - Introduces to some concepts which most people don't use as Beginners :P
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
// Was created/modified for Umair. Later made public for reference. Corrections are welcome. | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <inttypes.h> | |
#include <stdlib.h> | |
#define TRUE 1 | |
#define FALSE 0 | |
#define REPEAT | |
typedef struct Results | |
{ | |
int value ; | |
uint8_t ResultAvailable ; //for any error you encounter. example: stack not initialized as in that case there will be no result etc... | |
} Result; | |
struct ArrayStack // its allocated in heap not in stack dear Umair (malloc) | |
{ | |
int top; | |
int capacity; | |
int *array; | |
}; | |
struct ArrayStack*createStack(int cap) | |
{ | |
struct ArrayStack*stack; | |
stack=malloc(sizeof(struct ArrayStack)); | |
stack->capacity=cap; | |
stack->top=-1; | |
stack->array=malloc(sizeof(int)*stack->capacity); | |
return stack; | |
} | |
Result isFull(struct ArrayStack *stack) | |
{ | |
Result myQuery; | |
if(stack->top==stack->capacity-1) | |
{ | |
myQuery.value = TRUE; | |
myQuery.ResultAvailable = TRUE; | |
} | |
else | |
{ | |
myQuery.value = FALSE; | |
myQuery.ResultAvailable = TRUE; | |
} | |
return myQuery; | |
} | |
Result isEmpty(struct ArrayStack *stack) | |
{ | |
Result myQuery; | |
if(stack->top==-1) | |
{ | |
myQuery.value = TRUE; | |
myQuery.ResultAvailable = TRUE; | |
} | |
else | |
{ | |
myQuery.value = FALSE; | |
myQuery.ResultAvailable = TRUE; | |
} | |
return myQuery; | |
} | |
Result PUSH(struct ArrayStack*stack,int item) | |
{ | |
Result myQuery; | |
if(!isFull(stack).value) | |
{ | |
//stack->top++; | |
stack->array[++stack->top] = item; | |
myQuery.ResultAvailable = TRUE; | |
myQuery.value = item; | |
} | |
else | |
{ | |
myQuery.ResultAvailable = FALSE; | |
myQuery.value = 0; | |
} | |
return myQuery; | |
} | |
Result POP(struct ArrayStack*stack) | |
{ | |
int item; | |
Result myQuery; | |
if( !isEmpty(stack).value ) | |
{ | |
item = stack->array[stack->top--]; | |
//stack->top--; | |
//return item; | |
myQuery.ResultAvailable = TRUE; | |
myQuery.value = item; | |
} | |
else | |
{ | |
myQuery.ResultAvailable = FALSE; | |
myQuery.value = 0; | |
} | |
return myQuery; | |
} | |
int main() | |
{ | |
int choice,data,item; | |
Result res; | |
struct ArrayStack *stack; | |
stack=createStack(4); | |
#ifdef REPEAT | |
for (;;) | |
{ | |
#endif | |
printf("\n\n============= Select Option =============\n"); | |
printf("[ 1 ] Push\n"); | |
printf("[ 2 ] Pop\n"); | |
printf("[ 3 ] Exit\n"); | |
printf("\n[Q] Enter your choice: "); | |
scanf("%d",&choice); | |
switch(choice) | |
{ | |
case 1: | |
printf("[Q] Enter element: "); | |
scanf("%d",&data); | |
res = PUSH(stack,data); | |
if (res.ResultAvailable) | |
{ | |
printf("\n[i] Pushed element %d!\n\n", res.value); | |
} | |
else | |
{ | |
printf("\n[!] Something went wrong while pushing. Maybe stack is full.\n\n"); | |
} | |
break; | |
case 2: | |
res = POP(stack); | |
if(!res.ResultAvailable) | |
{ | |
printf("\n[!] Something went wrong while poping. Maybe stack is empty.\n\n"); | |
} | |
else | |
{ | |
printf("\n[i] Popped element %d!\n\n", res.value); | |
} | |
break; | |
case 3: | |
return EXIT_SUCCESS; | |
default: | |
printf("\n[!] Wrong Option\n\n"); | |
break; | |
} | |
#ifdef REPEAT | |
} | |
#endif | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment