Resources | Subject Notes | Computer Science
An Abstract Data Type (ADT) is a mathematical model that defines a set of data and the operations that can be performed on that data. It focuses on what the data does, rather than how it is implemented. This separation of concerns allows for flexibility in implementation and promotes code reusability.
Using ADTs offers several advantages:
Common examples of ADTs include stacks, queues, and linked lists. These are fundamental data structures used in many computer science applications.
A stack is an ADT that follows the Last-In, First-Out (LIFO) principle. Think of a stack of plates ÔÇô the last plate added is the first one removed.
Operation | Description | Complexity |
---|---|---|
push | Adds an element to the top of the stack. | O(1) |
pop | Removes the element from the top of the stack. | O(1) |
peek | Returns the element at the top of the stack without removing it. | O(1) |
isEmpty | Checks if the stack is empty. | O(1) |
Implementation Examples: Stacks can be implemented using arrays or linked lists.
A queue is an ADT that follows the First-In, First-Out (FIFO) principle. Think of a queue of people waiting in line ÔÇô the first person in line is the first one served.
Operation | Description | Complexity |
---|---|---|
enqueue | Adds an element to the rear of the queue. | O(1) |
dequeue | Removes the element from the front of the queue. | O(1) |
peek | Returns the element at the front of the queue without removing it. | O(1) |
isEmpty | Checks if the queue is empty. | O(1) |
Implementation Examples: Queues can be implemented using arrays (circular queues) or linked lists.
A linked list is an ADT where elements are stored in nodes, and each node contains a data value and a pointer (or link) to the next node in the list.
Linked lists provide dynamic memory allocation, meaning they can grow or shrink as needed. They are useful when the size of the data is not known in advance.
Operation | Description | Complexity |
---|---|---|
insert | Adds a new node to the list. | O(1) (if position is known), O(n) (if searching for position) |
delete | Removes a node from the list. | O(1) (if node is known), O(n) (if searching for node) |
search | Finds a node with a specific value. | O(n) |
isEmpty | Checks if the list is empty. | O(1) |
Implementation: Linked lists are typically implemented using pointers (references) to the next node in the sequence.