ABSTRACT DATA TYPE
The Abstract datatype (ADT) is a special kind of datatype, whose behaviour is defined by a set of values and a set of operations. The keyword “Abstract” is used as we can use these datatypes, we can perform different operations. But how those operations are working is totally hidden from the user. The ADT is made of primitive datatypes, but operations logic are hidden
Examples of ADT are List, Stack and Queue etc.
Let us see some operations of those mentioned ADT −
List −
- size(), this function is used to get the number of elements present into the list
- insert(x), this function is used to insert one element into the list
- remove(x), this function is used to remove a given element from the list
- get(i), this function is used to get the element at position i
- replace(x, y), this function is used to replace x with y value
Stack −
- isFull(), This is used to check whether the stack is full or not
- isEmpry(), This is used to check whether the stack is empty or not
- push(x), This is used to push x into the stack
- pop(), This is used to delete one element from the top of the stack
- peek(), This is used to get the topmost element of the stack
- size(), this function is used to get the number of elements present into the stack
Queue −
- isFull(), This is used to check whether a queue is full or not
- isEmpry(), This is used to check whether a queue is empty or not
- insert(x), This is used to add x into the queue at the rear end
- delete(), This is used to delete one element from the front end of the queue
- size(), this function is used to get the number of elements present in the queue
ABSTRACT DATA TYPE-LIST
A list with elements of type T, that have a finite sequence of elements of type T together with the operations of create, update, delete, testing for empty, testing for full, finding the size, traversing the elements.
ARRAY IMPLEMENTATION OF LISTS
typedef struct
{int count;
int entry[100];
}list;
Example: - Creation, Inserting Deletion and traversing an element into a contiguous list
(Linear Array) at the specified position
#include < stdio.h > /*definition of linear list */ typedef struct { int data[10]; int count; } list; /*prototypes of functions */ void insert(list *, int, int); void create(list*); void traverse(list*); /*declaration of delete_list function */ void delete_list(list *, int); /*definition of delete_list function*/ /*the position of the element is given by the user and the element is deleted from the list*/ void delete_list(list *start, int position) { int temp = position; printf("\n information which we have to delete: %d", start->data[position]); while (temp <= start->count - 1) { start->data[temp] = start->data[temp + 1]; temp++; } start->count = start->count - 1; } /*definition of create function to READ data values into the list */ void create(list *start) { int i = 0, test = 1; /*decleare the data[i] of the list and terminat at element value "0" */ while (test) { fflush(stdin); printf("\n input value value for: %d:(zero to come out) ", i); scanf("%d", &start->data[i]); /*if element value "0" while loop terminate*/ if (start->data[i] == 0) test = 0; else i++; } /*set the lenght of List "i" in count*/ start->count = i; } /*Definition of the insert funtion To insert you need position and value of element */ void insert(list *start, int position, int element) { int temp = start->count; while (temp >= position) { /*copy the elements of the list in next index of the list */ start->data[temp + 1] = start->data[temp]; temp--; } /*insert element at position of index */ start->data[position] = element; /*increament the index of List by 1 */ start->count++; } /*OUTPUT FUNCTION TO PRINT ON THE CONSOLE */ void traverse(list *start) { int i; for (i = 0; i < start->count; i++) { printf("\n Value at the position: %d: %d ", i, start->data[i]); } } /*main function */ void main() { int position, element; list l; create(&l); printf("\n Entered list as follows:\n"); fflush(stdin); traverse(&l); fflush(stdin); printf("\n input the position of element you want to delete:"); scanf("%d", &position); fflush(stdin); delete_list(&l, position); traverse(&l); fflush(stdin); printf("\n input the position where you want to add a new data item:"); scanf("%d", &position); fflush(stdin); printf("\n input the value for the position:"); scanf("%d", &element); insert(&l, position, element); traverse(&l); }
0 Comments