Make a dictionary using linked list that searches names of people entered by the user using the First alphabet of the name
For example, if user enters names, Ali, abbas, bakar, rehan and then searches for names starting with 'a' then it displays the names
Abbas and Ali
Note:
No global declarations
Make use of class or struct
Test run your code in main
#include <iostream>
#include "string"
namespace user {
template<typename T>
struct List {
private:
struct Node {
Node() {}
Node(T n) : data{n} {}//конструктор
T &getValue() {
T &n = data;
return n;
}//Get number
Node *next() { return pointerNext; }//Next node pointer
Node *prev() { return pointerPrev; }//Prev node pointer
void setNext(Node *p = nullptr) { pointerNext = p; };
void setPrev(Node *p = nullptr) { pointerPrev = p; };
Node *pointerNext = nullptr;
Node *pointerPrev = nullptr;
T data;
};
int size = 0;
[[nodiscard]] bool isEmpty() const{return size == 0;}
Node *right = new Node();
Node *left = right;
public:
List<T>()= default;
List<T>(const List<T>&) = delete ;
List<T>& operator=(const List<T>&) = delete ;
/*T& get(void* founded);*/
T front() {
if (!isEmpty()) {
return left->getValue();
}
return T();
}
T back() {
if (!isEmpty()) {
T &transport = right->prev()->getValue();
return transport;
}
return T();
}
void pop_back() {
if (isEmpty()) {
return;
}
if (size == 1) {
pop_front();
return;
}
Node *work = right;
right = work->prev();
right->setNext();
delete work;
size--;
}
void pop_front() {
if (isEmpty()) {
return;
}
Node *work = left;
left = work->next();
left->setPrev();
delete work;
size--;
}
void push_back(T n) {
if (isEmpty()) {
left = new Node(n);
right->setPrev(left);
left->setNext(right);
size++;
return;
}
Node *work = right->prev();
right->setPrev(new Node(n));
work->setNext(right->prev());
(work->next())->setPrev(work);
(work->next())->setNext(left);
size++;
}
void push_front(T n) {
if (isEmpty()) {
push_back(n);
return;
}
Node *work = left;
left = new Node(n);
work->setPrev(left);
left->setNext(work);
size++;
}
void *find(T n) {
Node *founded = nullptr;
if (isEmpty()) {
return founded;
}
Node *ptr = left;
while (ptr->next() != nullptr) {
if (ptr->getValue() == n) {
founded = ptr;
break;
}
ptr = ptr->next();
}
void *direction = founded;
return direction;
}
template<typename C>
void find(C n, List<T>* founded) {
Node *ptr = left;
for(auto i = size; i>0;i--) {
if (n(ptr->getValue())) {
founded->push_back(ptr->getValue());
}
ptr = ptr->next();
}
}
bool erase(void *founded = nullptr) {
if (founded == nullptr || isEmpty()) {
return false;
}
Node *trash = static_cast<Node *>(founded);
if (trash == left) {
pop_front();
return true;
} else if (trash == right->prev()) {
pop_back();
return true;
} else {
trash->next()->setPrev(trash->prev());
trash->prev()->setNext(trash->next());
}
delete trash;
size--;
return true;
}
void clear() {
for (; 0 < size; size--) {
pop_back();
}
}
~List() {
delete right;
for (; size; size--) {
left = left->next();
delete left->prev();
}
}
[[nodiscard]] bool empty() const {return isEmpty();};
};
}
int main() {
user::List<std::string> names{};
int i{};
std::cin >> i; //number;
for(;i>0;--i){
std::string temp{};
std::cin>>temp;
names.push_back(temp);
}
char x;
bool flag = true;
while(flag){
std::cout<<"\nFirst letter: ";
std::cin>>x;
user::List<std::string> temp{};
names.find([x](const std::string& str)mutable ->bool{
return str[0]==x;}, &temp);
flag = false;
while(!temp.empty()){
flag = true;
std::cout<<"\n"<<temp.front();
temp.pop_front();
}
}
return 0;
}
Comments
Leave a comment