Advertisement
nuray__alam

Problem 4

Dec 8th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. typedef struct node
  5. {
  6. char name[40],phone[20];
  7. struct node *next;
  8. }node;
  9.  
  10. node *head=NULL;
  11.  
  12. void insertlast(char n[], char p[])
  13. {
  14. node *nd=(node*)malloc(sizeof(node));
  15. nd->next=NULL;
  16. node *list=head;
  17. strcpy(nd->name,n);
  18. strcpy(nd->phone,p);
  19. if(head==NULL) head=nd;
  20. else{
  21. while(list->next!=NULL){
  22. list=list->next;
  23. }
  24. list->next=nd;
  25. }
  26. return;
  27. }
  28. void display(int n)
  29. {
  30. int i;
  31. node *list=head;
  32. printf("You have %d friends in your list\n",n);
  33. while(list!=NULL){
  34. printf("Friend %d:\nName: %s\nPhone: %s\n",++i,list->name,list->phone);
  35. list=list->next;
  36. }
  37. return;
  38. }
  39.  
  40. int main()
  41. {
  42. int n,i;
  43. char name[40],phone[20];
  44. printf("Number of friends: ");
  45. scanf("%d",&n);
  46. printf("\n");
  47. for(i=1;i<=n;i++){
  48. printf("Enter the name: ");
  49. scanf(" %[^\n]",name);
  50. printf("\n");
  51. printf("Enter the phone number: ");
  52. scanf(" %[^\n]",phone);
  53. printf("\n");
  54.  
  55. insertlast(name,phone);
  56. }
  57. display(n);
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement