Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following questions are based on a linked list that store Person structure type. Refer to folder Part1Q2 for the codes. Create a list list3

  1. The following questions are based on a linked list that store Person structure type. Refer to folder Part1Q2 for the codes.
  2. Create a list list3 to store object Person define in Person.h.
  3. Create an array of 5 elements to store Person object with the following values:

{{"Julie", "3546778", 2000}, {"Robert", "7822113", 6000}, {"Thomas", "5006791", 5000}, {"Kathy", "5568009", 3000}, {"Lana", "7331168", 8000}}

  1. Insert the person object in the array to list3 using bool insert(type) (To use this inset, you must have implemented function compareName1 to compare 2 person name using >= operator in struct Person). Start insert from the first element in the array.
  2. Write a function bool printList(List) in main to print all the nodes in list that store Person object. Use the printList function to print the list3 after insert items in (c).
  3. Write statement to find person in node 3 and print out the information for this person if found.
  4. Two objects person p3 and p4 are declared in the main. Write a function bool Redundant(List, type) to check if an item already exist in a list. Assume name is unique and use it to compare if the person exist already. This function is useful if we do not want to insert redundant item to the same list. You need to write the function compareName2 in struct Person to compare name using == operator.

app.cpp

#include #include #include "List.h" #include "Person.h"

using namespace std;

bool printList(List); bool Redundant(List, Person);

int main() { Person p3 = {"Robert", "7822113", 6000}; Person p4 = {"John", "6550011", 7000};

cout << " ****************************(a), (b) and (c)******************************* ";

cout << " ********************************(e)************************************** ";

cout << " ********************************(f)************************************** "; system("pause"); return 0; } List.cpp

#include #include "List.h" #include "Person.h"

using namespace std;

//typedef Person type;

using type = Person;

List::List() { head = NULL; count = 0; }

bool List::empty() { if (count==0) return true; return false; }

int List::size() { return count; }

Node *List::find(int position) { Node *cur; if (position > count) return NULL; cur = head; for (int count=1; countnext; return cur; }

bool List::get(int position, type &result) { if (position > count) return false; result = find(position)->item; return true; }

bool List::set(int position, type newItem) { if (position > count) return false; find(position)->item = newItem; return true; }

bool List::insert(int at, type newItem) {// Any simplification can be done on code below? Node *pre, *cur, *tmp = new Node(newItem);

if (at < 1 || at > count+1) return false; if (!tmp) return false;

if (empty()) { head = tmp; count++; return true; } if (at == 1) { tmp->next = head; head = tmp; count++; return true; } pre = find(at-1); cur = pre->next; tmp->next = cur; pre->next = tmp; count++; return true; }

bool List::remove(int from) { Node *pre, *cur;

if (from < 1 || from > count) return false; if (from == 1) { cur = head; head = head->next; count--; free(cur); return true; } pre = find(from-1); cur = pre->next; pre->next = cur->next; free(cur); count--; return true; }

//insert in ascending order - user-defined type bool List::insert(type newItem) { Node *pre, *cur, *tmp;

tmp = new Node(newItem);

if (!tmp) return false; if (empty()) { head = tmp; count++; return true; } count++; if (head->item.compareName1(newItem)) { tmp->next = head; head = tmp; return true; } pre = head; cur = pre->next; for (; cur != NULL;) { if (cur->item.compareName1(newItem)) break; pre = cur; cur = cur->next; } tmp->next = cur; pre->next = tmp; return true; }

Node.cpp

#include #include "Node.h" #include "Person.h"

//typedef Person type;

using type = Person;

Node::Node(type newItem) { item = newItem; next = NULL; }

Person.cpp

#include #include "Person.h"

//bool Person::operator>= (Person p2) { bool Person::compareName1(Person p2){ if (strcmp(name, p2.name) >= 0) return true; return false; }

//bool Person::operator== (Person p2) { bool Person::compareName2(Person p2){ if (strcmp(name, p2.name) == 0) return true; return false; }

void Person::print() { cout << " Name: " << name; cout << " Phone: " << phone; cout << " Money: " << money;

cout << " ";

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions