bhupixb

Doubly Linked list

Sep 6th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef struct s{
  4. int data;
  5. struct s *prev, *next;
  6. }node;
  7.  
  8. int main(int argc, char const *argv[]){
  9. node *head, *temp;
  10. int i, n;
  11. head = new node;
  12. head -> prev = NULL;
  13. head -> next = NULL;
  14. scanf("%d", &n);
  15. scanf("%d", &(head->data));
  16.  
  17. for(i = 1; i < n; i++){
  18. temp = new node;
  19. temp->prev = head;
  20. temp->next = NULL;
  21. head->next = temp;
  22. head = head->next;
  23. scanf("%d", &(head->data));
  24. }
  25.  
  26. while(head != NULL){
  27. printf("%d ", head->data);
  28. head = head->prev;
  29. }
  30. return 0;
  31. }
  32.  
  33.  
  34. #include <bits/stdc++.h>
  35. using namespace std;
  36. typedef struct s{
  37. int data;
  38. struct s *prev, *next;
  39. }node;
  40.  
  41. int main(int argc, char const *argv[]){
  42. node *head, *temp;
  43. int i, n;
  44. head = new node;
  45. head -> prev = NULL;
  46. head -> next = NULL;
  47. scanf("%d", &n);
  48. //
  49. //scanf("%d", &(head->data));
  50.  
  51. for(i = 0; i < n; i++){
  52. temp = new node;
  53. temp->prev = head;
  54. temp->next = NULL;
  55. head->next = temp;
  56. head = head->next;
  57. scanf("%d", &(head->data));
  58. }
  59. // 0 based index (head->prev != NULL)
  60. // 1 based index (head != NULL) and ek input pehle lena padega
  61. while(head->prev != NULL){
  62. printf("%d ", head->data);
  63. head = head->prev;
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70. return 0;
  71. }
  72.  
  73. // C
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76.  
  77. typedef struct s{
  78. int data;
  79. struct s *prev, *next;
  80. }node;
  81.  
  82. int main(int argc, char const *argv[]){
  83. node *head, *temp;
  84. int i, n;
  85. head = (node*) malloc(sizeof(node));
  86. head -> prev = NULL;
  87. head -> next = NULL;
  88. scanf("%d", &n);
  89. //
  90. //scanf("%d", &(head->data));
  91.  
  92. for(i = 0; i < n; i++){
  93. temp = (node*) malloc(sizeof(node));
  94. temp->prev = head;
  95. temp->next = NULL;
  96. head->next = temp;
  97. head = head->next;
  98. scanf("%d", &(head->data));
  99. }
  100. // 0 based index (head->prev != NULL)
  101. // 1 based index (head != NULL) and ek input pehle lena padega
  102. while(head->prev != NULL){
  103. printf("%d ", head->data);
  104. head = head->prev;
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. return 0;
  112. }
Add Comment
Please, Sign In to add comment