In: Computer Science
Programming C: Write a program for a Rolodex of contact information (e.g., name, phone number, email) implemented as a linked list. The program will ask the user to enter a new contact information, retrieve/print a person’s contact information, and to delete a person. It will maintain the linked list in alphabetical order by last name. It will also allow the user to search for a person’s contact information by last name. Assume that all last names are unique.
//C program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
char firstName[20];
char lastName[20];
int phoneNumber;
char email[25];
struct node*next;
}Node;
void Insert(Node** head, Node* new_node)
{
Node* current;
if (*head == NULL ||
strcmp((*head)->lastName,new_node->lastName)>0)
{
new_node->next = *head;
*head = new_node;
}
else
{
current = *head;
while (current->next!=NULL &&
strcmp(current->lastName,new_node->lastName)<0)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
void print(char *name,Node*head) {
while(head
&&strcmp(head->lastName,name)!=0)head =
head->next;
if(!head){
printf("Not found in list
\n");
return;
}
printf("Name : %s %s
\n",head->firstName,head->lastName);
printf("Phone Number :
%d\n",head->phoneNumber);
printf("Email : %s\n\n",head->email);
}
void deletion(Node**head,char*name){
Node* temp = *head, *prev;
if (temp != NULL &&
strcmp(temp->lastName,name)==0)
{
*head = temp->next;
free(temp);
return;
}
while (temp != NULL &&
strcmp(temp->lastName,name)!=0)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
int main(){
Node*head=NULL;
int choice;
char first[20],last[20],email[25];
int phone;
do{
printf("\n1:insert\n2:print\n3:Delete\n4:Quit\nEnter choice :
");
scanf("%d",&choice);
switch(choice){
case 1:{
printf("Enter first name : ");
scanf("%s",first);
printf("Enter last name : ");
scanf("%s",last);
printf("Enter phone number : ");
scanf("%d",&phone);
printf("Enter Email : ");
scanf("%s",email);
Node*newNode =
(Node*)malloc(sizeof(Node));
strcpy(newNode->firstName, first);
strcpy(newNode->lastName, last);
newNode->phoneNumber= phone;
strcpy(newNode->email, email);
Insert(&head,newNode);
break;
}
case 2:{
printf("Enter last name : ");
scanf("%s",last);
print(last,head);
break;
}
case 3:{
printf("Enter last name : ");
scanf("%s",last);
deletion(&head,last);
break;
}
case 4:{
printf("\nthank you!\n");
break;
}
default:{
printf("Invalid choice\n");
break;
}
}
}while(choice!=4);
return 0;
}
//sample output