Abstract Data Type (ADT) In Python

Tusamma Sal Sabil
2 min readMar 15, 2020

--

What is ADT?

The abstract data type is special kind of data type, whose behavior is defined by a set of values and set of operations. The keyword “Abstract” is used as we can use these data types, we can perform different operations. But how those operations are working that is totally hidden from the user. The ADT is made of primitive data types, but operation logics are hidden.

Some examples of ADT are Stack, Queue, List etc.

Stack Abstract Data Type

The stack abstract data type is defined by the following structure and operations. A stack is structured, as described above, as an ordered collection of items where items are added to and removed from the end called the “top”. Stacks are ordered LIFO. The stack operations are given below.

  • Stack(): creates a new stack that is empty. It needs no parameters and returns an empty stack.
  • push(item): adds a new item to the top of the stack. It needs the item and returns nothing.
  • pop(): removes the top item from the stack. It needs no parameters and returns the item. The stack is modified.
  • peek(): returns the top item from the stack but does not remove it. It needs no parameters. The stack is not modified.
  • isEmpty(): tests to see whether the stack is empty. It needs no parameters and returns a boolean value.
  • size(): returns the number of items on the stack. It needs no parameters and returns an integer.

For example, if s is a stack that has been created and starts out empty, then it shows the results of a sequence of stack operations. Under stack contents, the top item is listed at the far right.

Sample Stack Operations

Queue Abstract Data Type

A Queue is a FIFO (first in, first out) list with the following operations: Enqueue, Dequeue, Size, Font.

  • Queue(): creates a new queue that is empty. It needs no parameters and returns an empty queue.
  • enqueue(item): adds a new item to the rear of the queue. It needs the item and returns nothing.
  • dequeue(): removes the item from the front of the queue. It needs no parameters and returns the item. The queue is modified.
  • front(): returns the front item from the queue but does not remove it. It needs no parameters. The queue is not modified.
  • isEmpty(): tests to see whether the queue is empty. It needs no parameters and returns a boolean value.
  • size(): returns the number of items on the queue. It needs no parameters and returns an integer.

All operations o queue adt is same as stack except dequeue. It pick first item of the queue.

For detailed coding example please go through my github repository.

GitHub Link: Abstract Data Types

--

--