



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




An Abstract Data Type (ADT) is a type (or class) of objects whose behavior is defined by a set of values and a set of operations. ADTs provide an abstract view of the data, focusing on what operations can be performed on the data rather than how those operations are implemented.
The key aspects of an ADT are:
Abstraction : ADTs provide an objective view of the implementation, hiding the internal details. Encapsulation : ADTs encapsulate the data and the operations that can be performed on that data. Information Hiding : ADTs hide the internal representation of the data from the user, allowing the implementation to be changed without affecting the user.
Example ADTs
Array List ADT : Operations: add(item), remove(index), get(index), size()
Example implementation using an array in Java: ArrayList
Stack ADT :
Operations: push(item), pop(), peek(), isEmpty(), isFull()
Follows the Last-In-First-Out (LIFO) principle
Queue ADT :
Operations: enqueue(item), dequeue(), front(), isEmpty(), isFull() Follows the First-In-First-Out (FIFO) principle
A stack is an abstract data type that represents a collection of elements, with two principal operations:
Push : Adds an element to the collection.
Pop : Removes the most recently added element that was not yet removed.
The order in which elements come out of a stack gives rise to its alternative name, Last-In-First-Out (LIFO) stack.
Stack Operations
Push(item) : Adds the given item to the top of the stack. Pop() : Removes and returns the top item from the stack. Peek() : Returns the top item from the stack without removing it. isEmpty() : Returns true if the stack is empty, false otherwise. isFull() : Returns true if the stack is full, false otherwise.
Stack Implementation
Stacks can be implemented using various data structures, such as arrays or linked lists. The choice of implementation depends on the specific requirements of the application.
Example implementation using an array in Java:
@SuppressWarnings("unchecked") public Stack(int capacity) { items = (T[]) new Object[capacity]; top = -1; } public void push(T item) { if (top == items.length - 1) { throw new IllegalStateException("Stack is full"); } items[++top] = item; } public T pop() { if (top == -1) { throw new IllegalStateException("Stack is empty"); } return items[top--]; } public T peek() { if (top == -1) { throw new IllegalStateException("Stack is empty"); } return items[top]; } public boolean isEmpty() { The order in which elements are removed from a queue gives rise to its alternative name, **First-In-First-Out (FIFO)** queue. **Queue Operations** **Enqueue(item)** : Adds the given item to the rear of the queue. **Dequeue()** : Removes and returns the item at the front of the queue. **Front()** : Returns the item at the front of the queue without removing it. **isEmpty()** : Returns true if the queue is empty, false otherwise. **isFull()** : Returns true if the queue is full, false otherwise. **Queue Implementation** Queues can be implemented using various data structures, such as arrays or linked lists. The choice of implementation depends on the specific requirements of the application. Example implementation using an array in Java: ```java public class Queue { private T[] items; private int front, rear, size; @SuppressWarnings("unchecked") public Queue(int capacity) { items = (T[]) new Object[capacity]; front = 0; rear = -1; size = 0; } public void enqueue(T item) { if (rear == items.length - 1) { rear = -1; } items[++rear] = item; size++; } public T dequeue() { if (size == 0) { throw new IllegalStateException("Queue is empty"); } T item = items[front++]; if (front == items.length) { front = 0; } size--; return item; } public T front() { if (size == 0) { throw new IllegalStateException("Queue is empty"); } return items[front]; } public boolean isEmpty() { return size == 0; } public boolean isFull() { return size == items.length; } } ``` ## Conclusion In this document, we have explored the concept of Abstract Data Types (ADTs) and their key characteristics, including abstraction, encapsulation, and information hiding. We have examined three common ADTs: the Stack, the Memory Stack, and the Queue, along with their operations and implementation details. Understanding these fundamental data structures and their associated algorithms is crucial for designing and developing efficient software systems. **How Stack Used to Implement Function Calls** ## Definition of Function A function is a set of code that performs a specific task and can be used whenever needed just by calling it. While using multiple function calls or recursion, the concept of a function call is necessary to be known, for understanding of the code with the best (Cornen, 2009). ## Example of the Working of Function Calls Since the call stack is organized as a stack, the caller pushes the return address into the stack, and the called subroutine, when it finishes, pulls or pops the return address off the call stack and transfers control to that address. The primary purpose of a call stack is to store the return addresses. When a subroutine is called, the address of the instruction at which the calling routine can later resume needs to be saved somewhere. Stacks are known as last-in first-out (LIFO) data structures - the last element pushed (inserted) in the stack is the first element popped (removed) from the stack. ## Explanation of Function Calls When the program calls a function, the main() is called, an activation record is created and added to the top of the stack. The main() calls A() and next is B(), putting an activation record for A() first and then