Question

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

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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

Add a comment
Know the answer?
Add Answer to:
IN C++; A. Develop a class for a list, "" Develop the appropriate methods to insert...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C++; Develop a class for a queue. "Develop the appropriate methods to insert and remove...

    IN C++; Develop a class for a queue. "Develop the appropriate methods to insert and remove data". -I need to be explained step by step and the complete program to study in my exam.

  • Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have tw...

    Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have two state variables namely data and nextNode. The doubly list class should contain the following methods: Middlelnsert- insert a node somewhere in the middle of the list Startinsert-insert a node at start of the Linked list Endinsert- insert a node at the end of the Linked list Delete-delete a node Traverse-prints all the node's data Reverse-reverses the linked list . . Note: Choose...

  • Exercise-1:15 points Develop a node class and a singly list class. The node class should have two...

    Exercise-1:15 points Develop a node class and a singly list class. The node class should have two state variables namely data and nextNode. The singly list class should contain the following methods . Middlelnsert-insert a node somewhere in the middle of the list . Startinsert-insert a node at start of the Linked list Endinsert-insert a node at the end of the Linked list . Delete-delete a node Traverse-prints all the node's data Reverse -reverses the linked list Note: Choose appropriate...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

  • Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due...

    Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due Friday, February 9th, 2017 @ 11:59PM EST Directions Create a List object. Using the following definition (List.h file is also in the repository for your convenience) for a list, implement the member functions (methods) for the List class and store the implementation in a file called List.cpp. Use an array to implement the list. Write the client code (the main method and other non-class...

  • Using the following definition (List.h file) for a list, implement the member functions (methods) for the...

    Using the following definition (List.h file) for a list, implement the member functions (methods) for the List class and store the implementation in a List.cpp file. Use a doubly linked list to implement the list. Write the client code (the main method and other non-class methods) and put in file driver.cpp. file: List.h typedef int ElementType; class node{ ​ElementType data; ​node * next; node* prev; }; class List { public: List(); //Create an empty list bool Empty(); // return true...

  • This exercise is to exam the dynamic behavior of the working pointers the following Methods: Add...

    This exercise is to exam the dynamic behavior of the working pointers the following Methods: Add(item) and Remove(item) for LInkedQueue push(item), pop(item) and top() for LinkedStack Place an A4 paper in landscape orientation, draw the illustration on the left half and write the corresponding C++ code directly to its right. The main focus of the Illustration is to show the relationship between the list elements with the working pointers in the program. Group your illustration in (test) scenarios of empty...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT