0

I have recently had a lesson in data structure and the first thing that was taught was how to implement a stack in an array. I could understand how the stack implementation was done but I don't understand the reason why it is done. I mean are't arrays more useful and practical and flexible compared to stacks. I can't really thing of any advantage of stack compared to arrays which lead to the need of building a stack out of an array. Can you help me clear my ideas about these concept?

1
  • "array" can have several meanings in programming. If you're talking about a specific language, can you clarify that much at least? I assume C or C++? Commented Mar 3, 2015 at 0:02

4 Answers 4

1

If we want to understand why a programming abstraction exist, the answer does not lie in the explaining its implementation details (as both Johns tried in their answers.). You need to look for real world analogies after which we have modelled something. There are so many. Stacks are all around us.

  • coin holder in a car
  • plates in ikea trolley
  • 9 mm gun
  • stacked goods in stores all around the world (thats probably the most prevalent stack in human world)
  • etc..

You are talking about an advantage of stack over array. Its advantage is that is fit for purpose when you business logic models a real world problem which is a stack.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you're confusing the basic advantages of a stack and an array. A stack can be implemented via an array, but at the most basic level, an array is a tool, and a stack is a way in which you can use that tool to your advantage.

In other words, an array may be used to implement a stack, but think of an array as a tool to store data. You can use an array to implement a heap, a stack, or even a queue – it's all about how you use the array to accomplish a particular task.

What's important is that you recognize when you need to use a stack, heap, queue, etc. and likewise, what "tools" (array, linked-list, etc.) you have at your disposal to implement such data structures.

Comments

0

Conceptually, you use Push and Pop operations on a Stack: You push onto the stack and pop off the stack. The stack is keeping track of the order that elements arrive, the last one in is the first one out (LIFO). You only work with the element at the top of the stack, either pop the top item off the stack or push a new element on top of it. If one component pushes 4 things onto the stack, another component can pop them off the stack and knows the order that was used by the 'pushing' component.

You typically use an array by either iterating through each member of the list, or, you access an element anywhere in the array by an index. Usually you use an array when you really don't care about the LIFO order, you just want to access any item for some other purpose, or, you want to perform an operation on each element and don't care about the LIFO order.

Comments

0

I can provide you java version of the same : /** * */ package com.base.stack;

/** * @author kamals1986 * The data that goes in the stack. */ public class Item {

int value;

public Item( int value) {
    this.value = value;
}

@Override
public String toString() {
    return "Item [value=" + value + "]";
}

}

Actual class now :

package com.base.stack;

import java.util.Arrays;

/** * @author kamals1986 * */ public class MyStack {

private static int INITIAL_SIZE = 5;
protected Item[] items = new Item[INITIAL_SIZE];
int top = -1;

@Override
public String toString() {
    return "MyStack [items=" + Arrays.toString(items) + "]\n";
}

public void push(Item i) {
    if (top == -1 || stackNotFull()) {
        System.out.println("Not resizing");
        top = top + 1;
        items[top] = i;
    } else {
        System.out.println("Stack is full , I am resizing it by 2 more");
        int newCapacity = items.length + 2;
        top = items.length - 1;
        items = Arrays.copyOf(items, newCapacity);
        top++;
        items[top] = i;
    }
}

public void displayContents() {
    System.out.println(this);

}

public Item pop() {
    if (top == -1) {
        throw new RuntimeException("Stack is empty");
    } else {
        Item i = items[top];
        items[top] = null;
        top--;
        return i;
    }
}

private boolean stackNotFull() {
    return top < items.length - 1;
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.