C/C programming
Expert: Zlatko - 10/7/2010
QuestionWhat do you mean by Abstract Data Type?
AnswerAn abstract data type (ADT) is one which is manipulated by functions and where the user of the data type does not need to know about the implementation of the data type in order to use it.
Consider a singly-linked list. There are two different ways to approach using a such a linked list in your program. One approach is to have all parts of your program "know" about the structure of the list, specifically that it is a singly-linked list. All parts of the program have access the pointers of the linked list data structure, and are able to change them. This is a bad approach because if you decide to change your linked list to some other kind of data structure, such as a doubly-linked list, then all parts of your program which access the list will have to be changed. It a bad approach also because the list manipulation code ends up being duplicated all over the program.
The ADT approach is to define a handle to the linked list. The handle can be some struct definition, for example
typedef struct linkedList
{
void* head;
void* tail;
} LinkedList;
Whatever the handle is doesn't matter to the code using the linked list. The code using the list should not know how the list is implemented. The code using the list should not even look at the contents of the handle. Instead, you define a set of functions, such as insert, find, delete, etc, to do operations on the list. The program using the list then passes the handle to the functions. In this way, if the implementation of the list changes, only the list functions (insert, find, delete, etc) need to be changed. The rest of the program does not need to be changed. The combination of the handle, and the functions makes the abstract data type.
Here is an example of the function declarations:
void ListInsert(LinkedList* handle, void* dataToInsert);
void* ListFind(LinkedList* handle, void* dataToFind);
void ListDelete(LinkedList* handle, void* dataToDelete);
Another example of an ADT is the FILE* which you are familiar with from C. Notice that you don't even know what is in the FILE structure. It is just a handle that is passed around to fread, fwrite, feof, etc.
If you have more questions, ask me.
Best regards
Zlatko