really struggling with this one :( Write an object oriented C++ program to creat
ID: 3583470 • Letter: R
Question
really struggling with this one :(
Write an object oriented C++ program to create a doubly linked. Each node should be able to store a person's first and last names, along with their respective age. Initialize the list with 5 nodes. The program should include a member function that allows the user to print out all the nodes whose age is less than a value input by the user (in either direction). It should also allow the user to search for a particular person's name. It should be able to output the lists average age. Finally it should allow for a name to be removed. The program should allow the user to take choices from a menu.
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
typedef string Elem; // list element type
class DNode { // doubly linked list node
private:
Elem elem; // node element value
Elem age;
DNode* prev; // previous node in list
DNode* next; // next node in list
friend class DLinkedList; // allow DLinkedList access
};
class DLinkedList { // doubly linked list
public:
DLinkedList();
~DLinkedList();
bool empty() const;
const Elem& front() const; // get front element
const Elem& back() const; // get back element
void addFront(const Elem& name, const Elem& age); // add to front of list
void addBack(const Elem& name, const Elem& age); // add to back of list
void removeFront(); // remove from front
void displayViaAge(const Elem& age);
void displayViaName(const Elem& name);
void average();
private:
DNode* header;
DNode* trailer;
protected:
void add(DNode* v, const Elem& name, const Elem& age); // insert new node before v
void remove(DNode* v); // remove node v
};
DLinkedList::DLinkedList() { // constructor
header = new DNode;
trailer = new DNode;
header->next = trailer;
trailer->prev = header;
}
DLinkedList::~DLinkedList() { // destructor
while (!empty()) removeFront(); // remove all but sentinels
delete header; // remove the sentinels
delete trailer;
}
// insert new node before v
void DLinkedList::add(DNode* v, const Elem& name, const Elem& age) {
DNode* u = new DNode;
u->elem = name; //assign name to elem
u->age = age; //assign age to element age
u->next = v;
u->prev = v->prev;
v->prev->next = u;
v->prev = u;
}
void DLinkedList::addFront(const Elem& name, const Elem& age) // add to front of list
{ add(header->next, name, age); }
void DLinkedList::addBack(const Elem& name, const Elem& age) // add to back of list
{ add(trailer, name, age); }
void DLinkedList::remove(DNode* v) { // remove node v
DNode* u = v->prev;
DNode* w = v->next;
u->next = w; // unlink v from list
w->prev = u;
delete v;
}
void DLinkedList::removeFront() // remove from font
{ remove(header->next); }
bool DLinkedList::empty() const // is list empty?
{ return (header->next == trailer); }
const Elem& DLinkedList::front() const // get front element
{ return header->next->elem; }
const Elem& DLinkedList::back() const // get back element
{ return trailer->prev->elem; }
void DLinkedList::displayViaAge(const Elem& age) { //Displays person name & age via age
DNode*temp = header->next;
int flag = 0;
while(temp!=trailer)
{
if(temp->age < age)
{
cout << temp->elem << " " << temp->age << endl;
flag = 1;
}
temp = temp -> next;
}
if (flag == 0)
{
cout << "None of the age is lesser than the user input age" <<endl;
}
}
void DLinkedList::displayViaName(const Elem& name) { //Displays person name via name
DNode*temp = header->next;
int flag = 0;
while(temp!=trailer)
{
if(temp->elem == name)
{
flag = 1;
cout << "Entered name is found in the list" <<endl;
return;
}
temp = temp -> next;
}
if (flag == 0)
{
cout << "Entered name is not found in the list" <<endl;
}
}
void DLinkedList::average() { //calculates average of all age
DNode* temp = header->next;
int sum = 0;
int count = 0;
int avg = 0;
while (temp != trailer) {
sum = stoi(temp->age) + sum;
count++;
temp = temp->next;
}
avg = sum/count;
cout << avg << endl;
}
int main(){
int input = 0;
string entry;
string age;
DLinkedList person;
person.addFront("Akash", "25");
person.addBack("Aparna", "26");
person.addBack("Ashika", "27");
person.addBack("Supriya", "28");
person.addBack("Yash", "29");
while (input !=6)
{
cout << "What would you like to do?" << endl;
cout << "Enter 1 to: Add a new person" << endl;
cout << "Enter 2 to: Remove a person" << endl;
cout << "Enter 3 to: Search for people via age" << endl;
cout << "Enter 4 to: Search for people via name" << endl;
cout << "Enter 5 to: Average all the total ages" << endl;
cout << "Enter 6 to: Quit" << endl;
cin >> input;
cout << endl;
switch (input)
{
case 1:
cout << "Please enter name: ";
cin.ignore();
getline(cin, entry);
cout << "Please enter their age: ";
cin >> age;
person.addFront(entry, age);
break;
case 2:
cout << "Who would you like to remove: ";
cin.ignore();
getline(cin, entry);
person.removeFront();
break;
case 3:
cout << "What is the age of the person you are looking for?: ";
cin.ignore();
getline(cin, entry);
person.displayViaAge(entry);
break;
case 4:
cout << "What is the name of the person you are looking for?: ";
cin.ignore();
getline(cin, entry);
person.displayViaName(entry);
break;
case 5:
person.average();
break;
default:
break;
}
cout << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.