Question

In: Computer Science

Create an ADT class that creates a friend contact list. The program should be able to...

Create an ADT class that creates a friend contact list. The program should be able to add, remove and view the contacts. In C++

Solutions

Expert Solution

#include<iostream>
#include<cstdlib>
#include<string.h>
using namespace std;
//contact class
class contact
{
   public:
   //data members
   char name[30];//name
   char phone[30];//phone number  
   //add any details you want
   //pointer to next contact
   contact *next;
   //constructor
   contact()
   {
       cout<<"Enter name :";
       string n;
       getline(cin,n);//reading name
       int i=0;
       for(i=0;n[i]!='\0';i++)name[i]=n[i];
       name[i]='\0';
       cout<<"Enter phone number: ";
       string p;
       getline(cin,p);  
       for(i=0;p[i]!='\0';i++)phone[i]=p[i];
       phone[i]='\0';
       next=NULL;
   }
  
};
//friend contact list class
class contact_list
{
   contact *list;
  
   public:
   //contstructor
   contact_list()
   {
       list=NULL;  
   }
   //method to add contact
   void Add()
   {
       contact *n = new contact();
      
       if(list==NULL)list=n;
       else
       {
           n->next = list;
           list = n;  
       }
      
          
   }
  
   //method to remove contact
   void Remove()
   {
       string nam;
       //reading name to remove
       cout<<"Enter name to remove :";
       getline(cin,nam);
       char n[30];
       for(int i=0;nam[i]!='\0';i++)n[i]=nam[i];
      
      
       contact *temp = list;
       contact *prev= NULL;
       while(temp!=NULL)
       {
           if(strcmp(n,temp->name)==0)//means name is found
           break;
           prev=temp;
           temp=temp->next;  
       }
      
       if(temp==NULL)
       {
           cout<<"Name not found in contact list\n";  
       }  
       else
       {
           if(prev==NULL)
           list=list->next;
           else
           {
               prev->next=temp->next;  
           }
       }
   }
  
   //method to display all contacts
  
   void Display()
   {
      
       contact *temp = list;
      
       while(temp!=NULL)
       {
           cout<<"Name :"<<temp->name<<endl;
           cout<<"Phone number :"<<temp->phone<<endl;
           cout<<endl;
           temp=temp->next;  
       }
          
   }
      
  
  
};
int main()
{
   contact_list *list= new contact_list();;
   //testing
   int c;
   while(1)
   {
       cout<<"1:Add contact\n2:Remove contact\n3:View contacts\n4:Exit\nEnter :";
       cin>>c;
       if(c==1)
       {string cc;
       getline(cin,cc);//reading empty line
      
      
       list->Add();
          
       }
       else if(c==2)
       {string cc;
       getline(cin,cc);//reading empty line
       list->Remove();
      
       }
       else if(c==3)
       {
           string cc;
       getline(cin,cc);
       list->Display();
  
       }
       else if(c==4)
       break;
          
   }
  
  
   return 0;
}

output:

1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :1
Enter name :Surya
Enter phone number: 9887654322
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :3
Name :Surya
Phone number :9887654322

1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :1
Enter name :phane
Enter phone number: 23456789
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :3
Name :phane
Phone number :23456789

Name :Surya
Phone number :9887654322

1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :2
Enter name to remove :phane
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :3
Name :Surya
Phone number :9887654322

1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :2
Enter name to remove :asdf
Name not found in contact list
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :3
Name :Surya
Phone number :9887654322

1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :1
Enter name :Hello
Enter phone number: 123456789
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :3
Name :Hello
Phone number :123456789

Name :Surya
Phone number :9887654322

1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :4


Process exited normally.
Press any key to continue . . .


//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.


Related Solutions

Create an unsorted LIST class. Each list should be able to store 100 names.
Create an unsorted LIST class. Each list should be able to store 100 names.
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the user to input a few lines of text and then outputs strings in same order of entry. (use of the library ArrayDeque) In Java please.
Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. In Java please.
First, create a project that creates an ordered linked list class (use the code provided, make...
First, create a project that creates an ordered linked list class (use the code provided, make sure to update the insert function so that it maintains an ordered list). To this class, add a function that prints the list in REVERSE order. Making use of recursion is the easiest and best way to do this, but certainly not the only way. Submit a .zip of your entire project.
Create the following java program with class list that outputs: //output List Empty List Empty List...
Create the following java program with class list that outputs: //output List Empty List Empty List Empty Item not found Item not found Item not found Original list Do or do not. There is no try. Sorted Original List Do There do is no not. or try. Front is Do Rear is try. Count is 8 Is There present? true Is Dog present? false List with junk junk Do or moremorejunk do not. There is no try. morejunk Count is...
Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
Write a program that creates a Singleton class to connect to two databases. The program must...
Write a program that creates a Singleton class to connect to two databases. The program must provide two instances of this class. One instance for connecting to MySQL database and the other for connecting to Oracle database.
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined...
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined as follows: struct Employee int id; // unique employee identifier string name; // employee’s full name double rate; // employee’s hourly rate double hours; // how many hours they worked since last pay double taxable; // the current year’s taxable income }; This definition should appear above the main() function. The main() function should include: 1. Declare a single array to hold at most...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT