w3resource

C Exercises: Find the top and kth element of a stack

C Stack: Exercise-16 with Solution

Write a C program to implement a stack and accept some numeric values. Find the top and kth element of the stack.

Sample Solution:

C Code:

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

#define MAX_SIZE 100

// Declare the stack structure
struct Stack {
    int arr[MAX_SIZE]; // Array to store elements
    int top; // Index of the top element
};

// Initialize the stack
void init_Stack(struct Stack *stack) {
    stack->top = -1; // Initialize top index to -1
}

// Check if stack is empty
int isEmpty(struct Stack *stack) { 
    return (stack->top == -1); // Returns 1 if the stack is empty, 0 otherwise
}

// Check if stack is full
int isFull(struct Stack *stack) {
    return (stack->top == MAX_SIZE - 1); // Returns 1 if the stack is full, 0 otherwise
}

// Push an element to the stack
void push(struct Stack *stack, int value) {
    if (isFull(stack)) {
        printf("Stack is underflow!\n"); // Display an error message if the stack is full
        return;
    }
    stack->arr[++stack->top] = value; // Increment top index and add element
}

// Pop an element from the stack
int pop(struct Stack *stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty!\n"); // Display an error message if the stack is empty
        return -1;
    }
    return stack->arr[stack->top--]; // Return top element and decrement top index
}

// Get the top element of the stack
int top(struct Stack *stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty\n"); // Display an error message if the stack is empty
        return -1;
    }
    return stack->arr[stack->top]; // Return the top element of the stack
}

// Get the kth element from the top of the stack
int get_Kth_element_from_top(struct Stack *stack, int k) {
    if (k < 1 || k > stack->top + 1) {
        printf("Invalid value of k\n"); // Display an error message if k is out of range
        return -1;
    }
    return stack->arr[stack->top - k + 1]; // Return the kth element from the top of the stack
}

// Function to print the elements of the stack
void print_stack(struct Stack* s) {
    if (isEmpty(s)) {
        printf("Stack is empty\n"); // Display a message if the stack is empty
        return;
    }
    for (int i = 0; i <= s->top; i++) {
        printf("%d ", s->arr[i]); // Print each element of the stack
    }
    printf("\n");
}

// Main function
int main() {
    struct Stack stack;
    init_Stack(&stack); // Initialize the stack
    // Push elements onto the stack
    push(&stack, 1);
    push(&stack, 2);
    push(&stack, 3);
    push(&stack, 4);
    push(&stack, 5);
    push(&stack, 6);
    printf("Elements of the stack:\n");
    print_stack(&stack); // Print the elements of the stack
    printf("Top element: %d\n", top(&stack)); // Print the top element of the stack
    printf("3rd element from top: %d\n", get_Kth_element_from_top(&stack, 3)); // Print the 3rd element from the top
    printf("Remove the topmost element from the stack:\n");
    pop(&stack); // Remove the top element from the stack
    printf("Elements of the stack:\n");
    print_stack(&stack); // Print the elements of the stack after pop operation
    printf("Top element: %d\n", top(&stack)); // Print the top element of the stack
    printf("4th element from top: %d\n", get_Kth_element_from_top(&stack, 4)); // Print the 4th element from the top
    return 0;
}

Sample Output:

Elements of the stack:
1 2 3 4 5 6
Top element: 6
3rd element from top: 4
Remove the topmost element from the stack:
Elements of the stack:
1 2 3 4 5
Top element: 5
4th element from top: 2 

Flowchart:

Flowchart: Find the top and kth element of a stack.
Flowchart: Find the top and kth element of a stack.
Flowchart: Find the top and kth element of a stack.

C Programming Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Remove the minimum value from a stack.
Next: Decimal number to its binary equivalent using stack.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming-exercises/stack/c-stack-exercise-16.php