Question

In: Computer Science

I need to add the following to this program 1) Alphabetically sort the list of entries...

I need to add the following to this program

1) Alphabetically sort the list of entries by name (first or last).

2) Find a phone number for a given name.

3) Randomly select a friend from the phonebook for you to call.

4) Delete everyone from the phonebook at the same time.

#include <stdio.h>
#include<string.h>
#include <ctype.h>
#include<stdlib.h>


typedef struct contact {
char firstname[20];
char lastname[20];
char phoneNumber[15];
struct contact * next;
struct contact * prev;
}contact;

typedef enum {
FALSE,
TRUE
}boolean;

contact * head = NULL;

contact * insert(contact * head, contact * new_data) {
if (head == NULL) {
head = new_data;
} else {
new_data -> next = head;
new_data -> prev = NULL;
head -> prev = new_data;
head = new_data;
}

return head;
}
contact * search(contact * head, char first_name[], char last_name[]) {
contact * temp = head;
while (temp != NULL) {
if ((strcmp(temp -> firstname, first_name) == 0) && (strcmp(temp -> lastname, last_name) == 0)) {
return temp;
}
temp = temp -> next;
}
return NULL;
}
boolean isValid(char phoneNumber[]) {
   int i;
for (i = 0; phoneNumber[i] != '\0'; i++) {
if ((!isdigit(phoneNumber[i])) && (phoneNumber[i] != '-')) {
return FALSE;
}
}

return TRUE;
}

int main() {
int choice = 0;
while (choice != 4) {
printf("1.To insert\n2.To delete\n3.To display phonebook\n4.To exit\n");
scanf("%d", & choice);
switch (choice) {
case 1: {
contact * new_data = (contact * ) malloc(sizeof(contact));
char first_name[20], last_name[20], phoneNumber[15];
printf("First Name: ");
scanf("%s", first_name);
printf("Last Name: ");
scanf("%s", last_name);
printf("Phone Name: ");
scanf("%s", phoneNumber);

strcpy(new_data -> firstname, first_name);
strcpy(new_data -> lastname, last_name);
strcpy(new_data -> phoneNumber, phoneNumber);
head = insert(head, new_data);
break;
}
case 2: {
contact * old_data, * prev_temp, * next_temp;
char first_name[20], last_name[20];
printf("First Name: ");
scanf("%s", first_name);
printf("Last Name: ");
scanf("%s", last_name);
old_data = search(head, first_name, last_name);
if (old_data -> prev != NULL) {
prev_temp = old_data -> prev;
prev_temp -> next = old_data -> next;
} else {
head = old_data -> next;
}
if (old_data -> next != NULL) {
next_temp = old_data -> next;
next_temp -> prev = old_data -> prev;
}

break;
}
case 3: {
contact * temp = head;
while (temp != NULL) {
if (isValid(temp -> phoneNumber) == TRUE) {
printf("FIRST NAME: %s\t", temp -> firstname);
printf("LAST NAME: %s\t", temp -> lastname);
printf("PHONE NUM: %s\n", temp -> phoneNumber);
}
temp = temp -> next;
}
break;
}
case 4: {
break;
}
default:
printf("Not a valid choice[1-4]\n");
}
}

}

Solutions

Expert Solution

I've created the program from scratch and not using the template cause I didn't understand that easily and I felt easy to write a program from scratch. (Due to the indentation and not availability of comments I didn't understand the program)

Program: (C)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

//Function prototypes
void Add();
void Delete();
void Display();
void Alphabetical();
void Number();
void Random();
void DeleteAll();

//Structure for Phonebook list
typedef struct PhoneBookList {
    char *FirstName;
    char *LastName;
    char *PhoneNumber;  
}list;

//Structure for delete
typedef struct DeleteEntry {
    char *FirstName;
    char *LastName;
}take;


//Pointers to structures
list *lt;
take *tk;

//Global variables
int count = 0;
int delCount = 0;

int main(void) 
{ 
        //Variable declarations
        int iSelection;
        do 
        {
          //Print menu to screen, ask user for selection
          printf("Phone Book:\n\n");
          printf(" 1) Add friend\n");
          printf(" 2) Delete friend\n");
          printf(" 3) Display phone book\n");
          printf(" 4) Sort and display entries alphabetically by last name\n");
          printf(" 5) Find phone number for given name\n");
          printf(" 6) Randomly select contact\n");
          printf(" 7) Delete all contacts\n");
          printf(" 8) Exit\n");
          printf(" What do you want to do? \n");
          scanf("%d", &iSelection);
        
          switch (iSelection) 
          {
               case 1:  //Add entry
                    Add();  
                    break;
               
               case 2:  //Delete entry
                    Delete();
                    break;
                    
               case 3:  //Display all entries
                    Display();
                    break;
                    
               case 4: //Sort entries alphabetically
                    Alphabetical();
                    break;
                    
               case 5: //Find phone number
                    Number();
                    break;
                    
               case 6: //Randomly select contact
                    Random();
                    break;
                    
               case 7: //Delete all contacts
                    DeleteAll();
                    break;
                    
               case 8: //Break loop and close program
                    break;
               
               default:  //User entered an invalid number
                    printf("\nYou entered an invalid selection.  Try again.\n");
                    break;
               
            } //End Switch           
          } while (iSelection != 8);  //End Do While loop
          
          //Return used memory
          free(tk);
          free(lt);
          tk = NULL;
          lt = NULL;
          return 0; 
} //End Main


//Add an entry
void Add()
{ 
    char *leftName;
     
    if (count == 0) 
    {
        lt = (list *) malloc ((count*25) + 25);
    }
    else
    {
        lt = (list *) realloc (lt, (count*50) + 50);
    }
    
    if (lt == NULL)
    {
       printf("You cannot add more memory\n");
    }
    else
    {
       lt[count].FirstName = (char *) malloc(sizeof(char)*15);
       lt[count].LastName = (char *) malloc(sizeof(char)*15);
       lt[count].PhoneNumber = (char *) malloc(sizeof(char)*15);
       printf("\nEnter their First Name: ");
       scanf("%s", lt[count].FirstName);
       printf("\nEnter their Last Name: ");
       scanf("%s", lt[count].LastName);
       printf("\nEnter their Phone Number: ");
       scanf("%s", lt[count].PhoneNumber);
       printf("\nContact added\n");      
    }
    count++;
}//End Function

//Delete an entry
void Delete()
{
    int i;
    int q = 0;
    char *userName;
    if (delCount == 0) 
    {
        tk = (take *) malloc ((delCount*25) + 25);
    }
    else
    {
        tk = (take *) realloc (tk, (delCount*1) + 1);
    }
     
        if (tk == NULL)
        {
            printf("This cannot be deleted (out of memory)\n");
        }
        else
        {
            tk[delCount].FirstName = (char *) malloc(sizeof(char)*15);
            tk[delCount].LastName = (char *) malloc(sizeof(char)*15);
            printf("\nEnter their First Name: ");
            scanf("%s", tk[delCount].FirstName);
            printf("\nEnter their Last Name: ");
            scanf("%s", tk[delCount].LastName);
        }
        
        for (i = 0; i < count; i++)
        {
                if (lt[i].FirstName == NULL && lt[i].LastName == NULL) 
                        continue;
                        if (strcmp(lt[i].FirstName, tk[delCount].FirstName) == 0 && strcmp(lt[i].LastName, tk[delCount].LastName) == 0)
                        {
                            printf("\n%s %s has been deleted\n", lt[i].FirstName, lt[i].LastName);
                            lt[i].FirstName = NULL;
                            lt[i].LastName = NULL;
                            lt[i].PhoneNumber = NULL;
                            q = 1;
                            break;
                        }
    } //End for loop                       
         
        if (q != 1)
        {
                printf("\nThis person is not in the Phonebook\n");
        }
                delCount++;
                count--;
}//End function

//Display all phonebook entries
void Display()
{
    int i;
    printf("\nYour contacts:\n"); 
    for (i = 0; i < count; i++)
    {
        if (lt[i].FirstName != NULL && lt[i].LastName != NULL)
        {
            printf("\n%s %s: %s\n", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
        }
    }//End for loop
    system("pause");
}//End function

//Sort entries alphabetically by last name.
void Alphabetical()
{
    int i;
    int j;
    char temp[50][50]; 
    printf("\nYour contacts:\n");
    
    for (i = 0; i < count; i++)
    {
        for (j = i + 1; j < count; j++)
        {
            if (strcmp(lt[i].LastName, lt[j].LastName) > 0)
            {
                strcpy(temp[i], lt[i].LastName);
                strcpy(lt[i].LastName, lt[j].LastName);
                strcpy(lt[j].LastName, temp[i]);
            }
        }
    }//End for loop

    for (i = 0; i < count; i++)
        printf("\n%s %s: %s\n", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber); 
        system("pause");
}//End function  

//Find phone number for given name
void Number()
{  
    int i;
        int q = 0;
    char fName[25];
    char lName[25];
     
    printf("\nEnter their First Name: ");
    scanf("%s", fName);
    printf("\nEnter their Last Name: ");
    scanf("%s", lName);
       
    for (i = 0; i < count; i++)
    {
        if (strcmp(lt[i].FirstName, fName) == 0 && strcmp(lt[i].LastName, lName) == 0)
        {
            printf("\n%s %s's phone number is:  %s\n", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
            q = 1;
            break;
        }
    }
     
    if (q != 1)
    {
                printf("\nThis person is not in the Phonebook\n");
    }
    system("pause");
}//End function

//Randomly select contact
void Random()
{
        srand(time(NULL));  
        int iRandomNum;   
        iRandomNum = (rand() % count) + 1;
        printf("%s %s:  %s\n", lt[iRandomNum].FirstName, lt[iRandomNum].LastName, lt[iRandomNum].PhoneNumber);
        system("pause");
}//End function

//Delete everyone from phone book
void DeleteAll()
{
    int i;
     
    for (i = 0; i < count; i++)
    {
        do
                {
            lt[i].FirstName = NULL;
            lt[i].LastName = NULL;
            lt[i].PhoneNumber = NULL;
            break;    
        } while (i <= count); 
        }
     
    printf("\nAll contacts deleted\n\n");
    system("pause");
     
}//End function

----------------------------------------------------------------------------------------------------------------------------------------------------------Feel free to ask you doubts in the comments.


Related Solutions

Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
I need to write a method that sorts a provided Linked list with bubble sort and...
I need to write a method that sorts a provided Linked list with bubble sort and using ONLY Java List Iterator Methods (Link Below) https://docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html     public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {     // first line to start you off     ListIterator<E> iit = c.listIterator(), jit;     /**** Test code: do not modify *****/     List cc = new LinkedList(c);     Collections.sort(c);     ListIterator it1 = c.listIterator(), it2 = cc.listIterator(); while (it1.hasNext()) { if (!it1.next().equals(it2.next()))         throw new Exception("List not sorted");...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into ascending order. Use the Bubble Sort algorithm from the lecture. You can use either Base Addressing or Indexed Addressing for the arrays. For this assignment, make sure you prompt the user for the numbers. Do not hard-code them in the data section. NOTE: Declare the array last in the Data section.
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the bubble sort algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here       ...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse order." It needs subprogram and those subprogram does not have t registers.
I am trying to add two linked-list based integers, but the program keeps giving me the...
I am trying to add two linked-list based integers, but the program keeps giving me the incorrect result. I am trying to add (List 1: 43135) + (List 2: 172). I have pasted the code below #include <iostream> using namespace std; //Linked list node class Node {    public:    int num;    Node* next; }; //Function to create a new node with given numbers Node *new_Node(int num) {    Node *newNode = new Node();    newNode->num = num;   ...
I have already created the journal entries for the following information but I need the t-accounts,...
I have already created the journal entries for the following information but I need the t-accounts, income statement, statement of retained earning and balance sheet: The following transactions occurred during 2018 (the company uses a perpetual inventory system with FIFO): 1)      Jan 4 Stockholders invested an additional $10,000 cash in the business in exchange for common stock 2)      Jan 4 Purchased 20 rabbits at $50 each on account from Jelly Bean Farms. 3)      Jan 4 Established a $200 petty change...
Create a Python program that will take an unsorted list of 1000 integers and sort them...
Create a Python program that will take an unsorted list of 1000 integers and sort them using a bubble sort and an insertion sort. Your output should include displaying both sorted lists (from each algorithm) identifying each sorted list by the algorithm used.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT