Saturday 16 February 2019

How to create an abstract data type

#data_type#

 Image result for abstract data type

1 – Define the abstract data type
The ADT in C is usually defined as a pointer to a structure. A header file contains the ADT declaration without any of the underlying details, leaving it up to the implementer to fully declare the ADT in the source module. Examples of ADTs include a StackPtr_t, NodePtr_t or QueuePtr_t to name a few. The example below shows how a developer might declare an ADT:
    typedef struct StackStruct_t * StackPtr_t;
The declaration would go in the stack.h file, allowing the module's user to make use of StackPtr_t, which is a pointer to StackStruct_t. The details of StackStruct_t's members are completely hidden from the users’ perspective. Any interaction with StackPtr_t must be done using predefined operations.

2 – Define the operations that can be performed on the data The operations that may be performed on an ADT completely depend on the ADT's purpose. For example, an ADT for a stack might include operations such as initialization, pushing data, popping data, destroying the stack, checking to see if the stack is full, checking to see if the stack is empty, and so on. Keep in mind that using an ADT is quite different from the way in which a developer would normally manipulate data.

3 – Fill in the interface specification The interface specification is the function prototype for all of the public operations that can be performed on the ADT. The interface specification should be located in the ADT header file. Going back to the stack example

Create the implementation The ADT's implementation could change from one application to the next. In fact, the ADT implementation could change during project development. That is one of the nice aspects of using an ADT: the implementation details are located in the source module and “hidden” from view of the higher level application developer. The use of an ADT thus provides a developer with a high degree of flexibility

Put the abstract data type to the test Finally, once a developer has specified and implemented an ADT it is time to put it to the test by writing some application code. The application code should declare an ADT and then manipulate the data's contents through by using the interface specification

No comments:

Post a Comment

Note: only a member of this blog may post a comment.