cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct record{
char name[64];
int number;
struct record *next;
}student;
student *addContact(student *list);
student *viewContact(student *list);
student *searchContact(student *list);
student *deleteAll(student *list);
main(){
student *ptr, *head=NULL; int option = 0, k;
while(option!=5){
printf("\n M E N U \n[1] Add Contact\n[2] View Contact\n[3] Search Contact\n[4] Delete All Contact\n[5] Exit\nOption: ");
scanf("%i",&option);
switch(option){
case 1: head = addContact(head);
break;
case 2: head = viewContact(head);
break;
case 3: head = searchContact(head);
break;
case 4: head = deleteAll(head);
break;
case 5:
exit(1);
default: printf("\nInvalid Option!\n\n");
}
}
}
student * addContact(student * list){
student * new;
new = (student *) malloc(sizeof(student));
printf("\nName: "); scanf("%s", new->name);
printf("Number: "); scanf("%i", &new->number);
new->next = list;
return new;
}
student * viewContact(student * list){
student * ptr = list;
if(NULL==ptr){
printf("\nNOTHING to view\n");
}else{
printf("\nCONTACTS\n");
while(NULL!=ptr){
printf("Name: %s\nNumber: %i\n",ptr->name, ptr->number);
ptr = ptr->next;
}
}
return list;
}
student * searchContact(student * list){
student * ptr = list;
printf("search for: ");
scanf("%s", ptr);
while(ptr){
if(strcmp(ptr->next,ptr)>0)
return ptr;
ptr=ptr->next;
}
return NULL;
//printf("NO DATA FOUND\n");
//}else{
//printf("\nName: %s Number: %d\n", ptr->name,ptr->number);
//ptr = ptr->next;
//}
}
student * deleteAll(student * list){
student * ptr = list;
while(ptr){
list = ptr->next;
free(ptr);
ptr = list;
}
return NULL;
}
** Edit **