Saturday, 28 September 2024

Write a function for Implementation of stack using C

Method 1

#include <stdio.h>

//#include <conio.h>


#define MAX 100  // Define the maximum size of the stack


// Stack variables

int top = -1;

int stack[MAX];


// Function to add an element to the stack

void push(int value) {

    if (top == MAX - 1) {

        printf("Stack overflow! Cannot push %d\n", value);

    } else {

        stack[++top] = value;

        printf("%d pushed to stack\n", value);

    }

}


// Function to remove an element from the stack

void pop() {

    if (top == -1) {

        printf("Stack underflow! Cannot pop\n");

    } else {

        printf("%d popped from stack\n", stack[top--]);

    }

}


// Function to get the top element of the stack

void peek() {

    if (top == -1) {

        printf("Stack is empty!\n");

    } else {

        printf("Top element is %d\n", stack[top]);

    }

}


// Function to check if the stack is empty

int isEmpty() {

    return top == -1;

}


// Function to check if the stack is full

int isFull() {

    return top == MAX - 1;

}


int main() {

    // Perform stack operations

    push(10);

    push(20);

    push(30);


    peek();  // Peek at the top element


    pop();   // Remove the top element

    pop();


    peek();  // Peek at the new top element


    // Check if the stack is empty or full

    if (isEmpty()) {

        printf("Stack is empty\n");

    } else {

        printf("Stack is not empty\n");

    }


    if (isFull()) {

        printf("Stack is full\n");

    } else {

        printf("Stack is not full\n");

    }


   //  return 0;

}



Method2

#include <stdio.h>

#include <conio.h>


#define MAX 100  // Define the maximum size of the stack


// Stack structure definition

struct Stack {

    int top;

    int arr[MAX];

};


// Function to initialize the stack

void initialize(struct Stack* stack) {

    stack->top = -1;  // Initialize top of the stack

}


// Function to add an element to the stack

void push(struct Stack* stack, int value) {

    if (stack->top == MAX - 1) {

        printf("Stack overflow! Cannot push %d\n", value);

    } else {

        stack->arr[++(stack->top)] = value;

        printf("%d pushed to stack\n", value);

    }

}


// Function to remove an element from the stack

void pop(struct Stack* stack) {

    if (stack->top == -1) {

        printf("Stack underflow! Cannot pop\n");

    } else {

        printf("%d popped from stack\n", stack->arr[(stack->top)--]);

    }

}


// Function to get the top element of the stack

void peek(struct Stack* stack) {

    if (stack->top == -1) {

        printf("Stack is empty!\n");

    } else {

        printf("Top element is %d\n", stack->arr[stack->top]);

    }

}


// Function to check if the stack is empty

int isEmpty(struct Stack* stack) {

    return stack->top == -1;

}


// Function to check if the stack is full

int isFull(struct Stack* stack) {

    return stack->top == MAX - 1;

}


int main() {

    struct Stack stack;  // Create a stack object

    initialize(&stack);  // Initialize the stack


    // Perform stack operations

    push(&stack, 10);

    push(&stack, 20);

    push(&stack, 30);


    peek(&stack);  // Peek at the top element


    pop(&stack);   // Remove the top element

    pop(&stack);


    peek(&stack);  // Peek at the new top element


    // Check if the stack is empty or full

    if (isEmpty(&stack)) {

        printf("Stack is empty\n");

    } else {

        printf("Stack is not empty\n");

    }


    if (isFull(&stack)) {

        printf("Stack is full\n");

    } else {

        printf("Stack is not full\n");

    }


    getch();

    return 0;

}