Created
May 3, 2021 11:29
-
-
Save lablnet/5ddf14ea093d2e2da8f10ad85bec82d4 to your computer and use it in GitHub Desktop.
DSA Mid Exam Riphah
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MIDTERMEXAM_DATA_H | |
#define MIDTERMEXAM_DATA_H | |
#include "List.h" | |
class Items { | |
public: | |
int id; | |
std::string name; | |
std::string cnic; | |
std::string address; | |
std::string gender; | |
std::string city; | |
std::string provience; | |
bool status; | |
std::string occupation; | |
std::string dateOfReport; | |
float age; | |
}; | |
class Data { | |
DoublyLinkedList<Items> list; | |
public: | |
void insert(int id, std::string name, std::string cnic, float age, std::string gender, std::string address, std::string city, std::string provience, std::string occupation, std::string dateOfReport, bool status) { | |
auto *temp = (Items*) malloc(sizeof(Items)); | |
temp->id = id; | |
temp->name = name; | |
temp->address = address; | |
temp->age = age; | |
temp->city = city; | |
temp->cnic = cnic; | |
temp->dateOfReport = dateOfReport; | |
temp->occupation = occupation; | |
temp->gender = gender; | |
temp->provience = provience; | |
temp->status = status; | |
this->list.add(*temp); | |
} | |
void print(List<Items> *node) { | |
std::cout << "Name: " << node->data.name << std::endl; | |
std::cout << "City: " << node->data.city << std::endl; | |
std::cout << "Age: " << node->data.age << std::endl; | |
std::cout << "Address: " << node->data.address << std::endl; | |
std::cout << "Status: " << node->data.status << std::endl; | |
std::cout << "Provience: " << node->data.provience << std::endl; | |
std::cout << "Occupation: " << node->data.occupation << std::endl; | |
std::cout << "CNIC: " << node->data.cnic << std::endl; | |
} | |
int totalPatients(bool status) { | |
int total = 0; | |
auto *temp = this->list.head; | |
while (temp != nullptr) { | |
if (status == temp->data.status) { | |
total++; | |
} | |
temp = temp->next; | |
} | |
return total; | |
} | |
void print(float age , std::string city) { | |
auto *temp = this->list.head; | |
while (temp != nullptr) { | |
if (age == temp->data.age && city == temp->data.city) { | |
print(temp); | |
} | |
temp = temp->next; | |
} | |
} | |
void print() { | |
std::cout << "All records: " << std::endl; | |
auto *temp = this->list.head; | |
while (temp != nullptr) { | |
print(temp); | |
temp = temp->next; | |
} | |
} | |
}; | |
#endif //MIDTERMEXAM_DATA_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MIDTERMEXAM_LIST_H | |
#define MIDTERMEXAM_LIST_H | |
template <typename T> | |
class List { | |
public: | |
T data; | |
List *prev; | |
List *next; | |
}; | |
template <typename T> | |
class DoublyLinkedList { | |
public: | |
List<T> *head, *tail = nullptr; | |
int size = 0; | |
void add(T item); | |
}; | |
template<typename T> | |
void DoublyLinkedList<T>::add(T item) | |
{ | |
// Get new memory/ Create new list. | |
auto *list = new List<T>(); | |
list->data = item; | |
list->next = nullptr; | |
list->prev = this->tail; | |
if (this->head == nullptr) { | |
this->head = list; | |
this->tail = nullptr; | |
++this->size; | |
return; | |
} | |
List<T> *temp = this->head; | |
while (temp->next != nullptr ) { | |
temp = temp->next; | |
} | |
temp->next = list; | |
this->tail = list; | |
++this->size; | |
} | |
#endif //MIDTERMEXAM_LIST_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "Data.h" | |
int main() { | |
Data d; | |
// inserting | |
d.insert(12, "name","cnic", 16.5, "male", "address", "rawalpindi", "punjab", "teacher", "12 nov", true); | |
d.insert(122, "name 2","cnic", 34.5, "male", "address", "lahore", "punjab", "teacher", "12 nov", true); | |
d.print(34.5, "lahore"); | |
std::cout << d.totalPatients(true); | |
d.print(); | |
std::cout << "Hello, World!" << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment