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...
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...
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT