Question

In: Computer Science

What do you understand by abstract data type or ADT? Provide examples and discuss implementation mechanisms...

What do you understand by abstract data type or ADT? Provide examples and discuss implementation mechanisms in C++ and in object-oriented programming languages in general.

Solutions

Expert Solution

An abstract data type is a class or type which only exposes the the compatible operations that can be performed by the object of that type without providing any details of the implementation. It is called “abstract” because it gives an implementation-independent view.

For example, a Stack ADT can have the following functionalities:

  • 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.

Below is a sample code for the same:

CODE

#include <bits/stdc++.h>

using namespace std;

#define MAX 1000

class Stack {

  int top;

public:

  int a[MAX];

  Stack() { top = -1; }

  bool push(int x);

  int pop();

  int peek();

  bool isEmpty();

int size();

bool isFull();

};

bool Stack::push(int x)

{

  if (top >= (MAX - 1)) {

    cout << "Stack Overflow";

    return false;

  }

  else {

    a[++top] = x;

    cout << x << " pushed into stack\n";

    return true;

  }

}

int Stack::pop()

{

  if (top < 0) {

    cout << "Stack Underflow";

    return 0;

  }

  else {

    int x = a[top--];

    return x;

  }

}

int Stack::peek()

{

  if (top < 0) {

    cout << "Stack is Empty";

    return 0;

  }

  else {

    int x = a[top];

    return x;

  }

}

bool Stack::isEmpty()

{

  return (top < 0);

}

bool Stack::isFull()

{

  return (top >= MAX);

}

int Stack::size() {

return top;

}


Related Solutions

Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What...
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What is the running time for each operation?
1.  What is an abstract data type? In an ADT, what is known and what is hidden?
1.  What is an abstract data type? In an ADT, what is known and what is hidden?
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined...
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined as follows: struct Employee int id; // unique employee identifier string name; // employee’s full name double rate; // employee’s hourly rate double hours; // how many hours they worked since last pay double taxable; // the current year’s taxable income }; This definition should appear above the main() function. The main() function should include: 1. Declare a single array to hold at most...
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to...
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to teach each of the operations in the ADT. Any errors discovered during the processing should be printed as a part of the test result. Please Use C++ language.
What do you understand by adjusting entries? Give examples.
What do you understand by adjusting entries? Give examples.
What two types of services do internal auditors provide? Provide three examples of each type of...
What two types of services do internal auditors provide? Provide three examples of each type of engagement. What steps are included in the planning phase of an assurance engagement? What is the relationship between business objectives and business assertions? What does "inherent risk" mean? What elements do well-written observations include? What is the difference between "negative assurance" and "positive assurance?" What information must final assurance engagement communications include?
What do you understand by " hydrated ions" ? Give examples of hydrated ions and explain...
What do you understand by " hydrated ions" ? Give examples of hydrated ions and explain at least one hydrated ion's sturcture and formation.
Please provide Python 3.7 implementation of Graph ADT using in-built structure List only (not using dict)....
Please provide Python 3.7 implementation of Graph ADT using in-built structure List only (not using dict). Basic structure of graph will be: Node = [] Edges = [ [ ] , [ ] ] Also provide method to print the graph along with all path
What do you understand by Fixed Cost and Variable Cost? Give some examples also.
What do you understand by Fixed Cost and Variable Cost? Give some examples also.
What do you understand by intelligence?
What do you understand by intelligence? what are the types of intelligence.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT