diff --git a/C Programming Language/Stack/Stack-using-Array.c b/C Programming Language/Stack/Stack-using-Array.c new file mode 100644 index 0000000..50bb140 --- /dev/null +++ b/C Programming Language/Stack/Stack-using-Array.c @@ -0,0 +1,66 @@ +// C program to implement Stack using array // +// Written by: @ExpressHermes + +#include +#include +#define SIZE 100 + +int stack[SIZE]; +int top = -1; + +void push(int value) { + if(top == SIZE -1) + printf("\nOverflow. Stack is full\n"); + else { + top++; + stack[top] = value; + printf("\nInsertion was successful\n"); + } +} + +void pop() { + if(top == -1) + printf("\nUnderflow. Stack is empty\n"); + else { + printf("\nPopped Value: %d\n", stack[top]); + top--; + } +} + +void display() { + if(top == -1) + printf("\nStack is empty \n"); + else { + printf("Stack elements are: "); + for(int i = top; i >= 0; i--) + printf("%d\t", stack[i]); + printf("\n"); + } +} + +int main(void) { + int choice, value; + printf("\n************* STACK IMPLEMENTATION USING STRUCURES *************\n"); + + while(1) { + printf("1. Push\n2. Pop\n3. Display\n4. Exit\n"); + printf("Enter your choice: "); + scanf("%d",&choice); + + switch(choice) { + case 1: printf("Enter value to be pushed: "); + scanf("%d", &value); + push(value); + break; + case 2: pop(); + break; + case 3: display(); + break; + case 4: exit(0); + default: printf("\nWrong choice. Try again.\n"); + } + } + return 0; +} + + diff --git a/C Programming Language/Stack/Stack-using-Structure.c b/C Programming Language/Stack/Stack-using-Structure.c new file mode 100644 index 0000000..00c194c --- /dev/null +++ b/C Programming Language/Stack/Stack-using-Structure.c @@ -0,0 +1,79 @@ +// C program to implement Stack using structure // +// Written by: @ExpressHermes + +#include +#include + +struct Node { + int data; + struct Node* next; +}; + +struct Node* top = NULL; + +void push(int value) { + + struct Node *newNode; + newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode -> data = value; + + if(top == NULL) + newNode -> next = NULL; + else + newNode -> next = top; + top = newNode; + + printf("\nInsertion was successful\n"); +} + +void pop() { + if(top == NULL) + printf("\nUnderflow. Stack is empty.\n"); + else { + struct Node* temp = top; + printf("Popped Value:\t%d\n", temp -> data); + top = temp -> next; + free(temp); + } +} + +void display() { + if(top == NULL) + printf("\nUnderflow. Stack is empty.\n"); + else { + struct Node* temp = top; + printf("Stack elements are: "); + while(temp != NULL) { + printf("%d\t", temp -> data); + temp = temp -> next; + } + printf("\n"); + } +} + +int main(void) { + int choice, value; + printf("\n************* STACK IMPLEMENTATION USING STRUCURES *************\n"); + + while(1) { + printf("1. Push\n2. Pop\n3. Display\n4. Exit\n"); + printf("Enter your choice: "); + scanf("%d",&choice); + + switch(choice) { + case 1: printf("Enter value to be pushed: "); + scanf("%d", &value); + push(value); + break; + case 2: pop(); + break; + case 3: display(); + break; + case 4: exit(0); + default: printf("\nWrong choice. Try again.\n"); + } + } + return 0; +} + +