Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions C Programming Language/Stack/Stack-using-Array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// C program to implement Stack using array //
// Written by: @ExpressHermes

#include <stdio.h>
#include <stdlib.h>
#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;
}


79 changes: 79 additions & 0 deletions C Programming Language/Stack/Stack-using-Structure.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// C program to implement Stack using structure //
// Written by: @ExpressHermes

#include <stdio.h>
#include <stdlib.h>

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;
}