Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

UNIT 3

Abstract Data Types


This module presents terminology and definitions related to techniques for managing the
tremendous complexity of computer programs. It also presents working definitions for the
fundamental but somewhat slippery terms “data item” and “data structure”. We begin with
the basic elements on which data structures are built.

A type is a collection of values. For example, the Boolean type consists of the
values true and false. The integers also form a type. An integer is a simple
type because its values contain no subparts. A bank account record will typically contain
several pieces of information such as name, address, account number, and account
balance. Such a record is an example of an aggregate type or composite type. A data
item is a piece of information or a record whose value is drawn from a type. A data item is
said to be a member of a type.

A data type is a type together with a collection of operations to manipulate the type. For
example, an integer variable is a member of the integer data type. Addition is an example of
an operation on the integer data type.

A distinction should be made between the logical concept of a data type and its physical
implementation in a computer program. For example, there are two traditional
implementations for the list data type: the linked list and the array-based list. The list data
type can therefore be implemented using a linked list or an array. But we don’t need to know
how the list is implemented when we wish to use a list to help in a more complex design. For
example, a list might be used to help implement a graph data structure.

As another example, the term “array” could refer either to a data type or an implementation.
“Array” is commonly used in computer programming to mean a contiguous block of memory
locations, where each memory location stores one fixed-length data item. By this meaning,
an array is a physical data structure. However, array can also mean a logical data type
composed of a (typically homogeneous) collection of data items, with each data item
identified by an index number. It is possible to implement arrays in many different ways
besides as a block of contiguous memory locations. The sparse matrix refers to a large,
two-dimensional array that stores only a relatively few non-zero values. This is often
implemented with a linked structure, or possibly using a hash table. But it could be
implemented with an interface that uses traditional row and column indices, thus appearing
to the user in the same way that it would if it had been implemented as a block of contiguous
memory locations.

An abstract data type (ADT) is the specification of a data type within some language,
independent of an implementation. The interface for the ADT is defined in terms of a type
and a set of operations on that type. The behavior of each operation is determined by its
inputs and outputs. An ADT does not specify how the data type is implemented. These
implementation details are hidden from the user of the ADT and protected from outside
access, a concept referred to as encapsulation.

A data structure is the implementation for an ADT. In an object-oriented language, an ADT


and its implementation together make up a class. Each operation associated with the ADT is
implemented by a member function or method. The variables that define the space
required by a data item are referred to as data members. An object is an instance of a
class, that is, something that is created and takes up storage during the execution of a
computer program.

The term data structure often refers to data stored in a computer’s main memory. The
related term file structure often refers to the organization of data on peripheral storage,
such as a disk drive or CD.

LIST ADT

Each list element must have some data type. In the simple list implementations discussed in
this chapter, all elements of the list are usually assumed to have the same data type,
although there is no conceptual objection to lists whose elements have differing data types if
the application requires it. The operations defined as part of the list ADT do not depend on
the elemental data type. For example, the list ADT can be used for lists of integers, lists of
characters, lists of payroll records, even lists of lists.

A list is said to be empty when it contains no elements. The number of elements currently


stored is called the length of the list. The beginning of the list is called the head, the end of
the list is called the tail.

We need some notation to show the contents of a list, so we will use the same angle bracket
notation that is normally used to represent sequences. To be consistent with standard array
indexing, the first position on the list is denoted as 0. Thus, if there are nn elements in the
list, they are given positions 0 through n−1n−1 as ⟨ a0, a1, ..., an−1 ⟩⟨ a0, a1, ..., an−1 ⟩. The
subscript indicates an element’s position within the list. Using this notation, the empty list
would appear as ⟨ ⟩⟨ ⟩.

A list should be able to grow and shrink in size as we insert and remove elements. We
should be able to insert and remove elements from anywhere in the list. We should be able
to gain access to any element’s value, either to read it or to change it. We must be able to
create and clear (or reinitialize) lists. It is also convenient to access the next or previous
element from the “current” one.

we can define the ADT for a list object in terms of a set of operations on that object. We will
use an interface to formally define the list ADT. List defines the member functions that any
list implementation inheriting from it must support, along with their parameters and return
types.

 The data is generally stored in key sequence in a list which


has a head structure consisting of count, pointers and address of
compare function needed to compare the data in the list.
 The data node contains the pointer to a data structure and
a self-referential pointer which points to the next node in the list.
//List ADT Type Definitions

typedef struct node

 void *DataPtr;

 struct node *link;

} Node;

typedef struct

 int count;

 Node *pos;

 Node *head;

 Node *rear;

 int (*compare) (void *argument1, void *argument2)

} LIST; 
 The List ADT Functions is given below:

2. A list contains elements of the same type arranged in sequential


order and following operations can be performed on the list.
 get() – Return an element from the list at any given
position.
 insert() – Insert an element at any position of the list.
 remove() – Remove the first occurrence of any element
from a non-empty list.
 removeAt() – Remove the element at a specified location
from a non-empty list.
 replace() – Replace an element at any position by another
element.
 size() – Return the number of elements in the list.
 isEmpty() – Return true if the list is empty, otherwise return
false.
 isFull() – Return true if the list is full, otherwise return false.
3. Stack ADT
 In Stack ADT Implementation instead of data being stored
in each node, the pointer to data is stored.
 The program allocates memory for the data and address is
passed to the stack ADT.

 The head node and the data nodes are encapsulated in the
ADT. The calling function can only see the pointer to the stack.
 The stack head structure also contains a pointer
to top and count of number of entries currently in stack.
//Stack ADT Type Definitions

typedef struct node

 void *DataPtr;

 struct node *link;

} StackNode;

typedef struct

 int count;

 StackNode *top;

} STACK;

4. A Stack contains elements of the same type arranged in


sequential order. All operations take place at a single end that is top of
the stack and following operations can be performed:
 push() – Insert an element at one end of the stack called
top.
 pop() – Remove and return the element at the top of the
stack, if it is not empty.
 peek() – Return the element at the top of the stack without
removing it, if the stack is not empty.
 size() – Return the number of elements in the stack.
 isEmpty() – Return true if the stack is empty, otherwise
return false.
 isFull() – Return true if the stack is full, otherwise return
false.
5. Queue ADT
 The queue abstract data type (ADT) follows the basic
design of the stack abstract data type.

 Each node contains a void pointer to the data and the link


pointer to the next element in the queue. The program’s responsibility
is to allocate memory for storing the data.
//Queue ADT Type Definitions

typedef struct node

{
 void *DataPtr;

 struct node *next;

} QueueNode;

typedef struct 

 QueueNode *front;

 QueueNode *rear;

 int count;

} QUEUE;

6. A Queue contains elements of the same type arranged in


sequential order. Operations take place at both ends, insertion is done at
the end and deletion is done at the front. Following operations can be
performed:
 enqueue() – Insert an element at the end of the queue.
 dequeue() – Remove and return the first element of the
queue, if the queue is not empty.
 peek() – Return the element of the queue without removing
it, if the queue is not empty.
 size() – Return the number of elements in the queue.
 isEmpty() – Return true if the queue is empty, otherwise
return false.
 isFull() – Return true if the queue is full, otherwise return
false.
From these definitions, we can clearly see that the definitions do not specify
how these ADTs will be represented and how the operations will be carried
out. There can be different ways to implement an ADT, for example, the List
ADT can be implemented using arrays, or singly linked list or doubly linked
list. Similarly, stack ADT and Queue ADT can be implemented using arrays
or linked lists.

You might also like