fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define gc getchar_unlocked
  4. inline int scan(){register int n=0,c=gc();while(c<'0'||c>'9')c=gc();while(c<='9'&&c>='0')n=(n<<1)+(n<<3)+c-'0',c=gc();return n;}
  5.  
  6. typedef struct node
  7. {
  8. int data;
  9. struct node* np; /* XOR of next and previous node */
  10. }node;
  11. node *head, *tail;
  12.  
  13. struct node* XOR (struct node *a, struct node *b){
  14. return (struct node*) ((unsigned int) (a) ^ (unsigned int) (b));
  15. }
  16. void insert(int data)
  17. {
  18. node *new_node = (node*) malloc(sizeof(node));
  19. new_node->data = data;
  20.  
  21. if (NULL == head) {
  22. new_node->np = NULL;
  23. head = tail = new_node;
  24. }
  25. //else if (at_tail) {
  26. else{
  27. new_node->np = XOR(tail, NULL);
  28. tail->np = XOR(new_node, XOR(tail->np, NULL));
  29. tail = new_node;
  30. }
  31. /*else { //code to enter new node at head
  32.   new_node->np = XOR(NULL, head);
  33.   head->np = XOR(new_node, XOR(NULL, head->np));
  34.   head = new_node;
  35.   }*/
  36. }
  37.  
  38.  
  39. void printList (struct node *head)
  40. {
  41. struct node *curr = head;
  42. struct node *prev = NULL;
  43. struct node *next;
  44. printf ("Following are the nodes of Linked List: \n");
  45. while (curr != NULL){
  46. printf ("%d ", curr->data);
  47. next = XOR (prev, curr->np);
  48. prev = curr;
  49. curr = next;
  50. }
  51. }
  52.  
  53. // Driver program to test above functions
  54. int main ()
  55. {
  56. //struct node *head = (struct node *) malloc (sizeof (struct node) );
  57. head = NULL;
  58.  
  59.  
  60. int t,n;
  61. t=scan();
  62.  
  63. while(t--){
  64. n=scan();
  65. insert(n);
  66. }
  67. printList (head);
  68.  
  69. return (0);
  70. }
Success #stdin #stdout 0s 2384KB
stdin
5 10 20 30 40 50
stdout
Following are the nodes of Linked List: 
10 20 30 40 50