The abstract data type for a software stack?

1.17K 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.

Anonymous 0 Comments

Abstract data types are used to describe an object without needing to know how it works.

A valet parking your car doesn’t care what’s under the hood. A valet’s job can be described by laying out an abstract concept of what he needs to do his job. He just needs a vehicle that accelerates, decelerates, turns left, turns right. A valet could park a plane with the interface below. A valet could park a boat. A valet doesn’t care that it’s a propeller or turbine making the plane go forward and backward. He doesn’t care that it’s a rudder moving the plane or boat left and right.

A valet can do his job as long as he has the methods below.

Object Vehicle

{

method Accelerate

method decelerate

method turn right

method turn left

}

In software, think about a system that needs to pull data from multiple sources. You could created an abstract concept of what the system needs. GetFirstName, GetLastName, GetPhoneNumber, etc… When a consumer needs to use that system, they would be in charge of creating their own object that implements this interface and actually does the things the methods describe. I need to GetFirstName, so I need to connect to a database, run a query based on parameters, get the data from the result set and send the first name back. How I do that is up to me, the systems doesn’t care how I implemented it, it just needs me to fill in the work.