অ্যারে এবং লিঙ্কড লিস্ট ব্যবহার করে Stack ইমপ্লিমেন্টেশন

স্ট্যাক (Stack in C) - সি দিয়ে ডেটা স্ট্রাকচার (DSA using C) - Computer Programming

407

Stack হল একটি ডেটা স্ট্রাকচার যা LIFO (Last In, First Out) পদ্ধতির উপর ভিত্তি করে কাজ করে। এই ধরনের ডেটা স্ট্রাকচারকে অ্যারে এবং লিঙ্কড লিস্ট ব্যবহার করে বাস্তবায়ন করা যেতে পারে। নিচে উভয় পদ্ধতির বিস্তারিত আলোচনা ও উদাহরণ দেওয়া হলো।


১. অ্যারে ব্যবহার করে Stack ইমপ্লিমেন্টেশন

১.১ স্ট্যাকের গঠন

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

#define MAX 100 // Stack এর সর্বাধিক সাইজ

struct Stack {
    int items[MAX]; // Stack উপাদানগুলির জন্য অ্যারে
    int top;        // Stack এর শীর্ষের ইনডেক্স
};

// Stack তৈরি এবং ইনিশিয়ালাইজেশন
void initStack(struct Stack* s) {
    s->top = -1; // Stack খালি
}

১.২ স্ট্যাকে উপাদান যোগ করা (Push)

void push(struct Stack* s, int value) {
    if (s->top == MAX - 1) {
        printf("Stack Overflow! Cannot push %d\n", value);
    } else {
        s->items[++(s->top)] = value; // নতুন উপাদান যোগ করা
        printf("%d pushed to stack\n", value);
    }
}

১.৩ স্ট্যাক থেকে উপাদান অপসারণ করা (Pop)

int pop(struct Stack* s) {
    if (s->top == -1) {
        printf("Stack Underflow! Cannot pop\n");
        return -1; // ত্রুটি নির্দেশ
    } else {
        return s->items[(s->top)--]; // শীর্ষ থেকে উপাদান অপসারণ করা
    }
}

১.৪ শীর্ষ উপাদান দেখা (Peek)

int peek(struct Stack* s) {
    if (s->top == -1) {
        printf("Stack is empty! No top element\n");
        return -1; // ত্রুটি নির্দেশ
    } else {
        return s->items[s->top]; // শীর্ষ উপাদান দেখানো
    }
}

১.৫ পুরো প্রোগ্রাম উদাহরণ

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

#define MAX 100 // Stack এর সর্বাধিক সাইজ

struct Stack {
    int items[MAX]; // Stack উপাদানগুলির জন্য অ্যারে
    int top;        // Stack এর শীর্ষের ইনডেক্স
};

// Stack তৈরি এবং ইনিশিয়ালাইজেশন
void initStack(struct Stack* s) {
    s->top = -1; // Stack খালি
}

// Push অপারেশন
void push(struct Stack* s, int value) {
    if (s->top == MAX - 1) {
        printf("Stack Overflow! Cannot push %d\n", value);
    } else {
        s->items[++(s->top)] = value; // নতুন উপাদান যোগ করা
        printf("%d pushed to stack\n", value);
    }
}

// Pop অপারেশন
int pop(struct Stack* s) {
    if (s->top == -1) {
        printf("Stack Underflow! Cannot pop\n");
        return -1; // ত্রুটি নির্দেশ
    } else {
        return s->items[(s->top)--]; // শীর্ষ থেকে উপাদান অপসারণ করা
    }
}

// Peek অপারেশন
int peek(struct Stack* s) {
    if (s->top == -1) {
        printf("Stack is empty! No top element\n");
        return -1; // ত্রুটি নির্দেশ
    } else {
        return s->items[s->top]; // শীর্ষ উপাদান দেখানো
    }
}

// Main ফাংশন
int main() {
    struct Stack stack; // Stack তৈরি
    initStack(&stack);  // Stack ইনিশিয়ালাইজেশন

    push(&stack, 10);   // Stack এ উপাদান যুক্ত করা
    push(&stack, 20);
    push(&stack, 30);

    printf("Top element is %d\n", peek(&stack)); // Peek করা

    printf("%d popped from stack\n", pop(&stack)); // Pop অপারেশন
    printf("Top element is now %d\n", peek(&stack)); // Peek করা

    return 0;
}

২. লিঙ্কড লিস্ট ব্যবহার করে Stack ইমপ্লিমেন্টেশন

২.১ স্ট্যাকের গঠন

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

// Node structure
struct Node {
    int data;              // Data part
    struct Node* next;     // Pointer to the next node
};

// Stack structure
struct Stack {
    struct Node* top;      // Pointer to the top node
};

২.২ স্ট্যাক তৈরি এবং ইনিশিয়ালাইজেশন

void initStack(struct Stack* s) {
    s->top = NULL; // Stack খালি
}

২.৩ স্ট্যাকে উপাদান যোগ করা (Push)

void push(struct Stack* s, int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Create new node
    newNode->data = value;   // Assign data
    newNode->next = s->top;  // Point new node to old top
    s->top = newNode;        // Move top to new node
    printf("%d pushed to stack\n", value);
}

২.৪ স্ট্যাক থেকে উপাদান অপসারণ করা (Pop)

int pop(struct Stack* s) {
    if (s->top == NULL) {
        printf("Stack Underflow! Cannot pop\n");
        return -1; // ত্রুটি নির্দেশ
    } else {
        struct Node* temp = s->top; // Store current top
        int poppedValue = temp->data; // Get data from top
        s->top = s->top->next; // Move top to next node
        free(temp); // Free the old top
        return poppedValue; // Return the popped value
    }
}

২.৫ শীর্ষ উপাদান দেখা (Peek)

int peek(struct Stack* s) {
    if (s->top == NULL) {
        printf("Stack is empty! No top element\n");
        return -1; // ত্রুটি নির্দেশ
    } else {
        return s->top->data; // Return the top value
    }
}

২.৬ পুরো প্রোগ্রাম উদাহরণ

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

// Node structure
struct Node {
    int data;              // Data part
    struct Node* next;     // Pointer to the next node
};

// Stack structure
struct Stack {
    struct Node* top;      // Pointer to the top node
};

// Function to initialize the stack
void initStack(struct Stack* s) {
    s->top = NULL; // Stack খালি
}

// Function to push an element onto the stack
void push(struct Stack* s, int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Create new node
    newNode->data = value;   // Assign data
    newNode->next = s->top;  // Point new node to old top
    s->top = newNode;        // Move top to new node
    printf("%d pushed to stack\n", value);
}

// Function to pop an element from the stack
int pop(struct Stack* s) {
    if (s->top == NULL) {
        printf("Stack Underflow! Cannot pop\n");
        return -1; // ত্রুটি নির্দেশ
    } else {
        struct Node* temp = s->top; // Store current top
        int poppedValue = temp->data; // Get data from top
        s->top = s->top->next; // Move top to next node
        free(temp); // Free the old top
        return poppedValue; // Return the popped value
    }
}

// Function to peek at the top element of the stack
int peek(struct Stack* s) {
    if (s->top == NULL) {
        printf("Stack is empty! No top element\n");
        return -1; // ত্রুটি নির্দেশ
    } else {
        return s->top->data; // Return the top value
    }
}

// Main function to demonstrate stack operations
int main() {
    struct Stack stack; // Create a stack
    initStack(&stack);  // Initialize the stack

    push(&stack, 10);   // Push elements onto the stack
    push(&stack, 20);
    push(&stack, 30);

    printf("Top element is %d\n", peek(&stack)); // Peek at top element

    printf("%d popped from stack\n", pop(&stack)); // Pop element
    printf("Top element is now %d\n", peek(&stack)); // Peek at top element

    return 0;
}
Content added By
Promotion

Are you sure to start over?

Loading...