Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ In this project, you will write the implementation of the PeopleList using a

ID: 3869025 • Letter: C

Question

c++

In this project, you will write the implementation of the PeopleList using a doubly linked list, which should be sorted alphabetically according to last name, then first name. You will also implement a couple of algorithms that operate on a PeopleList.

Implement PeopleList

Consider the following PeopleList interface:

The add function primarily places elements so that they are sorted in the list based on last name. If there are multiple entries with the same last name then those elements, with the same last name, are added so that they are sorted by their first name. In other words, this code fragment

must result in the output:

Notice that the empty string is just as good a string as any other; you should not treat it in any special way:

When comparing keys for add, change, addOrChange, remove, contains, and lookup, just use the == or != operators provided for the string type by the library. These do case-sensitive comparisons, and that's fine.

For this project, implement this PeopleList interface using a doubly-linked list. (You must not use any container from the C++ library.)

For your implementation, if you let the compiler write the destructor, copy constructor, and assignment operator, they will do the wrong thing, so you will have to declare and implement these public member functions as well:

Destructor

When a PeopleList is destroyed, all dynamic memory must be deallocated.

Copy constructor

When a brand new PeopleList is created as a copy of an existing PeopleList, a deep copy should be made.

Assignment operator

When an existing PeopleList (the left-hand side) is assigned the value of another PeopleList (the right-hand side), the result must be that the left-hand side object is a duplicate of the right-hand side object, with no memory leak (i.e. no memory from the old value of the left-hand side should be still allocated yet inaccessible).

Notice that there is no a priori limit on the maximum number of elements in the PeopleList (so addOrChange should always return true). Also, if a PeopleList has a size of n, then the values of the first parameter to the get member function are 0, 1, 2, ..., n-1; for other values, it returns false without setting its parameters.

Implement some non member functions

Using only the public interface of PeopleList, implement the following two functions. (Notice that they are non-member functions; they are not members of PeopleList or any other class.)

bool combine(const PeopleList& m1, const PeopleList& m2, PeopleList& result);

When this function returns, result must consist of pairs determined by these rules:

If a full name appears in exactly one of m1 and m2, then result must contain an element consisting of that full name and its corresponding value.

If a full name appears in both m1 and m2, with the same corresponding value in both, then result must contain an element with that full name and value.

When this function returns, result must contain no elements other than those required by these rules. (You must not assume result is empty when it is passed in to this function; it might not be.)

If there exists a full name that appears in both m1 and m2, but with different corresponding values, then this function returns false; if there is no full name like this, the function returns true. Even if the function returns false, result must be constituted as defined by the above rules.

For example, suppose a PeopleList maps the full name to integers. If m1 consists of these three elements

and m2 consists of

then no matter what value it had before, result must end up as a list consisting of

and combine must return true. If instead, m1 were as before, and m2 consisted of

then no matter what value it had before, result must end up as a list consisting of

and combine must return false.

When this function returns, result must contain a copy of all the elements in m1 that match the search terms; it must not contain any other elements. You can wildcard the first name, last name or both by supplying "*". (You must not assume result is empty when it is passed in to this function; it may not be.)

For example, if p consists of the three elements

and the following call is made:

then no matter what value it had before, result must end up as a PeopleList consisting of

If instead, p1 were

and the following call is made:

then no matter what value it had before, result must end up as a list consisting of

and if the following call is made:

then no matter what value it had before, result must end up being a copy of p

Be sure these functions behave correctly in the face of aliasing: What if m1 and result refer to the same PeopleList, for example?

Other Requirements

Regardless of how much work you put into the assignment, your program will receive a low score for correctness if you violate these requirements:

Your class definition, declarations for the two required non-member functions, and the implementations of any functions you choose to inline must be in a file named PeopleList.h, which must have appropriate include guards. The implementations of the functions you declared in PeopleList.h that you did not inline must be in a file named PeopleList.cpp. Neither of those files may have a main routine (unless it's commented out). You may use a separate file for the main routine to test your PeopleList class; you won't turn in that separate file.

Except to add a destructor, copy constructor, assignment operator, and dump function (described below), you must not add functions to, delete functions from, or change the public interface of the PeopleList class. You must not declare any additional struct/class outside the PeopleList class, and you must not declare any public struct/class inside the PeopleList class. You may add whatever private data members and private member functions you like, and you may declare private structs/classes inside the PeopleList class if you like. The source files you submit for this project must not contain the word friend. You must not use any global variables whose values may be changed during execution.

If you wish, you may add a public member function with the signature void dump() const. The intent of this function is that for your own testing purposes, you can call it to print information about the map; we will never call it. You do not have to add this function if you don't want to, but if you do add it, it must not make any changes to the map; if we were to replace your implementation of this function with one that simply returned immediately, your code must still work correctly. The dump function must not write to cout, but it's allowed to write to cerr.

Your code must build successfully (under both g32 and either clang++ or Visual C++) if linked with a file that contains a main routine.

You must have an implementation for every member function of PeopleList, as well as the non-member functions combine and psearch. Even if you can't get a function implemented correctly, it must have an implementation that at least builds successfully. For example, if you don't have time to correctly implement PeopleList::remove or psearch, say, here are implementations that meet this requirement in that they at least build successfully:

You've probably met this requirement if the following file compiles and links with your code. (This uses magic beyond the scope of CS 32.)

If you add #include to PeopleList.h, have the typedef define InfoType as std::string, and link your code to a file containing

the linking must succeed. When the resulting executable is run, it must write Passed all tests to cout and nothing else to cout.

If we successfully do the above, then make no changes to PeopleList.h other than to change the typedefs for PeopleList so that InfoType specifies int, recompile PeopleList.cpp, and link it to a file containing

the linking must succeed. When the resulting executable is run, it must write Passed all tests to cout and nothing else to cout.

During execution, if a client performs actions whose behavior is defined by this spec, your program must not perform any undefined actions, such as dereferencing a null or uninitialized pointer.

Your code in PeopleList.h and PeopleList.cpp must not read anything from cin and must not write anything whatsoever to cout. If you want to print things out for debugging purposes, write to cerr instead of cout. cerr is the standard error destination; items written to it by default go to the screen. When we test your program, we will cause everything written to cerr to be discarded instead — we will never see that output, so you may leave those debugging output statements in your program if you wish.

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

struct node

{

struct node *prev;

int n;

struct node *next;

}*h,*temp,*temp1,*temp2,*temp4;

void insert1();

void insert2();

void insert3();

void traversebeg();

void traverseend(int);

void sort();

void search();

void update();

void delete();

int count = 0;

void main()

{

int ch;

h = NULL;

temp = temp1 = NULL;

printf(" 1 - Insert at beginning");

printf(" 2 - Insert at end");

printf(" 3 - Insert at position i");

printf(" 4 - Delete at i");

printf(" 5 - Display from beginning");

printf(" 6 - Display from end");

printf(" 7 - Search for element");

printf(" 8 - Sort the list");

printf(" 9 - Update an element");

printf(" 10 - Exit");

while (1)

{

printf(" Enter choice : ");

scanf("%d", &ch);

switch (ch)

{

case 1:

insert1();

break;

case 2:

insert2();

break;

case 3:

insert3();

break;

case 4:

delete();

break;

case 5:

traversebeg();

break;

case 6:

temp2 = h;

if (temp2 == NULL)

printf(" Error : List empty to display ");

else

{

printf(" Reverse order of linked list is : ");

traverseend(temp2->n);

}

break;

case 7:

search();

break;

case 8:

sort();

break;

case 9:

update();

break;

case 10:

exit(0);

default:

printf(" Wrong choice menu");

}

}

}

/* TO create an empty node */

void create()

{

int data;

temp =(struct node *)malloc(1*sizeof(struct node));

temp->prev = NULL;

temp->next = NULL;

printf(" Enter value to node : ");

scanf("%d", &data);

temp->n = data;

count++;

}

/* TO insert at beginning */

void insert1()

{

if (h == NULL)

{

create();

h = temp;

temp1 = h;

}

else

{

create();

temp->next = h;

h->prev = temp;

h = temp;

}

}

/* To insert at end */

void insert2()

{

if (h == NULL)

{

create();

h = temp;

temp1 = h;

}

else

{

create();

temp1->next = temp;

temp->prev = temp1;

temp1 = temp;

}

}

/* To insert at any position */

void insert3()

{

int pos, i = 2;

printf(" Enter position to be inserted : ");

scanf("%d", &pos);

temp2 = h;

if ((pos < 1) || (pos >= count + 1))

{

printf(" Position out of range to insert");

return;

}

if ((h == NULL) && (pos != 1))

{

printf(" Empty list cannot insert other than 1st position");

return;

}

if ((h == NULL) && (pos == 1))

{

create();

h = temp;

temp1 = h;

return;

}

else

{

while (i < pos)

{

temp2 = temp2->next;

i++;

}

create();

temp->prev = temp2;

temp->next = temp2->next;

temp2->next->prev = temp;

temp2->next = temp;

}

}

/* To delete an element */

void delete()

{

int i = 1, pos;

printf(" Enter position to be deleted : ");

scanf("%d", &pos);

temp2 = h;

if ((pos < 1) || (pos >= count + 1))

{

printf(" Error : Position out of range to delete");

return;

}

if (h == NULL)

{

printf(" Error : Empty list no elements to delete");

return;

}

else

{

while (i < pos)

{

temp2 = temp2->next;

i++;

}

if (i == 1)

{

if (temp2->next == NULL)

{

printf("Node deleted from list");

free(temp2);

temp2 = h = NULL;

return;

}

}

if (temp2->next == NULL)

{

temp2->prev->next = NULL;

free(temp2);

printf("Node deleted from list");

return;

}

temp2->next->prev = temp2->prev;

if (i != 1)

temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */

if (i == 1)

h = temp2->next;

printf(" Node deleted");

free(temp2);

}

count--;

}

/* Traverse from beginning */

void traversebeg()

{

temp2 = h;

if (temp2 == NULL)

{

printf("List empty to display ");

return;

}

printf(" Linked list elements from begining : ");

while (temp2->next != NULL)

{

printf(" %d ", temp2->n);

temp2 = temp2->next;

}

printf(" %d ", temp2->n);

}

/* To traverse from end recursively */

void traverseend(int i)

{

if (temp2 != NULL)

{

i = temp2->n;

temp2 = temp2->next;

traverseend(i);

printf(" %d ", i);

}

}

/* To search for an element in the list */

void search()

{

int data, count = 0;

temp2 = h;

if (temp2 == NULL)

{

printf(" Error : List empty to search for data");

return;

}

printf(" Enter value to search : ");

scanf("%d", &data);

while (temp2 != NULL)

{

if (temp2->n == data)

{

printf(" Data found in %d position",count + 1);

return;

}

else

temp2 = temp2->next;

count++;

}

printf(" Error : %d not found in list", data);

}

/* To update a node value in the list */

void update()

{

int data, data1;

printf(" Enter node data to be updated : ");

scanf("%d", &data);

printf(" Enter new data : ");

scanf("%d", &data1);

temp2 = h;

if (temp2 == NULL)

{

printf(" Error : List empty no node to update");

return;

}

while (temp2 != NULL)

{

if (temp2->n == data)

{

temp2->n = data1;

traversebeg();

return;

}

else

temp2 = temp2->next;

}

printf(" Error : %d not found in list to update", data);

}

/* To sort the linked list */

void sort()

{

int i, j, x;

temp2 = h;

temp4 = h;

if (temp2 == NULL)

{

printf(" List empty to sort");

return;

}

for (temp2 = h; temp2 != NULL; temp2 = temp2->next)

{

for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)

{

if (temp2->n > temp4->n)

{

x = temp2->n;

temp2->n = temp4->n;

temp4->n = x;

}

}

}

traversebeg();

}

#include<iostream>

#include<cstdio>

#include<cstdlib>

/*

* Node Declaration

*/

using namespace std;

struct node

{

int info;

struct node *next;

struct node *prev;

}*start;

/*

Class Declaration

*/

class double_llist

{

public:

void create_list(int value);

void add_begin(int value);

void add_after(int value, int position);

void delete_element(int value);

void search_element(int value);

void display_dlist();

void count();

void reverse();

double_llist()

{

start = NULL;

}

};

/*

* Main: Conatins Menu

*/

int main()

{

int choice, element, position;

double_llist dl;

while (1)

{

cout<<endl<<"----------------------------"<<endl;

cout<<endl<<"Operations on Doubly linked list"<<endl;

cout<<endl<<"----------------------------"<<endl;   

cout<<"1.Create Node"<<endl;

cout<<"2.Add at begining"<<endl;

cout<<"3.Add after position"<<endl;

cout<<"4.Delete"<<endl;

cout<<"5.Display"<<endl;

cout<<"6.Count"<<endl;

cout<<"7.Reverse"<<endl;

cout<<"8.Quit"<<endl;

cout<<"Enter your choice : ";

cin>>choice;

switch ( choice )

{

case 1:

cout<<"Enter the element: ";

cin>>element;

dl.create_list(element);

cout<<endl;

break;

case 2:

cout<<"Enter the element: ";

cin>>element;

dl.add_begin(element);

cout<<endl;

break;

case 3:

cout<<"Enter the element: ";

cin>>element;

cout<<"Insert Element after postion: ";

cin>>position;

dl.add_after(element, position);

cout<<endl;

break;

case 4:

if (start == NULL)

{

cout<<"List empty,nothing to delete"<<endl;   

break;

}

cout<<"Enter the element for deletion: ";

cin>>element;

dl.delete_element(element);

cout<<endl;

break;

case 5:

dl.display_dlist();

cout<<endl;

break;

case 6:

dl.count();

break;

case 7:

if (start == NULL)

{

cout<<"List empty,nothing to reverse"<<endl;

break;

}

dl.reverse();

cout<<endl;

break;

case 8:

exit(1);

default:

cout<<"Wrong choice"<<endl;

}

}

return 0;

}

/*

* Create Double Link List

*/

void double_llist::create_list(int value)

{

struct node *s, *temp;

temp = new(struct node);

temp->info = value;

temp->next = NULL;

if (start == NULL)

{

temp->prev = NULL;

start = temp;

}

else

{

s = start;

while (s->next != NULL)

s = s->next;

s->next = temp;

temp->prev = s;

}

}

/*

* Insertion at the beginning

*/

void double_llist::add_begin(int value)

{

if (start == NULL)

{

cout<<"First Create the list."<<endl;

return;

}

struct node *temp;

temp = new(struct node);

temp->prev = NULL;

temp->info = value;

temp->next = start;

start->prev = temp;

start = temp;

cout<<"Element Inserted"<<endl;

}

/*

* Insertion of element at a particular position

*/

void double_llist::add_after(int value, int pos)

{

if (start == NULL)

{

cout<<"First Create the list."<<endl;

return;

}

struct node *tmp, *q;

int i;

q = start;

for (i = 0;i < pos - 1;i++)

{

q = q->next;

if (q == NULL)

{

cout<<"There are less than ";

cout<<pos<<" elements."<<endl;

return;

}

}

tmp = new(struct node);

tmp->info = value;

if (q->next == NULL)

{

q->next = tmp;

tmp->next = NULL;

tmp->prev = q;

}

else

{

tmp->next = q->next;

tmp->next->prev = tmp;

q->next = tmp;

tmp->prev = q;

}

cout<<"Element Inserted"<<endl;

}

/*

* Deletion of element from the list

*/

void double_llist::delete_element(int value)

{

struct node *tmp, *q;

/*first element deletion*/

if (start->info == value)

{

tmp = start;

start = start->next;

start->prev = NULL;

cout<<"Element Deleted"<<endl;

free(tmp);

return;

}

q = start;

while (q->next->next != NULL)

{   

/*Element deleted in between*/

if (q->next->info == value)

{

tmp = q->next;

q->next = tmp->next;

tmp->next->prev = q;

cout<<"Element Deleted"<<endl;

free(tmp);

return;

}

q = q->next;

}

/*last element deleted*/

if (q->next->info == value)

{

tmp = q->next;

free(tmp);

q->next = NULL;

cout<<"Element Deleted"<<endl;

return;

}

cout<<"Element "<<value<<" not found"<<endl;

}

/*

* Display elements of Doubly Link List

*/

void double_llist::display_dlist()

{

struct node *q;

if (start == NULL)

{

cout<<"List empty,nothing to display"<<endl;

return;

}

q = start;

cout<<"The Doubly Link List is :"<<endl;

while (q != NULL)

{

cout<<q->info<<" <-> ";

q = q->next;

}

cout<<"NULL"<<endl;

}

/*

* Number of elements in Doubly Link List

*/

void double_llist::count()

{

struct node *q = start;

int cnt = 0;

while (q != NULL)

{

q = q->next;

cnt++;

}

cout<<"Number of elements are: "<<cnt<<endl;

}

/*

* Reverse Doubly Link List

*/

void double_llist::reverse()

{

struct node *p1, *p2;

p1 = start;

p2 = p1->next;

p1->next = NULL;

p1->prev = p2;

while (p2 != NULL)

{

p2->prev = p2->next;

p2->next = p1;

p1 = p2;

p2 = p2->prev;

}

start = p1;

cout<<"List Reversed"<<endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote