The abstract data type for a software stack?

1.20K views

When it comes to ADTs I’m clueless.

In: Technology

2 Answers

Anonymous 0 Comments

An abstract data type is a scheme for storing and retrieving data combined with a set of operators for doing so. The idea is one programmer makes a really good tool that other programmings can use. The internals are hidden from them, so they don’t have to worry about how it works, they just use the provided interface. And if someone changes those internals, so long as they keep the interface the same other programmers don’t have to change their code to work with the new version.

A stack is a First In Last Out (FILO) list, the last thing you put in the list is the first thing that has to come out before you can access any other item. I like to envision that spring-loaded stack of plates you see at cafeterias and buffets when I think of a stack. A stack ADT will typically have four operators, create, push (put an item on top of the stack), pop (remove an item from the stack and return it) and destroy. A piece of code might look something like this:

`s = Stack.create()`

`s.push(4) // the value 4 is at the top of the stack`

`s.push(7) // the stack is now 7, 4`

`x = s.pop // x is 7, the stack is 4`

`s.destory() // the resources used by the stack are returned to the OS`

How is the stack implemented internally? Unless you are writing the ADT, it doesn’t matter. It could be an array, it could be a linked list, it could be something weird and exotic. All a programmer using a stack cares about is whether create, push, pop, and destroy do what they are supposed to.

You are viewing 1 out of 2 answers, click here to view all answers.