 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Design Phone Directory in C++
Suppose we want to design a Phone Directory which supports the following operations −
- get − This will provide a number that is not assigned to anyone. 
- check − This will check whether a number is available or not. 
- release − This will recycle or release a number. 
Using the initializer, we can initialize n numbers at first
To solve this, we will follow these steps −
- Define one set s 
- Define one queue available 
- The initializer will take maxNumbers. 
- N := maxNumbers 
-  for initialize i := 0, when i < N, update (increase i by 1), do − - insert i into available 
 
- Define a function get() 
-  if size of available is same as 0, then − - return -1 
 
- x := first element of available 
- insert x into s 
- delete element from available 
- return x 
- Define a function check(), this will take number, 
-  if number >= N or number < 0, then − - return false 
 
- return true number is not in s 
- Define a function release(), this will take number, 
-  if check(number), then − - return 
 
- x := number 
- delete x from s 
- insert x into available 
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class PhoneDirectory { public:    set<int< s;    queue<int< available;    int N;    PhoneDirectory(int maxNumbers){       N = maxNumbers;       for (int i = 0; i < N; i++) {          available.push(i);       }    }    int get(){       if (available.size() == 0)          return -1;       int x = available.front();       s.insert(x);       available.pop();       return x;    }    bool check(int number){       if (number >= N || number < 0)          return false;       return s.find(number) == s.end();    }    void release(int number){       if (check(number))          return;       int x = number;       s.erase(x);       available.push(x);    } }; main(){    PhoneDirectory ob(3);    cout << (ob.get()) << endl;    cout << (ob.get()) << endl;    cout << (ob.check(2)) << endl;    cout << (ob.get()) << endl;    cout << (ob.check(2)) << endl;    ob.release(2);    cout << (ob.check(2)) << endl; }  Input
ob.get(); ob.get(); ob.check(2); ob.get(); ob.check(2); ob.release(2); ob.check(2);
Output
0 1 1 2 0 1
