Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. typedef struct LIST {
  6. int a;
  7. struct LIST *next;
  8. } list;
  9.  
  10. void add(list *temp, int item)
  11. {
  12. list *node,
  13. *ptr = temp;
  14.  
  15. if (!temp) {
  16. temp = malloc(sizeof(list));
  17. temp->a = item;
  18. temp->next = NULL;
  19. } else {
  20. node = (list *)malloc(sizeof(list));
  21. node->a = item;
  22. node->next = NULL;
  23.  
  24. while (ptr->next != NULL) {
  25. ptr = ptr->next;
  26. }
  27.  
  28. ptr->next = node;
  29. }
  30. }
  31.  
  32. void print(list *temp)
  33. {
  34. list *node = temp;
  35.  
  36. while (node != NULL) {
  37. printf("%d ", node->a);
  38. node = node->next;
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. list *temp = NULL;
  45. int a;
  46.  
  47. while (1) {
  48. scanf_s("%d", &a);
  49.  
  50. if (a == -1)
  51. break;
  52.  
  53. add(temp, a);
  54. }
  55.  
  56. print(temp);
  57.  
  58. _getch();
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement