1 answer

IN C++; A. Develop a class for a list, "" Develop the appropriate methods to insert...

Question:

IN C++;

A. Develop a class for a list, "" Develop the appropriate methods to insert and remove data." Describe the behavior of a list and what class you would use to extend to develop a list class".

B. Explain the difference between an array based and a linked based implementation. What are the advantages and disadvantages of each.

-I need to be explained step by step and the complete program to study in my exam.


Answers

#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;

// Class List definition
class List
{
public:
// Data member to store data
string name;
// Pointer to point to next node
List *next;

};// End of class

// Declares a start pointer and assigns null to it
List *start = NULL;

// Function to add a name
void addName()
{
string name;
// Creates a temporary pointer
List *temp = new List();

// Checks if temp is null then list is full
if(temp==NULL)
{
cout<<"\nOut of Memory Space:\n";
return;
}// End of if condition

cout<<"\n Enter a name to add: ";
cin>>temp->name;
// Assigns null to temp next
temp->next = NULL;
// Checks if the start is NULL then it is the first node to insert
if(start == NULL)
{
// temp address is assigned to start
start = temp;
}// End of if condition
// Otherwise
else
{
// start address is assigned to temp next
temp->next = start;
// temp address is assigned to start
start = temp;
}// End of else
}// End of function

// Function to display information
void displayNames()
{
// Creates a temporary pointer
List *ptr;
// Checks if start is null then list is empty
if(start == NULL)
{
cout<<"\nList is empty:\n";
return;
}// End of if condition
else
{
// Points to starting name
ptr = start;
cout<<"\n List of names \n";
// Loops till end of the list
while(ptr != NULL)
{
cout<<"\n Name: "<<ptr->name;
// Move to next name
ptr = ptr->next ;
}// End of while loop
}// End of else
}// End of function

// Function to search a name in the list
int searchName(string name)
{
int position = -1, counter = 0;
// Creates a temporary pointers
List *ptr;
// Checks if start is null then list is empty
if(start==NULL)
{
cout<<"\nThe List is Empty:\n";
return -1;
}// End of if condition

// Otherwise
else
{
// Points to starting name
ptr = start;
// Loops till end of the list
while(ptr != NULL)
{
// Checks if current name number is equals to parameter name
if(ptr->name.compare(name) == 0)
{
cout<<"\n Name "<<name<<" available.";
// Assigns the found position
position = counter;
// Returns the position
return counter;
}// End of if condition
// Move to next name
ptr=ptr->next ;
// Increase the record counter
counter++;
}// End of while loop
}// End of else

// Checks if found position is -1 name not found
if(position == -1)
cout<<"\n Name "<<name<<" not available.";
// returns the position
return position;
}// End of function

// Function to delete a node at specified position
void deleteName()
{
int position;
string name;
// Creates a temporary pointer
List *temp,*ptr;
// Checks if start is null then list is empty
if(start==NULL)
{
cout<<"\nThe List is Empty:\n";
return;
}// End of if condition
// Otherwise
else
{
// Accepts the name
cout<<"\nEnter the name: \t";
cin>>name;
// Calls the function to search the name
// Stores the returned found position
position = searchName(name);

// Checks if found position is -1 name not found
if(position == -1)
{
cout<<"\n No such "<<name<<" found to delete.";
return;
}// End of if condition

// Checks if the position is zero
if(position == 0)
{
// Temporary pointer points start
ptr = start;
// Start pointing to next node
start = start->next;
cout<<"\n "<<name<<" deleted from list.";
// Deletes the pointer
free(ptr);
}// End of if condition

// Otherwise
else
{
// Temporary pointer points start
ptr = start;

// Loops till record found position
for(int c = 0; c < position; c++)
{
// temporary pointer pointing to previous node ptr
temp = ptr;
// Move to next name
ptr = ptr->next;

// Checks if ptr is null node not found
if(ptr == NULL)
{
cout<<"\n Name: "<<name<<" not found in the list.\n";
return;
}// End of if condition
}// End of for loop
temp->next = ptr->next ;
cout<<"\n The deleted name: "<<name;

// Deletes the name node
free(ptr);
}// End of inner else
}// End of outer else
}// End of function

// Function to display user choice and return user choice
int menu()
{
// To store user choice
int choice;
// Displays menu

cout<<"\n LIST MENU \n";
cout<<"---------------------------------------\n";
cout<<"\n 1. Add Name.";
cout<<"\n 2. Delete name.";
cout<<"\n 3. Search name.";
cout<<"\n 4. Display all names.";
cout<<"\n 5.

Quit";
cout<<"\n--------------------------------------\n";
// Accepts user choice
cout<<"Enter your choice:\t";
cin>>choice;
// returns user choice
return choice;
}// End of function

// main function definition
int main()
{
string name;
// Loops till user choice is not 5
while(1)
{
// Calls the function to display menu.
// Calls the function as per user choice returned by the function
switch(menu())
{
case 1:
addName();
break;
case 2:
deleteName();
break;
case 3:
cout<<"\n Enter a name to search: ";
cin>>name;
searchName(name);
break;
case 4:
displayNames();
break;
case 5:
exit(0);
default:
cout<<"\n Wrong Choice:\n";
}//end of switch - case
}// End of while loop
return 0;
}//end of main function

Sample Output:


LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 4

List is empty:

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 3

Enter a name to search: Mohan

The List is Empty:

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 2

The List is Empty:

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 1

Enter a name to add: Pyari

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 1

Enter a name to add: Mohan

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 1

Enter a name to add: Sahu

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 1

Enter a name to add: Ram

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 2

Enter the name: sunita

Name sunita not available.
No such sunita found to delete.
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 2

Enter the name: Ram

Name Ram available.
Ram deleted from list.
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 4

List of names

Name: Sahu
Name: Mohan
Name: Pyari
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 3

Enter a name to search: Sita

Name Sita not available.
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 3

Enter a name to search: Pyari

Name Pyari available.
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 9

Wrong Choice:

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.

Quit
--------------------------------------
Enter your choice: 5

.

Similar Solved Questions

1 answer
An ecologist is monitoring a recently established population of wild lupine (a perennial plant). By carefully...
An ecologist is monitoring a recently established population of wild lupine (a perennial plant). By carefully mapping, tagging, and censusing the entire population of plants for one entire reproductive cycle (June of one year to June of the next year) the ecologist obtained the following data, use t...
1 answer
An example of this is instruction/data stream is two or more forked processing running on an...
An example of this is instruction/data stream is two or more forked processing running on an Intel i7. SIMD MISD SISD MIMD QUESTION 5 In fine-grained multithreading, a thread continues until it stalls, then it switches to another thread. True False QUESTION 6 In a multithreaded system, a process can...
1 answer
Your friend, another accountant, has bet you that with your knowledge of accounting and just the...
Your friend, another accountant, has bet you that with your knowledge of accounting and just the calculations for common analytical measures, you can figure out many aspects of a company's financial statements. You take the bet! Match each calculation to one of the liquidity and solvency measure...
1 answer
Use the References to access important values if needed for this question. constructed in which a...
Use the References to access important values if needed for this question. constructed in which a Cd2+ | Cd half cell acts as the cathode. Which of the following statements are correct? A standard galvanic cell Hint: Refer to a table of standard reduction potentials (Choose all that apply.) As the c...
1 answer
Problem #3 Provide the full IUPAC name for the following molecule. H3C
Problem #3 Provide the full IUPAC name for the following molecule. H3C...
1 answer
What are the asymptotes for #y=-4/(x+2)# and how do you graph the function?
What are the asymptotes for #y=-4/(x+2)# and how do you graph the function?...
1 answer
X Company uses the high-low method to predict monthly overhead costs. The following were May and...
X Company uses the high-low method to predict monthly overhead costs. The following were May and September cost and activity results: May September OH Cost $6,709 $11,077 Production 2,300 5,100 If December production is expected to be 4,000 units, what are expected total fixed overhead costs in Dece...
1 answer
Light is entering a polarizer or set of polarizers from the left. Determine the relative intensity...
Light is entering a polarizer or set of polarizers from the left. Determine the relative intensity of the light exiting on the right after passing through all of the polarizers. The dashed vertical lines represent the vertical reference. The solid lines represent either the polarization angle of the...
1 answer
Subject: HRM Introduction and Instructions You have recently been hired as the Director of Human Resources...
Subject: HRM Introduction and Instructions You have recently been hired as the Director of Human Resources for Wilson Brothers Canada and have HR responsibility for all of the company’s Canadian operations. Bob and John Wilson have asked you to prepare a report for their review focusin...
1 answer
Add EX ALU Add dresult Shift left 2 Regst Branch MomRoad Instruction (31-26) MemtoReg Control ALUOO...
Add EX ALU Add dresult Shift left 2 Regst Branch MomRoad Instruction (31-26) MemtoReg Control ALUOO MemWrite ALUST RegWrite instruction [25-21] Read register 1 Read instruction (20-16) Read data 1 register 2 Write Read data 2 instruction (15-11) register Write data Registers Read address Zero ALU AL...
1 answer
The following information is available for Market, Inc. and Supply, Inc. at December 31: Accounts Accounts...
The following information is available for Market, Inc. and Supply, Inc. at December 31: Accounts Accounts receivable Allowance for doubtful accounts Sales revenue Market, Inc. $ 59, 280 2,448 616,960 Supply, Inc. $ 77,200 2,456 917,100 Required a. What is the accounts receivable turnover for each o...
1 answer
Question 4 1 pts In one study we talked about in class, researchers Webb, Cambell, Schwartz,...
Question 4 1 pts In one study we talked about in class, researchers Webb, Cambell, Schwartz, and Sechrest (2000) wanted to measure patrons' interest in museum exhibits when there were no patrons around to observe. One measure they used was looking at the number of nose-print marks on the glass o...
1 answer
1. What is the ER/PR and HER2 expression profile of tumors that might benefit from PIQRAY...
1. What is the ER/PR and HER2 expression profile of tumors that might benefit from PIQRAY treatment?...