Saturday 6 June 2015

Stack implementation

Stack implementation in C using Arrays.



As in one of my previous blog, I showed the Stack implementation in Java using arrays, this time I will demonstrate the same features using C as a programming language. This is a very basic implementation and nicely shows the use of pointers in the easiest way. 



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

struct ArrayStack {
    int top;
    int capacity;
    int *array;
};

struct ArrayStack *createStack(int capacity) {
    struct ArrayStack *stack = malloc(sizeof(struct ArrayStack));
    stack -> top = -1;
    stack -> capacity = capacity;
    stack -> array = malloc(stack -> capacity * sizeof(int));
    return stack;
}

int isStackEmpty(struct ArrayStack *stack) {
    return (stack -> top == -1);
}

int isStackFull(struct ArrayStack *stack) {
    return (stack -> top == stack -> capacity - 1);
}

void push(struct ArrayStack *stack, int data) {
    if (!isStackFull(stack)) {
        stack -> array[++stack -> top] = data;
    } else {
        printf("The stack is Full!\n");
    }
}

int pop(struct ArrayStack *stack) {
    if (!isStackEmpty(stack)) {
        return stack -> array[stack -> top--];
    } else {
        printf("The stack is Empty!\n");
        return 0;
    }
}

void deleteStack(struct ArrayStack *stack) {
    if(stack) {
        if (stack -> array) {
            free(stack -> array);
        }
        free(stack);
    }
}

void display(struct ArrayStack *stack) {
    int i = 0;
    if(!isStackEmpty(stack)) {
        printf("The Stack contents are: ");
        for(i = 0; i <= stack -> top; ++i) {
            printf("%d ", stack -> array[i]);
        }
        printf("\n");
    } else {
        printf("The Stack is empty!\n");
    }
}

int main(int argc, char const *argv[])
{
    int capacity, operation, num;
    char choice;
    printf("Enter the stack capacity: ");
    scanf("%d", &capacity);
    struct ArrayStack *stack = createStack(capacity);
   
    do {
        printf("Stack Operations: \n");
        printf("\t1. Push\n");
        printf("\t2. Pop\n");
        printf("\t3. Stack Overflow\n");
        printf("\t4. Stack Unverflow\n");
        printf("\t5. Display\n");
        printf("Enter the operation to be performed: ");
        scanf("%d", &operation);

        switch(operation) {
            case 1: printf("Enter the number to be pushed to the stack: ");
                    scanf("%d", &num);
                    push(stack, num);
                    break;

            case 2: printf("The element popped is: %d\n", pop(stack));
                    break;

            case 3: if (isStackFull(stack)) {
                        printf("Stack Overflow: True\n");
                        break;
                    } else {
                        printf("Stack Overflow: False\n");
                        break;
                    }

            case 4: if (isStackEmpty(stack)) {
                        printf("Stack Underflow: True\n");
                        break;
                    } else {
                        printf("Stack Overflow: False\n");
                        break;
                    }

            case 5: display(stack);
                    break;

            default: printf("InCorrect Input, please try again!\n");
                     break;
        }

        printf("Do you want to continue, 'y' / 'n'?: ");
        scanf(" %c", &choice);
    } while(choice == 'y');
    return 0;
}
 

Please feel free to comment and let us know if this could be simplified any further!

Tuesday 2 June 2015

Stacks implementation in Java

Stacks is a ADT in Java which follows 'Last In First Out' principle. This post is a very simple implementation of Stacks in Java.

 

import java.util.Scanner;
class Stack {
    int top;
    int capacity;
    int array[];

    public Stack(int capacity) {
        this.top = -1;
        this.capacity = capacity;
        array = new int[capacity];
    }

    boolean isStackFull() {
        return this.top == capacity - 1;
    }

    boolean isStackEmpty() {   
        return this.top == -1;
    }

    void push(int data) {
        if(!isStackFull()) {
            this.array[++this.top] = data;
        } else {
            System.out.println("The Stack is Full!");
        }
    }

    int pop() {
        if(!this.isStackEmpty()) {
            return this.array[this.top--];
        } else {
            System.out.println("The Stack is Empty!");
            return -1;
        }
    }

    void display() {
        if(this.isStackEmpty()) {
            System.out.println("The Stack is Empty!");
        } else {
            System.out.print("The elements in the stack are: ");
            for (int i = 0; i <= this.top; i++) {
                System.out.print(this.array[i] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the size of the stack: ");
        Stack stack = new Stack(scanner.nextInt());

        do {
            System.out.println("1: Push");
            System.out.println("2: Pop");
            System.out.println("3: Display");
            System.out.println("4: isStackEmpty");
            System.out.println("5: isStackFull");
            System.out.println("6: Quit");
            System.out.print("Enter the stack operations: ");   

            int choice = scanner.nextInt();

            switch(choice) {
                case 1: System.out.print("Enter the element to be pushed: ");
                        stack.push(scanner.nextInt());
                        break;

                case 2: System.out.println("The element popped is: " + stack.pop());
                        break;

                case 3: stack.display();
                        break;

                case 4: System.out.println("Stack Empty: " + stack.isStackEmpty());
                        break;

                case 5: System.out.println("Stack Full: " + stack.isStackFull());
                        break;

                case 6: break;

                default: System.out.println("Please enter a valid input!");
            }

            System.out.print("Do you want to continue, 'y' / 'n'?: ");
        } while(scanner.next().toLowerCase().charAt(0) == 'y');
    }
}


  • Space Complexity: O(n)
  • Time Complexity(for all the individual operations): O(1)
  • Limitations: Max size of the array must be defined and cannot be changed.
  • Solution: Dynamic Array Implementation

Please Let me know if this could be simplified any further and done more efficiently!