Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming in C: Singly Linked List In this lab you design and implement one of

ID: 3746730 • Letter: P

Question

Programming in C: Singly Linked List

In this lab you design and implement one of the essential data structures of all operating systems. You are going to develop a "linked list". As discussed in class, operating systems use linked list to store and keep various information including but not limited to process information, scheduling data, I/O devices, etc.

For this project, you are designing a singly linked list which is used in a "phone book". The "phone book" stores the contact information in three data fields:

char First_Name[20];
char Last_Name[50];
char Phone_Number[10];

Your data is stored in a singly linked list with root node of root. Your program should provide the following functions:

Add, Search, Remove, Print [or] List

These functions behave as the followings:

Add: this function adds a contact to the linked list. Your function should ask for first name, last name and phone number. Then it should add the node to either head or tail of the linked list.

Search: this function searches for first name and returns the contact information (i.e. first and last name and the phone number) if the name exists in the list and returns an appropriate message (for example: contact does not exist in the list).

Remove: this function searches the list for the first name and removes the contact is the contact exists in the list. Otherwise, an appropriate message is printed.

Print [or] List: this function prints all the contacts in an appropriate format to show all the names along with the contact information.

You should submit your source code along with all other header files or function definition files (if you have them in separate files). Make sure your code compiles and executes before submission.

Explanation / Answer

#include #include #include struct phonebook { char First_Name[20]; char Last_Name[50]; char Phone_Number[10]; struct phonebook *next; }*ph; void append(struct phonebook *pb) { struct phonebook *temp,*right; temp= (struct phonebook *)malloc(sizeof(struct phonebook)); temp->First_Name=pb.First_Name; temp->Last_Name=pb.Last_Name; temp->Phone_Number=pb.Phone_Number; right=(struct phonebook *)ph; while(right->next != NULL) right=right->next; right->next =temp; right=temp; right->next=NULL; } void add(struct phonebook *pb) { struct phonebook *temp; temp=(struct phonebook *)malloc(sizeof(struct phonebook)); temp->First_Name=pb.First_Name; temp->Last_Name=pb.Last_Name; temp->Phone_Number=pb.Phone_Number; if (ph== NULL) { ph=temp; ph->next=NULL; } else { temp->next=ph; ph=temp; } } void addafter(struct phonebook *pb, int loc) { int i; struct phonebook *temp,*left,*right; right=ph; for(i=1;inext; } temp=(struct phonebook *)malloc(sizeof(struct phonebook)); temp->First_Name=pb.First_Name; temp->Last_Name=pb.Last_Name; temp->Phone_Number=pb.Phone_Number; left->next=temp; left=temp; left->next=right; return; } void insert(struct phonebook *pb) { int c=0; struct phonebook *temp; temp=ph; if(temp==NULL) { add(pb); } else { if(c==0) add(pb); else if(cnext; else { printf("Key not found "); return 0; } } } } int delete(char[] Firstname) { struct phonebook *temp, *prev; temp=ph; while(temp!=NULL) { if (temp.First_Name == Firstname) { if(temp==ph) { ph=temp->next; free(temp); return 1; } else { prev->next=temp->next; free(temp); return 1; } } else { prev=temp; temp= temp->next; } } return 0; } void display(struct phonebook *r) { r=ph; if(r==NULL) { return; } while(r!=NULL) { printf("%s",r->First_Name); printf("%s",r->Last_Name); printf("%s",r->Phone_Number); r=r->next; } printf(" "); } int count() { struct phonebook *n; int c=0; n=ph; while(n!=NULL) { n=n->next; c++; } return c; } int main() { int i; struct phonebook *n; struct phonebook pb; ph=NULL; while(1) { printf(" List Operations "); printf("=============== "); printf("1.Insert Data into Phonebook "); printf("2.Display Data "); printf("3.Search Data "); printf("4.Delete "); printf("5.Exit "); printf("Enter your choice : "); if(scanf("%d",&i)