fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node
  5. {
  6. int data;
  7. struct node *next;
  8. };
  9. struct node *head;
  10.  
  11. void addToSLL(int n)
  12. {
  13. struct node *cur;
  14.  
  15. cur = (struct node *)malloc(sizeof(struct node));
  16. cur->next = 0;
  17. cur->data = n;
  18. if (head == 0)
  19. {
  20. head = cur;
  21. return;
  22. }
  23. else
  24. {
  25. struct node *temp = head;
  26. while (temp->next != 0)
  27. {
  28. temp = temp->next;
  29. }
  30. temp->next = cur;
  31. return;
  32. }
  33. }
  34.  
  35. void showSLL()
  36. {
  37. struct node *cur = head;
  38. while (cur != 0)
  39. {
  40. printf("%d ", cur->data);
  41. cur = cur->next;
  42. }
  43. return;
  44. }
  45.  
  46. void show_reverseSLL(int num)
  47. {
  48. int n = num;
  49. while (n--)
  50. {
  51. struct node *cur = head;
  52. for (int i = 0; i<n; i++)
  53. {
  54. cur = cur->next;
  55. }
  56. printf("%d ", cur->data);
  57. }
  58. return;
  59. }
  60.  
  61. int main(void)
  62. {
  63. int num;
  64. int data;
  65. int i;
  66.  
  67. scanf("%d", &num);
  68.  
  69. for (i = 0; i < num; i++)
  70. {
  71. scanf("%d", &data);
  72. addToSLL(data);
  73. }
  74.  
  75. show_reverseSLL(num);
  76. return 0;
  77. }
Success #stdin #stdout 0s 3460KB
stdin
3
1 2 3 
stdout
3 2 1