14,392,366 members

# ATL / WTL / STL

 First PrevNext
 HOW TO ANSWER A QUESTION Chris Maunder16-Jul-09 4:09 Chris Maunder 16-Jul-09 4:09
 How to get an answer to your question Chris Maunder16-Jul-09 4:05 Chris Maunder 16-Jul-09 4:05
 To insert a node at the back of a XOR doubly linked list Tarun Jha17-Oct-19 10:38 Tarun Jha 17-Oct-19 10:38
 here is the method to insert a node at the front in the XORed doubly linked list or also known as memory efficient doubly linked list : ```// To write a memory efficient linked list #include using namespace std; // Node structure of a memory efficient doubly linked list class Node { public: int data; Node *npx; // XOR of next and previous node }; // return XORed value of the node address Node *XOR(Node *a, Node *b){ return ((Node *)( (uintptr_t)(a) ^ (uintptr_t)(b) )); } // insert a node at the beggining of the XORed linked list and makes the newly inserted node as head void insert(Node **head_ref, int data){ // allocate memory for new node Node *new_node = new Node(); new_node->data = data; // since node is inserted at the beggining, npx of the new node will always be XOR of curent head and null new_node->npx = XOR((*head_ref), nullptr); // if the linked list is not empty, then npx of current head node will be XOR of new node and node next to current head if(*head_ref != nullptr){ // (*head_ref)->npx is XOR of null and next. // so if we do XOR of it with null, we get next Node *next = XOR((*head_ref)->npx, nullptr); (*head_ref)->npx = XOR(new_node, next); } // change head *head_ref = new_node; } // prints contents of doubly linked list in forward direction void printList(Node *head){ Node *curr = head, *prev = nullptr, *next; cout << "Following are the nodes of Linked List: \n"; while(curr != nullptr) { // print current node cout << curr->data << " "; // get the address of next node : curr->npa is next^prev, // so curr->npx^prev will be next^prev^prev which is next next = XOR(prev, curr->npx); // update prev and curr for next iteration prev = curr; curr = next; } } // Driver function int main(){ Node *head = nullptr; insert(&head, 10); insert(&head, 20); insert(&head, 30); insert(&head, 40); insert(&head, 50); insert(&head, 60); printList(head); return 0; }``` Here is the code i have tried to put an element at the back of the list: ```Node *insert(Node **last, int data){ // allocate memory for new node Node *new_node = new Node(); new_node->data = data; new_node->npx = XOR(*last, nullptr); if(*last != nullptr) { Node *prev = XOR((*last)->npx, nullptr); (*last)->npx = XOR(prev, new_node); } // *last = new_node; return (new_node); } // Driver function int main(){ Node *head = nullptr; head = insert(&head, 10); head = insert(&head, 20); head = insert(&head, 30); head = insert(&head, 40); head = insert(&head, 50); head = insert(&head, 60); printList(head); return 0; }``` But it is not working output: it is same in both the cases Thank you
 Why non-template function does not compile where as template function compiles? PBMBJoshi3-May-19 0:23 PBMBJoshi 3-May-19 0:23
 Re: Why non-template function does not compile where as template function compiles? Richard MacCutchan3-May-19 0:29 Richard MacCutchan 3-May-19 0:29
 Re: Why non-template function does not compile where as template function compiles? k505410-Oct-19 9:53 k5054 10-Oct-19 9:53
 Advice on interdependent asynchronous functions and task queuing? arnold_w8-Feb-19 0:07 arnold_w 8-Feb-19 0:07
 Re: Advice on interdependent asynchronous functions and task queuing? arnold_w8-Feb-19 0:34 arnold_w 8-Feb-19 0:34
 Recommended way to deal with queues and pointers to buffers arnold_w3-Feb-19 10:09 arnold_w 3-Feb-19 10:09
 Re: Recommended way to deal with queues and pointers to buffers Richard MacCutchan3-Feb-19 23:19 Richard MacCutchan 3-Feb-19 23:19
 Re: Recommended way to deal with queues and pointers to buffers arnold_w4-Feb-19 0:40 arnold_w 4-Feb-19 0:40
 Re: Recommended way to deal with queues and pointers to buffers Richard MacCutchan4-Feb-19 0:48 Richard MacCutchan 4-Feb-19 0:48
 Re: Recommended way to deal with queues and pointers to buffers arnold_w4-Feb-19 4:15 arnold_w 4-Feb-19 4:15
 Re: Recommended way to deal with queues and pointers to buffers Richard MacCutchan4-Feb-19 4:46 Richard MacCutchan 4-Feb-19 4:46
 Re: Recommended way to deal with queues and pointers to buffers arnold_w4-Feb-19 4:55 arnold_w 4-Feb-19 4:55
 Re: Recommended way to deal with queues and pointers to buffers Richard MacCutchan4-Feb-19 5:04 Richard MacCutchan 4-Feb-19 5:04
 Re: Recommended way to deal with queues and pointers to buffers Business2sell18-Feb-19 19:08 Business2sell 18-Feb-19 19:08
 Can a union in standard C "skip" members? arnold_w31-Jan-19 23:46 arnold_w 31-Jan-19 23:46
 Re: Can a union in standard C "skip" members? Richard MacCutchan1-Feb-19 0:18 Richard MacCutchan 1-Feb-19 0:18
 Re: Can a union in standard C "skip" members? arnold_w1-Feb-19 0:47 arnold_w 1-Feb-19 0:47
 Re: Can a union in standard C "skip" members? Richard MacCutchan1-Feb-19 0:52 Richard MacCutchan 1-Feb-19 0:52
 Re: Can a union in standard C "skip" members? arnold_w1-Feb-19 1:13 arnold_w 1-Feb-19 1:13
 Re: Can a union in standard C "skip" members? Richard MacCutchan1-Feb-19 2:42 Richard MacCutchan 1-Feb-19 2:42
 Re: Can a union in standard C "skip" members? arnold_w1-Feb-19 3:08 arnold_w 1-Feb-19 3:08
 Re: Can a union in standard C "skip" members? Richard MacCutchan1-Feb-19 3:57 Richard MacCutchan 1-Feb-19 3:57
 Last Visit: 15-Dec-19 5:45     Last Update: 15-Dec-19 5:45 Refresh 1234567891011 Next »