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...
Overview: implement the ADT List in Java. This program is meant to the ADT List from...
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList. Requirements You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will...
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...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ 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...
Create a List Create a class that holds an ordered list of items. This list should...
Create a List Create a class that holds an ordered list of items. This list should have a variable size, most importantly this class should implement the interface SimpleArrayList. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other words, you have to write the code for each of the functions specified in the SimpleArrayList interface. You are not allowed to use any 3rd party data structures or libraries...
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.
List.h template class List { // List class ADT              public:    virtual void...
List.h template class List { // List class ADT              public:    virtual void clear() = 0; // Inserts an item into the list at position index    virtual bool insert(const ListItemType& newItem) = 0;    // Append "it" at the end of the list    // The client must ensure that the list's capacity is not exceeded    virtual bool append(const ListItemType& newItem) = 0;    // Deletes an item from the list at a given position...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT