
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- Email & Phone Validation in Swift
- How to obtain the phone number of the android phone programmatically?
- How to obtain the phone number of the iOS phone programmatically?
- What is Design of Lexical Analysis in Compiler Design?
- Advanced Mobile Phone System
- How to obtain the phone number of the Android phone programmatically using Kotlin?
- Differentiate between Interface design and Interaction design
- 6 must have apps in your phone
- How to get phone number in android?
- How to add Phone Input in NextJS ?
- Design HashSet in Python
- Design HashMap in Python
- Colour Palette in Design
- Design Pattern in Dart
- Copy a directory to an existing directory Linux?
