0% found this document useful (0 votes)
43 views15 pages

Assignment 2 Imran Shaheer 19F-0285 Problem # 1: Code

The document contains code for two problems related to data structures in C++. The first problem involves creating and merging two linked lists. Functions are defined to create a linked list from user input, count the number of nodes, merge two lists, and rebuild the list. The second problem defines a class for patients in a hospital with functions to admit, discharge, and search for patients. Patient details like name, ID, room number are stored and functions operate on a linked list of patients.

Uploaded by

Imran Shaheer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views15 pages

Assignment 2 Imran Shaheer 19F-0285 Problem # 1: Code

The document contains code for two problems related to data structures in C++. The first problem involves creating and merging two linked lists. Functions are defined to create a linked list from user input, count the number of nodes, merge two lists, and rebuild the list. The second problem defines a class for patients in a hospital with functions to admit, discharge, and search for patients. Patient details like name, ID, room number are stored and functions operate on a linked list of patients.

Uploaded by

Imran Shaheer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Assignment 2

Imran Shaheer
19F-0285
Problem # 1:
Code:
#include<iostream>
using namespace std;
struct Node {
int data;
Node* address;
} *Head, * nodeptr;
class linklist {
Node* Head = NULL;
public:
void Null();
linklist()
{
Head = NULL;
}
};
Node* list(int n) {
for (int x = 0; x < n; x++)
{
if (Head == NULL)
{
nodeptr = Head = new Node;
cin >> Head->data;
Head->address = NULL;
}
else
{
nodeptr = nodeptr->address = new Node;
cin >> nodeptr->data;
nodeptr->address = NULL;
}
}
return Head;
}

int count(Node* List) {


int count = 0;
nodeptr = List;
while (nodeptr != NULL)
{
count++;
nodeptr = nodeptr->address;
}
return count;
}

Node* merge_lists(Node* f_list, Node* s_list)


{
if (f_list != NULL)
{
nodeptr = Head = f_list;
while (nodeptr->address != NULL)
{
nodeptr = nodeptr->address;
}

if (nodeptr->address == NULL && s_list != NULL)


{
nodeptr->address = s_list;
}
}
if (f_list == NULL && s_list != NULL)
{
Head = s_list;
}
for (int x = 0; x < 10; x++)
{
nodeptr = Head;
while (nodeptr->address != NULL)
{
if (nodeptr->data > nodeptr->address->data)
{
int swap = nodeptr->data;
nodeptr->data = nodeptr->address->data;
nodeptr->address->data = swap;
}
nodeptr = nodeptr->address;
}
}
return Head;
}
void rebuild()
{
nodeptr = Head = NULL;
}
int main()
{
int n = 0;
cout << "Enter Number of nodes for first list: ";
cin >> n;
cout << "Enter Element" << endl;
Node* ptr;
Node* ptr1;
Node* ptr2;
ptr = list(n);
rebuild();
cout << "Enter Number of nodes for second list : ";
cin >> n;
cout << "Enter Element" << endl;
ptr1 = list(n);
rebuild();
cout << "The list After Merging:";
ptr2 = merge_lists(ptr, ptr1);
nodeptr = ptr2;
while (nodeptr != 0)
{
cout << "->" << nodeptr->data;
nodeptr = nodeptr->address;
}
return 0;
}

Problem # 2:
Code:
#include<iostream>
#include<string>
using namespace std;

struct Date {
int day, month, year;
};
string availroom[20];
int roomcheck()
{
bool check = 0;
for (int n = 0; n < 20; n++)
{
if (availroom[n] == "available")
{
availroom[n] = "reserved";
check = 1;
return n + 1;
}
}
if (!check)
cout << "\n Rooms Are Fulled\n\n";
return 0;
}
class Patient;
Patient* nodeptr;
class Patient {
string Name;
long int CNIC;
string disease;
int RoomNo;
Date date;
public:
Patient* next;
public:
Patient() :Name("\0"), CNIC(0), disease("\0"), RoomNo(0) {
date.day = date.month = date.year = 0;
}
Patient* AdmitPatient(Patient* patient)
{
cout << "\n Enter the Details Of Patient!\n\n";
if (this == NULL)
{
nodeptr = patient = new Patient;
cout << "Enter Name : ";
cin >> nodeptr->Name;
cout << "Enter CNIC : ";
cin >> nodeptr->CNIC;
cout << "Enter Disease : ";
cin >> nodeptr->disease;
cout << "Enter Date";
cin >> nodeptr->date.day;
cout << "/";
cin >> nodeptr->date.month;
cout << "/";
cin >> nodeptr->date.year;
nodeptr->RoomNo = roomcheck();
nodeptr->next = NULL;
}
else
{
nodeptr = patient;
while (nodeptr->next != NULL)
nodeptr = nodeptr->next;
nodeptr = nodeptr->next = new Patient;
cout << "Enter Name : ";
cin >> nodeptr->Name;
cout << "Enter CNIC : ";
cin >> nodeptr->CNIC;
cout << "Enter Disease : ";
cin >> nodeptr->disease;
cout << "Enter Date";
cin >> nodeptr->date.day;
cout << "/";
cin >> nodeptr->date.month;
cout << "/";
cin >> nodeptr->date.year;
nodeptr->RoomNo = roomcheck();
nodeptr->next = NULL;
}
return patient;
}
Patient* dischargepatient(Patient* patient, long CNIC)
{
if (this != NULL)
{
Patient* Hold_address = 0;
nodeptr = patient;
if (nodeptr->CNIC == CNIC)
{
patient = nodeptr->next;
free(nodeptr);
return patient;
}
nodeptr = nodeptr->next;
Hold_address = nodeptr;
while (nodeptr->next != NULL)
{
if (nodeptr->CNIC == CNIC)
{
Hold_address->next = nodeptr->next;
free(nodeptr);
return patient;
}
Hold_address = nodeptr;
nodeptr = nodeptr->next;
}
if (nodeptr->CNIC == CNIC)
{
Hold_address->next = NULL;
free(nodeptr);
return patient;
}
}
return patient;
}
void searchtool()
{
char possiblenum = '\0';
bool check = 0;
cout << "Search Posibilities"<<endl;
cout << "1: Search By Disease" << endl;
cout << "2: Search by Room Number" << endl;
cout << "3: Search by CNIC" << endl;
cout<< "Enter way ";
cin >> possiblenum;
switch (possiblenum)
{
case '1':
{
string disease;
cout << "Enter Disease: " << endl;
cin >> disease;
nodeptr = this;
while (nodeptr != NULL)
{
if (disease == nodeptr->disease)
{
cout << *nodeptr;
check = 1;
}
nodeptr = nodeptr->next;
}
if (check == 0)
cout << "Patient not found" << endl;
}
break;
case'2':
{
int RoomNo;
cout << "Enter Room No: " << endl;
cin >> RoomNo;
nodeptr = this;
while (nodeptr != NULL)
{
if (RoomNo == nodeptr->RoomNo)
{
cout << *nodeptr;
check = 1;
}
nodeptr = nodeptr->next;
}
if (check == 0)
cout << "Patient not Found:" << endl;
}
break;
case'3':
{
long int CNIC;
cout << "Enter CNIC: ";
cin >> CNIC;
nodeptr = this;
while (nodeptr != NULL)
{
if (CNIC == nodeptr->CNIC)
{
cout << *nodeptr;
check = 1;
}
nodeptr = nodeptr->next;
}
if (check == 0)
cout << "Patient not found" << endl;
}
break;
}
}
void displayall(Patient* patient)
{
nodeptr = patient;
while (nodeptr != NULL)
{
cout << *nodeptr;
nodeptr = nodeptr->next;
}
}
friend ostream& operator<<(ostream& cout, Patient pa);
};

ostream& operator<<(ostream& cout, Patient pa)


{
cout << "Details Of Patient" << endl;
cout << "Name : " << pa.Name << endl;
cout << "CNIC : " << pa.CNIC << endl;
cout << "Disease : " << pa.disease << endl;
cout << "Room# : " << pa.RoomNo << endl;
cout << "Date : " << pa.date.day << "/" << pa.date.month << "/" << pa.date.year
<< endl;
return cout;
}

int main()
{
for (int n = 0; n < 20; n++)
{
availroom[n] = "Available";
}
Patient* patient = NULL;
char number = 0;
do {
cout << "1: Admit Patient:" << endl;
cout << "2: Discharge Patient:" << endl;
cout << "3: Search Patient:" << endl;
cout << "4: Printing All Patient:" << endl;
cout<< "Enter Number: ";
cin >> number;
system("CLS");
switch (number)
{
case'1':
{
patient = patient->AdmitPatient(patient);
}
break;
case'2':
{
long int CNIC;
cout << "Enter CNIC: ";
cin >> CNIC;
patient = patient->dischargepatient(patient, CNIC);
}
break;
case'3':
patient->searchtool();
break;
case'4':
patient->displayall(patient);
break;
}
cout << "DO you want to do it again(y)/(N)";
cin >> number;
if (number == 'y' || number == 'Y')
system("CLS");
} while (number == 'y' || number == 'Y');
while (patient != NULL)
{
nodeptr = patient;
patient = patient->next;
free(nodeptr);
}
return 0;
}

Problem # 3:
Code:
#include<iostream>
#include<string>
using namespace std;
class phone_diary;
phone_diary* nodeptr;

class phone_diary {
string name;
long int phone;
int age;
static int count;
public:
phone_diary* next, * prev;
static char is_undo;
phone_diary() :name(" "), phone(0), age(0)
{
}
phone_diary* insert(phone_diary* diary, phone_diary& Dairy) {

if (this == NULL)
{
nodeptr = diary = new phone_diary;
nodeptr->name = Dairy.name;
nodeptr->phone = Dairy.phone;
nodeptr->age = Dairy.age;
nodeptr->prev = nodeptr->next = NULL;
}
else
{
phone_diary* NewNode = NULL;
nodeptr = diary;
if (Dairy.name < nodeptr->name)
{
NewNode = new phone_diary;
NewNode->prev = nodeptr->prev;
diary = nodeptr->prev = NewNode;
NewNode->next = nodeptr;
NewNode->name = Dairy.name;
NewNode->phone = Dairy.phone;
NewNode->age = Dairy.age;
count++;
nodeptr->is_undo = 'i';
return diary;
}
nodeptr = nodeptr->next;
while (nodeptr->next != NULL)
{
if (Dairy.name > nodeptr->name && Dairy.name < nodeptr->next-
>name)
{
NewNode = new phone_diary;
NewNode->next = nodeptr->next;
NewNode->prev = nodeptr;
nodeptr->next->prev = NewNode;
nodeptr->next = NewNode;
NewNode->name = Dairy.name;
NewNode->phone = Dairy.phone;
NewNode->age = Dairy.age;
count++;
nodeptr->is_undo = 'i';
return diary;
}
nodeptr = nodeptr->next;
}
if (Dairy.name > nodeptr->name)
{
NewNode = new phone_diary;
nodeptr->next = NewNode;
nodeptr->next->prev = nodeptr;
NewNode->next = NULL;
NewNode->name = Dairy.name;
NewNode->phone = Dairy.phone;
NewNode->age = Dairy.age;
count++;
nodeptr->is_undo = 'i';
return diary;
}
}
}

void reversesort()
{
nodeptr = this;
while (nodeptr->next != NULL)
nodeptr = nodeptr->next;
while (nodeptr != NULL)
{
cout << *nodeptr;
nodeptr = nodeptr->prev;
}
}
phone_diary* UndoAction(phone_diary* diary, phone_diary& Diary)
{
if (is_undo == 'i')
diary = Delete(diary, Diary, Diary.name);
else if (is_undo == 'd')
diary = insert(diary, Diary);
return diary;
}
void Display()
{
nodeptr = this;
while (nodeptr != NULL)
{
cout << *nodeptr;
nodeptr = nodeptr->next;
}
}
phone_diary* Delete(phone_diary* diary, phone_diary& Diary, string Name)
{
if (this != NULL)
{
phone_diary* Hold_address = 0;
nodeptr = diary;
if (nodeptr->name == Name)
{
diary = nodeptr->next;
Diary.name = nodeptr->name;
Diary.phone = nodeptr->phone;
Diary.age = nodeptr->age;
free(nodeptr);
is_undo = 'd';
count--;
return diary;
}
nodeptr = nodeptr->next;
Hold_address = nodeptr;
while (nodeptr->next != NULL)
{
if (nodeptr->name == Name)
{
Hold_address->next = nodeptr->next;
Diary.name = nodeptr->name;
Diary.phone = nodeptr->phone;
Diary.age = nodeptr->age;
free(nodeptr);
is_undo = 'd';
count--;
return diary;
}
Hold_address = nodeptr;
nodeptr = nodeptr->next;
}
if (nodeptr->name == Name)
{
Hold_address->next = NULL;
Diary.name = nodeptr->name;
Diary.phone = nodeptr->phone;
Diary.age = nodeptr->age;
free(nodeptr);
is_undo = 'd';
count--;
return diary;
}
}
return diary;
}
void searchtool()
{
bool check = 0;
string Name;
cout << "Enter Name: ";
cin >> Name;
nodeptr = this;
while (nodeptr != NULL)
{
if (Name == nodeptr->name)
{
cout << *nodeptr;
check = 1;
}
nodeptr = nodeptr->next;
}
if (check == 0)
cout << "No Person found:" << endl;

friend istream& operator>>(istream& cin, phone_diary& diary);


friend ostream& operator<<(ostream& cout, phone_diary diary);
};

ostream& operator<<(ostream& cout, phone_diary diary)


{
cout << "Person Details:" << endl;
cout << "Name: " << diary.name << endl;
cout << "Phone#: " << diary.phone << endl;
cout << "Age: " << diary.age << endl;
return cout;
}
istream& operator>>(istream& cin, phone_diary& diary)
{
cout << "Person Details:" << endl;
cout << "Name: " << diary.name << endl;
cout << "Phone#: " << diary.phone << endl;
cout << "Age: " << diary.age << endl;
return cin;
}
int phone_diary::count = 0;
char phone_diary::is_undo = '\0 ';
int main()
{
phone_diary* dairy = NULL, Dairy;
char number = 0;
do {
cout << "1: Insert" << endl;
cout << "2: Reverse the list" << endl;
cout << "3: Delete" << endl;
cout << "4: Undo Action" << endl;
cout << "5: Display" << endl;
cout << "6: Search" << endl;
cout<< "Enter Number: ";
cin >> number;
system("CLS");
switch (number)
{
case'1':
{
cin >> Dairy;
dairy = dairy->insert(dairy, Dairy);
}
break;
case'2':
{
dairy->reversesort();
}
break;
case'3':
{
string Name;
cout << "Enter Name";
cin >> Name;
dairy = dairy->Delete(dairy, Dairy, Name);
}
break;
case'4':
dairy = dairy->UndoAction(dairy, Dairy);
break;
case'5':
dairy->Display();
break;
case'6':
dairy->searchtool();
break;
}
cout << "DO you want to do it again(y)/(N)"; cin >> number;
if (number == 'y' || number == 'Y')
system("CLS");
} while (number == 'y' || number == 'Y');
while (dairy != NULL)
{
nodeptr = dairy;
dairy = dairy->next;
free(nodeptr);
}
cout << endl << endl;
return 0;
}

Problem # 4:
Code:
#include<iostream>
#include<string>
using namespace std;

class Employee;
Employee* nodeptr, * head;

class Employee {
string name;
int age, ID;
Employee* next, * prev, * end;
public:
Employee() :name("\0"), age(0), ID(0) {}
Employee* Construct_list(int numberofnodes)
{
for (int loop = 0; loop < numberofnodes; loop++)
{
if (head == NULL)
{
nodeptr = head = new Employee;
cout << "Enter Name: ";
cin >> nodeptr->name;
cout << "Enter Age: ";
cin >> nodeptr->age;
cout << "Enter ID: ";
cin >> nodeptr->ID;
system("CLS");
nodeptr->prev = nodeptr->next = head;
}
else
{
nodeptr->next = new Employee;
nodeptr->next->prev = nodeptr;
nodeptr->next->next = head;
nodeptr = nodeptr->next;
head->prev = nodeptr;
cout << "Enter Name: ";
cin >> nodeptr->name;
cout << "Enter Age: ";
cin >> nodeptr->age;
cout << "Enter ID: ";
cin >> nodeptr->ID;
do {
cin >> nodeptr->ID;
Employee* check = head;
while (check->next != head)
{
if (nodeptr->ID == check->ID && check !=
nodeptr)
{
nodeptr->ID = 0;
break;
}
check = check->next;
}
if (nodeptr->ID == check->ID && check != nodeptr)
{
nodeptr->ID = 0;
}
if (nodeptr->ID == 0)
cout << "ID Matched:" << endl;
cout << "Enter Again: " << endl;
} while (nodeptr->ID == 0);
system("CLS");
}
}
return head;
}
Employee* leader(int numberofRemovedemp)
{
nodeptr = this;
while (nodeptr->next != nodeptr && nodeptr->prev != nodeptr)
{
for (int loop = 0; loop < numberofRemovedemp; loop++)
nodeptr = nodeptr->next;
end = nodeptr->next;
nodeptr->next = end->next;
end->next->prev = nodeptr;
cout << "Delete";
cout << *end << endl << endl;
free(end);
}
return nodeptr;
}
friend ostream& operator<<(ostream& cout, Employee emp);
};

ostream& operator<<(ostream& cout, Employee employ)


{
cout << " Person Detail:" << endl;
cout << " Name: " << employ.name << endl;
cout << "Age: " << employ.age << endl;
cout << "ID: " << employ.ID << endl;
return cout;
}

int main()
{
Employee* employe = NULL; int n = 0;
cout << "Enter Employe: ";
cin >> n;
employe = employe->Construct_list(n);
system("CLS");
cout << "Enter Employes to jump: ";
cin >> n;
employe = employe->leader(n);
system("pause");
system("CLS");
cout << "Leader";
cout << *employe << endl << endl;
free(employe);
return 0;
}

You might also like