Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Contact{
  6. struct Contact* next; /*next is used to navigate through structures.*/
  7. char* name;
  8. char* tel;
  9.  
  10. }Contact;
  11.  
  12.  
  13.  
  14. Contact*start; /* start always points to the first node of the linked list.*/
  15.  
  16.  
  17.  
  18.  
  19. /*Defining of the functions to be implemented*/
  20.  
  21. Contact* goEnd()
  22. {
  23. Contact* end = start;
  24. while(end->next != NULL)
  25. {
  26. end = end->next;
  27. }
  28. return end;
  29. }
  30.  
  31. void addFirst(Contact* new_contact )
  32. {
  33. if(start == NULL)
  34. {
  35. start = new_contact;
  36. start->next = NULL;
  37. }
  38. else
  39. {
  40. Contact* current = start;
  41. start = new_contact;
  42. start->next = current;
  43. }
  44. }
  45.  
  46. void addLast(Contact* new_contact)
  47. {
  48.  
  49. if(start == NULL){
  50. start = new_contact;
  51. }
  52. else
  53. {
  54. goEnd()->next = new_contact;
  55. }
  56. new_contact->next = NULL;
  57.  
  58. }
  59.  
  60. void removeByName(char*name)
  61. {
  62. Contact* sel = start;
  63. Contact** prevNext = &start;
  64. while(sel != NULL){
  65. if(sel->name == name)
  66. {
  67.  
  68. *prevNext = sel->next; /*to delete the contact and free the memory occupied*/
  69. free(sel);
  70. return;
  71. }
  72. else
  73. {
  74. prevNext = &sel->next;
  75. sel = sel->next;
  76. }
  77. }
  78. }
  79.  
  80. void removeFirst()
  81. {
  82. if(start != NULL){
  83. Contact* ers = start;
  84. start = start->next;
  85. free(ers);
  86. }
  87. }
  88.  
  89. void removeLast()
  90. {
  91. Contact* head = start;
  92. Contact** prevNext = &start;
  93. if(start != NULL){
  94. while(head->next != NULL){
  95. prevNext = &head->next;
  96. head = head->next;
  97. }
  98. free(head);
  99. *prevNext = NULL;
  100. }
  101. }
  102. void printList() /*Prints out the contact lists*/
  103. {
  104. Contact* current = start;
  105.  
  106. while(current != NULL)
  107. {
  108. printf("The Name is: %s - %s\n", current->name, current->tel);
  109. current = current->next;
  110. }
  111. printf("\n\n");
  112. }
  113.  
  114. void builtList(Contact contacts[])
  115. {
  116. Contact *ptr=NULL;
  117. int i;
  118. for(i=0; i<=6; i++)
  119. {
  120. ptr=&contacts[i];
  121. addLast(ptr);
  122. }
  123.  
  124. }
  125.  
  126. /*Implementation methods*/
  127.  
  128. int main()
  129. {
  130. Contact contacts[7]=
  131. {
  132. {NULL,"Dennis","0203/123456"},
  133. {NULL,"Chantal","0177/3123345"},
  134. {NULL,"Robert","0163/9295986"},
  135. {NULL,"Bjoern","040 - 123232345"},
  136. {NULL,"Andreas","+49 178 11903123"},
  137. {NULL,"Jenny","+41 119 34544345"},
  138. {NULL,"Zeuss","0162-123-4531698"},
  139. };
  140.  
  141. builtList(contacts);
  142. printList();
  143. printf("Removing name Andreas\n");
  144. removeByName("Andreas");
  145. printList();
  146. printf("Removing first\n");
  147. removeFirst();
  148. printList();
  149. printf("Removing last\n");
  150. removeLast();
  151. printList();
  152.  
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement